dstandish commented on code in PR #30945: URL: https://github.com/apache/airflow/pull/30945#discussion_r1218325194
########## airflow/providers/amazon/aws/triggers/emr.py: ########## @@ -0,0 +1,105 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import asyncio +from functools import cached_property +from typing import Any, AsyncIterator + +from botocore.exceptions import WaiterError + +from airflow.providers.amazon.aws.hooks.emr import EmrContainerHook +from airflow.triggers.base import BaseTrigger, TriggerEvent + + +class EmrContainerSensorTrigger(BaseTrigger): + """ + Poll for the status of EMR container until reaches terminal state + + :param virtual_cluster_id: Reference Emr cluster id + :param job_id: job_id to check the state + :param max_tries: maximum try attempts for polling the status + :param aws_conn_id: Reference to AWS connection id + :param poll_interval: polling period in seconds to check for the status + """ + + def __init__( + self, + virtual_cluster_id: str, + job_id: str, + aws_conn_id: str = "aws_default", + poll_interval: int = 30, + max_attempts: int = 30, + **kwargs: Any, + ): + self.virtual_cluster_id = virtual_cluster_id + self.job_id = job_id + self.aws_conn_id = aws_conn_id + self.poll_interval = poll_interval + self.max_attempts = max_attempts + super().__init__(**kwargs) + + @cached_property + def hook(self) -> EmrContainerHook: + return EmrContainerHook(self.aws_conn_id, virtual_cluster_id=self.virtual_cluster_id) + + def serialize(self) -> tuple[str, dict[str, Any]]: + """Serializes EmrContainerSensorTrigger arguments and classpath.""" + return ( + "airflow.providers.amazon.aws.triggers.emr.EmrContainerSensorTrigger", + { + "virtual_cluster_id": self.virtual_cluster_id, + "job_id": self.job_id, + "aws_conn_id": self.aws_conn_id, + "max_attempts": self.max_attempts, + "poll_interval": self.poll_interval, + }, + ) + + async def run(self) -> AsyncIterator[TriggerEvent]: + async with self.hook.async_conn as client: + waiter = self.hook.get_waiter("container_job_complete", deferrable=True, client=client) + attempt = 0 + while attempt < self.max_attempts: Review Comment: Though I have seen it elsewhere, to me, putting "max_attempts" as a trigger parameter doesn't make sense. The "right" way to set a time limit on a trigger is the deferral timeout. IMO, if an existing operator has max_attempts param, then we should just calculate the deferral timeout based on that number and use that. This avoids the added complexity in the trigger, the extra signature param, and it avoids the odd fact that, if max attempts is used, then if a triggerer dies and the trigger is picked up again on another machine, it will start from zero and then get killed by the timeout anyway. What do you think about this @pankajastro ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
