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



##########
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:
       Some additional context:
   
   > it takes the execution time (which falls after the interval ends) and then 
adds 1 day to it to find the next start of the next interval. But that ends up 
skipping an interval, if I'm not mistaken.
   
   This makes me think maybe we should pass the entire `DagRunInfo` into 
`next_dagrun_info` instead of just the previous run date. It is of course not 
too difficult to reverse-infer the data interval from the run date in this 
example, but it might not be always as easy or even possible in more elaborated 
data interval logic.




-- 
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-unsubscr...@airflow.apache.org

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


Reply via email to