yustoris commented on a change in pull request #4291: [AIRFLOW-1488] Add the 
TriggeredDagRunSensor operator
URL: https://github.com/apache/airflow/pull/4291#discussion_r298830352
 
 

 ##########
 File path: airflow/contrib/sensors/triggered_dagrun_sensor.py
 ##########
 @@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed 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 airflow.exceptions import AirflowException
+from airflow.utils import db
+from airflow.utils.state import State
+from airflow.utils.decorators import apply_defaults
+from airflow.utils.trigger_rule import TriggerRule
+from airflow.models import DagRun
+from airflow.sensors.base_sensor_operator import BaseSensorOperator
+
+
+class TriggeredDagRunSensor(BaseSensorOperator):
+    """
+    Waits for triggered DAG run(s) to complete and checks status.
+
+    :param trigger_task_id: The id of the task that triggered the dags
+        and returns a list of dagrun ids to monitor.
+    :type trigger_task_id: str
+    :param sensor_rule: criteria for success after dagruns complete.
+        Default is ``TriggerRule.ONE_SUCCESS``
+    :type sensor_rule: str
+    """
+    @apply_defaults
+    def __init__(
+            self,
+            trigger_task_id,
+            sensor_rule=None,
+            *args, **kwargs):
+        super(TriggeredDagRunSensor, self).__init__(*args, **kwargs)
+        self.sensor_rule = sensor_rule or TriggerRule.ONE_SUCCESS
+        self.trigger_task_id = trigger_task_id
+
+    def poke(self, context):
+        with db.create_session() as session:
+            runcount = 0
+            ti = context['ti']
+            dagrun_ids = ti.xcom_pull(task_ids=self.trigger_task_id)
+            if dagrun_ids:
+                ids = dagrun_ids[:2]
+                ids = ids + ['...'] if len(dagrun_ids) > 2 else ids
+                self.log.info('Poking for %s', ','.join(ids))
+                runcount = session.query(DagRun).filter(
+                    DagRun.run_id.in_(dagrun_ids),
+                    DagRun.state == State.RUNNING,
+                ).count()
+            else:
+                raise AirflowException("No dagrun ids returned by '{}'".format(
+                    self.trigger_task_id))
+            self.log.info('runcount=%s', runcount)
+            if runcount == 0:
+                successcount = session.query(DagRun).filter(
+                    DagRun.run_id.in_(dagrun_ids),
+                    DagRun.state == State.SUCCESS,
+                ).count()
+                if self.sensor_rule == TriggerRule.ONE_SUCCESS:
+                    if successcount == 0:
+                        raise AirflowException('No dagruns completed 
successfully.')
+                else:
+                    raise AirflowException("sensor rule '{}' is not 
supported".format(
+                        self.sensor_rule))
 
 Review comment:
   @XD-DENG 
   I think this operator (sensor) intends to consume JUST ONE succeeded DagRun 
triggered by the corresponding `trigger_dag` per sensor run.
   So, this limits the rule to treat `TriggerRule.ONE_SUCCESS` .
   
   @ybendana 
   Is this my understand preceding right?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to