syedahsn commented on code in PR #30945:
URL: https://github.com/apache/airflow/pull/30945#discussion_r1218436958


##########
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:
   This was a point that was brought up in a [previous 
PR](https://github.com/apache/airflow/pull/30853#discussion_r1185794972) (by 
@dstandish :D), and the solution we went with was to essentially use both. Set 
a timeout on the operator level that is computed from the given parameters (as 
well as a 60 second buffer), but also use the number of attempts as a metric. I 
think it is beneficial to have the number of attempts used as a metric in the 
Triggers, but it is definitely a good idea to have the `timeout` set on the 
operator level in case of a Trigger restart, as mentioned above.



-- 
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]

Reply via email to