yeshbash commented on a change in pull request #19885:
URL: https://github.com/apache/airflow/pull/19885#discussion_r759673198



##########
File path: airflow/providers/amazon/aws/sensors/batch.py
##########
@@ -0,0 +1,85 @@
+# 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 typing import Dict, Optional
+
+from airflow.exceptions import AirflowException
+from airflow.providers.amazon.aws.hooks.batch_client import AwsBatchClientHook
+from airflow.sensors.base import BaseSensorOperator
+
+
+class BatchSensor(BaseSensorOperator):
+    """
+    Asks for the state of the Batch Job execution until it reaches a failure 
state or success state.
+    If the job fails, the task will fail.
+
+    :param job_id: Batch job_id to check the state for
+    :type job_id: str
+    :param aws_conn_id: aws connection to use, defaults to 'aws_default'
+    :type aws_conn_id: str
+    """
+
+    INTERMEDIATE_STATES = (
+        'SUBMITTED',
+        'PENDING',
+        'RUNNABLE',
+        'STARTING',
+        'RUNNING',
+    )
+    FAILURE_STATES = ('FAILED',)
+    SUCCESS_STATES = ('SUCCEEDED',)
+
+    template_fields = ['job_id']
+    template_ext = ()
+    ui_color = '#66c3ff'
+
+    def __init__(
+        self,
+        *,
+        job_id: str,
+        aws_conn_id: str = 'aws_default',
+        region_name: Optional[str] = None,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.job_id = job_id
+        self.aws_conn_id = aws_conn_id
+        self.region_name = region_name
+        self.hook: Optional[AwsBatchClientHook] = None
+
+    def poke(self, context: Dict) -> bool:
+        job_description = self.get_hook().get_job_description(self.job_id)
+        state = job_description['status']
+
+        if state in self.FAILURE_STATES:
+            raise AirflowException(f'Batch sensor failed. Batch Job Status: 
{state}')
+
+        if state in self.INTERMEDIATE_STATES:
+            return False

Review comment:
       This sensor's objective is to poke until the job reaches a logical end - 
success/failure. In this context, I can't think of a use-case where matching 
against any other target state can be useful. There could be other places where 
we want to check against a target state and the AwsBatchClientHook already has 
a method for this.
   
   
https://github.com/apache/airflow/blob/0df50f42dde4bd9b4c99cb6646416dde6fd4961e/airflow/providers/amazon/aws/hooks/batch_client.py#L321
   
   Let me know if I misunderstood your comment.




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