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 2021/08/28 02:30:19 UTC

[GitHub] [airflow] jhtimmins commented on a change in pull request #17552: AIP 39: Documentation

jhtimmins commented on a change in pull request #17552:
URL: https://github.com/apache/airflow/pull/17552#discussion_r697792179



##########
File path: airflow/example_dags/example_workday_timetable.py
##########
@@ -0,0 +1,92 @@
+#
+# 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.
+
+"""Example DAG demostrating how to implement a custom timetable for a DAG."""
+
+# [START howto_timetable]
+from datetime import timedelta
+from typing import Optional
+
+from pendulum import Date, DateTime, Time, timezone
+
+from airflow import DAG
+from airflow.operators.dummy import DummyOperator
+from airflow.timetables.base import DagRunInfo, DataInterval, TimeRestriction, Timetable
+
+UTC = timezone("UTC")
+
+
+class AfterWorkdayTimetable(Timetable):
+
+    # [START howto_timetable_infer_data_interval]
+    def infer_data_interval(self, run_after: DateTime) -> DataInterval:
+        weekday = run_after.weekday()
+        if weekday in (0, 6):  # Monday and Sunday -- interval is last Friday.
+            days_since_friday = (run_after.weekday() - 4) % 7
+            delta = timedelta(days=days_since_friday)
+        else:  # Otherwise the interval is yesterday.
+            delta = timedelta(days=1)
+        start = DateTime.combine((run_after - delta).date(), Time.min).replace(tzinfo=UTC)
+        return DataInterval(start=start, end=(start + timedelta(days=1)))
+
+    # [END howto_timetable_infer_data_interval]
+
+    # [START howto_timetable_next_dagrun_info]
+    def next_dagrun_info(
+        self,
+        last_automated_dagrun: Optional[DateTime],
+        restriction: TimeRestriction,
+    ) -> Optional[DagRunInfo]:
+        if last_automated_dagrun is not None:
+            # There was a previous run on the regular schedule.
+            # last_automated_dagrun os the last run's logical date.
+            weekday = last_automated_dagrun.weekday()
+            if 0 <= weekday < 4:  # Monday through Thursday -- next is tomorrow.
+                delta = timedelta(days=1)
+            else:  # Week is ending -- skip to next Monday.
+                delta = timedelta(days=(7 - weekday))
+            start = DateTime.combine((last_automated_dagrun + delta).date(), Time.min)

Review comment:
       Reminder @uranusjr that there is a bug in this example. The next interval should start at a delta from the start of the incoming dagrun, not the dagrun execution time.




-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org