This is an automated email from the ASF dual-hosted git repository.
ash pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 414f41e Custom timetable must return aware datetimes (#18420)
414f41e is described below
commit 414f41e38be23489aecf4d1de7b266db1654e42c
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Thu Sep 23 00:16:52 2021 +0800
Custom timetable must return aware datetimes (#18420)
---
airflow/example_dags/plugins/workday.py | 2 +-
airflow/timetables/base.py | 17 +++++++++++++----
airflow/utils/timezone.py | 6 +++---
docs/apache-airflow/howto/timetable.rst | 6 ++++++
4 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/airflow/example_dags/plugins/workday.py
b/airflow/example_dags/plugins/workday.py
index 3b39a6e..695d9c6 100644
--- a/airflow/example_dags/plugins/workday.py
+++ b/airflow/example_dags/plugins/workday.py
@@ -59,7 +59,7 @@ class AfterWorkdayTimetable(Timetable):
delta = timedelta(days=1)
else: # Last run on Friday -- skip to next Monday.
delta = timedelta(days=(7 - last_start_weekday))
- next_start = DateTime.combine((last_start + delta).date(),
Time.min)
+ next_start = DateTime.combine((last_start + delta).date(),
Time.min).replace(tzinfo=UTC)
else: # This is the first ever run on the regular schedule.
next_start = restriction.earliest
if next_start is None: # No start_date. Don't schedule.
diff --git a/airflow/timetables/base.py b/airflow/timetables/base.py
index 1ef62ea..e97f253 100644
--- a/airflow/timetables/base.py
+++ b/airflow/timetables/base.py
@@ -23,7 +23,11 @@ from airflow.typing_compat import Protocol
class DataInterval(NamedTuple):
- """A data interval for a DagRun to operate over."""
+ """A data interval for a DagRun to operate over.
+
+ Both ``start`` and ``end`` **MUST** be "aware", i.e. contain timezone
+ information.
+ """
start: DateTime
end: DateTime
@@ -44,8 +48,10 @@ class TimeRestriction(NamedTuple):
These values are generally set on the DAG or task's ``start_date``,
``end_date``, and ``catchup`` arguments.
- Both ``earliest`` and ``latest`` are inclusive; a DAG run can happen
exactly
- at either point of time.
+ Both ``earliest`` and ``latest``, if not *None*, are inclusive; a DAG run
+ can happen exactly at either point of time. They are guaranteed to be aware
+ (i.e. contain timezone information) for ``TimeRestriction`` instances
+ created by Airflow.
"""
earliest: Optional[DateTime]
@@ -61,7 +67,10 @@ class DagRunInfo(NamedTuple):
"""
run_after: DateTime
- """The earliest time this DagRun is created and its tasks scheduled."""
+ """The earliest time this DagRun is created and its tasks scheduled.
+
+ This **MUST** be "aware", i.e. contain timezone information.
+ """
data_interval: DataInterval
"""The data interval this DagRun to operate over."""
diff --git a/airflow/utils/timezone.py b/airflow/utils/timezone.py
index 67c3b26..e5245d9 100644
--- a/airflow/utils/timezone.py
+++ b/airflow/utils/timezone.py
@@ -176,11 +176,11 @@ def parse(string: str, timezone=None) -> DateTime:
def coerce_datetime(v: Union[None, dt.datetime, DateTime]) ->
Optional[DateTime]:
- """Convert whatever is passed in to ``pendulum.DateTime``."""
+ """Convert whatever is passed in to an timezone-aware
``pendulum.DateTime``."""
if v is None:
return None
- if isinstance(v, DateTime):
- return v
if v.tzinfo is None:
v = make_aware(v)
+ if isinstance(v, DateTime):
+ return v
return pendulum.instance(v)
diff --git a/docs/apache-airflow/howto/timetable.rst
b/docs/apache-airflow/howto/timetable.rst
index 8c1354f..965d529 100644
--- a/docs/apache-airflow/howto/timetable.rst
+++ b/docs/apache-airflow/howto/timetable.rst
@@ -62,6 +62,12 @@ schedule should not include the two weekend days. What we
want is:
For simplicity, we will only deal with UTC datetimes in this example.
+.. note::
+
+ All datetime values returned by a custom timetable **MUST** be "aware",
i.e.
+ contains timezone information. Furthermore, they must use ``pendulum``'s
+ datetime and timezone types.
+
Timetable Registration
----------------------