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 2018/08/29 15:47:23 UTC

[GitHub] andscoop closed pull request #3702: [AIRFLOW-81] Add ScheduleBlackoutSensor

andscoop closed pull request #3702: [AIRFLOW-81] Add ScheduleBlackoutSensor
URL: https://github.com/apache/incubator-airflow/pull/3702
 
 
   

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/schedule_blackout_sensor.py b/airflow/contrib/sensors/schedule_blackout_sensor.py
new file mode 100644
index 0000000000..ac66cbb3b2
--- /dev/null
+++ b/airflow/contrib/sensors/schedule_blackout_sensor.py
@@ -0,0 +1,99 @@
+# -*- coding: utf-8 -*-
+#
+# 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 airflow.sensors.base_sensor_operator import BaseSensorOperator
+from airflow.utils.decorators import apply_defaults
+from datetime import datetime
+
+
+class ScheduleBlackoutSensor(BaseSensorOperator):
+    """
+    Checks to see if a task is running for a specified date and time criteria
+    Returns false if sensor is running within "blackout" criteria, true otherwise
+
+    :param month_of_year: Integer representing month of year
+        Not checked if left to default to None
+    :type month_of_year: int
+    :param day_of_month: Integer representing day of month
+        Not checked if left to default to None
+    :type day_of_month: int
+    :param hour_of_day: Integer representing hour of day
+        Not checked if left to default to None
+    :type hour_of_day: int
+    :param min_of_hour: Integer representing minute of hour
+        Not checked if left to default to None
+    :type min_of_hour: int
+    :param day_of_week: Integer representing day of week
+        Not checked if left to default to None
+    :type day_of_week: int
+    :param day_of_week: Datetime object to check criteria against
+        Defaults to datetime.now() if set to none
+    :type day_of_week: datetime
+    """
+
+    @apply_defaults
+    def __init__(self,
+                 month_of_year=None, day_of_month=None,
+                 hour_of_day=None, min_of_hour=None,
+                 day_of_week=None,
+                 dt=None, *args, **kwargs):
+
+        super(ScheduleBlackoutSensor, self).__init__(*args, **kwargs)
+
+        self.dt = dt
+        self.month_of_year = month_of_year
+        self.day_of_month = day_of_month
+        self.hour_of_day = hour_of_day
+        self.min_of_hour = min_of_hour
+        self.day_of_week = day_of_week
+
+    def _check_criteria(self, crit, datepart):
+        if crit is None:
+            return None
+
+        elif isinstance(crit, list):
+            for i in crit:
+                if i == datepart:
+                    return True
+            return False
+        elif isinstance(crit, int):
+            return True if datepart == crit else False
+        else:
+            raise TypeError(
+                "Expected an interger or a list, received a {0}".format(type(crit)))
+
+    def poke(self, context):
+        self.dt = datetime.now() if self.dt is None else self.dt
+
+        criteria = [
+            # month of year
+            self._check_criteria(self.month_of_year, self.dt.month),
+            # day of month
+            self._check_criteria(self.day_of_month, self.dt.day),
+            # hour of day
+            self._check_criteria(self.hour_of_day, self.dt.hour),
+            # minute of hour
+            self._check_criteria(self.min_of_hour, self.dt.minute),
+            # day of week
+            self._check_criteria(self.day_of_week, self.dt.weekday())
+        ]
+
+        # Removes criteria that are set to None and then checks that all
+        # specified criteria are True. If all criteria are True - returns False
+        # in order to trigger a sensor failure if blackout criteria are met
+        return not all([crit for crit in criteria if crit is not None])
diff --git a/docs/code.rst b/docs/code.rst
index 80ec76193f..db16b028b5 100644
--- a/docs/code.rst
+++ b/docs/code.rst
@@ -222,6 +222,7 @@ Sensors
 .. autoclass:: airflow.contrib.sensors.pubsub_sensor.PubSubPullSensor
 .. autoclass:: airflow.contrib.sensors.qubole_sensor.QuboleSensor
 .. autoclass:: airflow.contrib.sensors.redis_key_sensor.RedisKeySensor
+.. autoclass:: airflow.contrib.sensors.sftp_sensor.ScheduleBlackoutSensor
 .. autoclass:: airflow.contrib.sensors.sftp_sensor.SFTPSensor
 .. autoclass:: airflow.contrib.sensors.wasb_sensor.WasbBlobSensor
 
diff --git a/tests/contrib/sensors/test_schedule_blackout_sensor.py b/tests/contrib/sensors/test_schedule_blackout_sensor.py
new file mode 100644
index 0000000000..95c2e965c3
--- /dev/null
+++ b/tests/contrib/sensors/test_schedule_blackout_sensor.py
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+#
+# 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.
+
+
+import unittest
+
+from airflow.contrib.sensors.schedule_blackout_sensor import ScheduleBlackoutSensor
+from datetime import datetime
+
+TEST_DT = datetime(2000, 1, 1, 1, 1)
+DEFAULT_DATE = datetime(2015, 1, 1)
+
+
+class TestScheduleBlackoutSensor(unittest.TestCase):
+    def test_criteria_met(self):
+        task = ScheduleBlackoutSensor(
+            task_id='task',
+            month_of_year=1,
+            hour_of_day=[i + 1 for i in range(4)],
+            day_of_week=[5],
+            dt=TEST_DT
+        )
+
+        output = task.poke(None)
+        self.assertFalse(output)
+
+    def test_criteria_not_met(self):
+        task = ScheduleBlackoutSensor(
+            task_id='task',
+            month_of_year=2,
+            hour_of_day=[i for i in range(12, 24)],
+            day_of_week=[1],
+            dt=TEST_DT
+        )
+
+        output = task.poke(None)
+        self.assertTrue(output)
+
+    def test_type_check(self):
+        task = ScheduleBlackoutSensor(
+            task_id='task',
+            month_of_year="str",
+            dt=TEST_DT
+        )
+
+        with self.assertRaises(TypeError):
+            task.poke(None)
+
+
+if __name__ == '__main__':
+    unittest.main()


 

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