Repository: apache/airflow
File: providers/microsoft/azure/src/airflow/providers/microsoft/azure/triggers/data_factory.py (Python)

Code before:
        self.run_id = run_id
        self.wait_for_termination = wait_for_termination
        self.resource_group_name = resource_group_name
        self.factory_name = factory_name
        self.end_time = end_time

    def serialize(self) -> tuple[str, dict[str, Any]]:
        """Serialize AzureDataFactoryTrigger arguments and classpath."""
        return (
            "airflow.providers.microsoft.azure.triggers.data_factory.AzureDataFactoryTrigger",
            {
                "azure_data_factory_conn_id": self.azure_data_factory_conn_id,
                "check_interval": self.check_interval,
                "run_id": self.run_id,
                "wait_for_termination": self.wait_for_termination,
                "resource_group_name": self.resource_group_name,
                "factory_name": self.factory_name,
                "end_time": self.end_time,
            },
        )

    async def run(self) -> AsyncIterator[TriggerEvent]:
        """Make async connection to Azure Data Factory, polls for the pipeline run status."""
        hook = AzureDataFactoryAsyncHook(azure_data_factory_conn_id=self.azure_data_factory_conn_id)
        try:
            async with hook:
                pipeline_status = await hook.get_adf_pipeline_run_status(
                    run_id=self.run_id,
                    resource_group_name=self.resource_group_name,
                    factory_name=self.factory_name,
                )
                executed_after_token_refresh = True
                if self.wait_for_termination:
                    while self.end_time > time.time():
                        try:
                            pipeline_status = await hook.get_adf_pipeline_run_status(
                                run_id=self.run_id,
                                resource_group_name=self.resource_group_name,
                                factory_name=self.factory_name,
                            )
                            executed_after_token_refresh = True
                            if pipeline_status in AzureDataFactoryPipelineRunStatus.FAILURE_STATES:
                                yield TriggerEvent(
                                    {
                                        "status": "error",
                                        "message": f"The pipeline run {self.run_id} has {pipeline_status}.",
                                        "run_id": self.run_id,
                                    }
                                )
                                return
                            elif pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:

Review comment (suggestion):
Same here:

`with AzureDataFactoryAsyncHook(azure_data_factory_conn_id=self.azure_data_factory_conn_id) as hook:`

Code after:
        check_interval: int = 60,
    ):
        super().__init__()
        self.azure_data_factory_conn_id = azure_data_factory_conn_id
        self.check_interval = check_interval
        self.run_id = run_id
        self.wait_for_termination = wait_for_termination
        self.resource_group_name = resource_group_name
        self.factory_name = factory_name
        self.end_time = end_time

    def serialize(self) -> tuple[str, dict[str, Any]]:
        """Serialize AzureDataFactoryTrigger arguments and classpath."""
        return (
            "airflow.providers.microsoft.azure.triggers.data_factory.AzureDataFactoryTrigger",
            {
                "azure_data_factory_conn_id": self.azure_data_factory_conn_id,
                "check_interval": self.check_interval,
                "run_id": self.run_id,
                "wait_for_termination": self.wait_for_termination,
                "resource_group_name": self.resource_group_name,
                "factory_name": self.factory_name,
                "end_time": self.end_time,
            },
        )

    def _build_trigger_event(self, pipeline_status: str) -> TriggerEvent | None:
        """Build TriggerEvent based on pipeline status. Returns None if status is not terminal."""
        if pipeline_status in AzureDataFactoryPipelineRunStatus.FAILURE_STATES:
            return TriggerEvent(
                {
                    "status": "error",
                    "message": f"The pipeline run {self.run_id} has {pipeline_status}.",
                    "run_id": self.run_id,
                }
            )
        if pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
            return TriggerEvent(
                {
                    "status": "success",
                    "message": f"The pipeline run {self.run_id} has {pipeline_status}.",
                    "run_id": self.run_id,
                }
            )
        return None

    async def run(self) -> AsyncIterator[TriggerEvent]:
        """Make async connection to Azure Data Factory, polls for the pipeline run status."""
        async with AzureDataFactoryAsyncHook(
            azure_data_factory_conn_id=self.azure_data_factory_conn_id
        ) as hook: