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

Code before:
        super().__init__()
        self.run_id = run_id
        self.azure_data_factory_conn_id = azure_data_factory_conn_id
        self.resource_group_name = resource_group_name
        self.factory_name = factory_name
        self.poke_interval = poke_interval

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

    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)
        executed_after_token_refresh = False
        try:
            async with hook:
                while True:
                    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 = False
                        if pipeline_status == AzureDataFactoryPipelineRunStatus.FAILED:
                            yield TriggerEvent(
                                {"status": "error", "message": f"Pipeline run {self.run_id} has Failed."}
                            )
                            return
                        elif pipeline_status == AzureDataFactoryPipelineRunStatus.CANCELLED:
                            msg = f"Pipeline run {self.run_id} has been Cancelled."
                            yield TriggerEvent({"status": "error", "message": msg})
                            return
                        elif pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
                            msg = f"Pipeline run {self.run_id} has been Succeeded."
                            yield TriggerEvent({"status": "success", "message": msg})
                            return
                        await asyncio.sleep(self.poke_interval)
                    except ServiceRequestError:
                        # conn might expire during long running pipeline.
                        # If exception is caught, it tries to refresh connection once.

Review comment (suggestion):
wouldn't it be nicer to write this like:

`with AzureDataFactoryAsyncHook(azure_data_factory_conn_id=self.azure_data_factory_conn_id) as hook:`

Code after:
        super().__init__()
        self.run_id = run_id
        self.azure_data_factory_conn_id = azure_data_factory_conn_id
        self.resource_group_name = resource_group_name
        self.factory_name = factory_name
        self.poke_interval = poke_interval

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

    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 == AzureDataFactoryPipelineRunStatus.FAILED:
            return TriggerEvent({"status": "error", "message": f"Pipeline run {self.run_id} has Failed."})
        if pipeline_status == AzureDataFactoryPipelineRunStatus.CANCELLED:
            return TriggerEvent(
                {"status": "error", "message": f"Pipeline run {self.run_id} has been Cancelled."}
            )
        if pipeline_status == AzureDataFactoryPipelineRunStatus.SUCCEEDED:
            return TriggerEvent(
                {"status": "success", "message": f"Pipeline run {self.run_id} has been Succeeded."}
            )
        return None

    async def run(self) -> AsyncIterator[TriggerEvent]:
        """Make async connection to Azure Data Factory, polls for the pipeline run status."""
        executed_after_token_refresh = False
        try:
            async with AzureDataFactoryAsyncHook(
                azure_data_factory_conn_id=self.azure_data_factory_conn_id
            ) as hook:
                while True:
                    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 = False
                        event = self._build_trigger_event(pipeline_status)
                        if event: