You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2019/01/10 22:49:14 UTC

[GitHub] stale[bot] closed pull request #1964: [AIRFLOW-722] Add Celery queue sensor

stale[bot] closed pull request #1964: [AIRFLOW-722] Add Celery queue sensor
URL: https://github.com/apache/airflow/pull/1964
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/sensors/celery_queue_sensor.py b/airflow/contrib/sensors/celery_queue_sensor.py
new file mode 100644
index 0000000000..3cb7d5dc5a
--- /dev/null
+++ b/airflow/contrib/sensors/celery_queue_sensor.py
@@ -0,0 +1,87 @@
+# -*- 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.
+
+import logging
+
+from airflow.operators.sensors import BaseSensorOperator
+from airflow.utils import apply_defaults
+from celery.task.control import inspect
+
+
+class CeleryQueueSensor(BaseSensorOperator):
+    """
+    Waits for a Celery queue to be empty. By default, in order to be considered 
+    empty, the queue must not have any tasks in the ``reserved``, ``scheduled`` 
+    or ``active`` states.
+
+    :param celery_queue: The name of the Celery queue to wait for.
+    :type celery_queue: str
+    """
+    @apply_defaults
+    def __init__(
+            self,
+            celery_queue,
+            af_task_id=None,
+            *args,
+            **kwargs):
+        """The constructor."""
+        super(CeleryQueueSensor, self).__init__(*args, **kwargs)
+        self.celery_queue = celery_queue
+        self.af_task_id = af_task_id
+
+    def _check_task_id(self, context):
+        """
+        Gets the returned Celery result from the Airflow task
+        ID provided to the sensor, and returns True if the
+        celery result has been finished execution.
+
+        :param context: Airflow's execution context
+        :type context: dict
+        :return: True if task has been executed, otherwise False
+        :rtype: bool
+        """
+        ti = context['ti']
+        celery_result = ti.xcom_pull(task_ids=self.af_task_id)
+        return celery_result.ready()
+
+    def poke(self, context):
+        """Poke."""
+        from celery.task.control import inspect
+
+        if self.af_task_id:
+            return self._check_task_id(context)
+
+        i = inspect()
+        reserved = i.reserved()
+        scheduled = i.scheduled()
+        active = i.active()
+
+        try:
+            reserved = len(reserved[self.celery_queue])
+            scheduled = len(scheduled[self.celery_queue])
+            active = len(active[self.celery_queue])
+
+            logging.info(
+                'Checking if celery queue {0} is empty.'.format(
+                    self.celery_queue
+                )
+            )
+
+            return reserved == 0 and scheduled == 0 and active == 0
+        except KeyError:
+            raise KeyError(
+                'Could not locate Celery queue {0}'.format(
+                    self.celery_queue
+                )
+            )


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services