date_time.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. import datetime
  20. from typing import TYPE_CHECKING, Any, NoReturn, Sequence
  21. from airflow.sensors.base import BaseSensorOperator
  22. from airflow.triggers.base import StartTriggerArgs
  23. from airflow.triggers.temporal import DateTimeTrigger
  24. from airflow.utils import timezone
  25. if TYPE_CHECKING:
  26. from airflow.utils.context import Context
  27. class DateTimeSensor(BaseSensorOperator):
  28. """
  29. Waits until the specified datetime.
  30. A major advantage of this sensor is idempotence for the ``target_time``.
  31. It handles some cases for which ``TimeSensor`` and ``TimeDeltaSensor`` are not suited.
  32. **Example** 1 :
  33. If a task needs to wait for 11am on each ``execution_date``. Using
  34. ``TimeSensor`` or ``TimeDeltaSensor``, all backfill tasks started at
  35. 1am have to wait for 10 hours. This is unnecessary, e.g. a backfill
  36. task with ``{{ ds }} = '1970-01-01'`` does not need to wait because
  37. ``1970-01-01T11:00:00`` has already passed.
  38. **Example** 2 :
  39. If a DAG is scheduled to run at 23:00 daily, but one of the tasks is
  40. required to run at 01:00 next day, using ``TimeSensor`` will return
  41. ``True`` immediately because 23:00 > 01:00. Instead, we can do this:
  42. .. code-block:: python
  43. DateTimeSensor(
  44. task_id="wait_for_0100",
  45. target_time="{{ next_execution_date.tomorrow().replace(hour=1) }}",
  46. )
  47. :param target_time: datetime after which the job succeeds. (templated)
  48. """
  49. template_fields: Sequence[str] = ("target_time",)
  50. def __init__(self, *, target_time: str | datetime.datetime, **kwargs) -> None:
  51. super().__init__(**kwargs)
  52. # self.target_time can't be a datetime object as it is a template_field
  53. if isinstance(target_time, datetime.datetime):
  54. self.target_time = target_time.isoformat()
  55. elif isinstance(target_time, str):
  56. self.target_time = target_time
  57. else:
  58. raise TypeError(
  59. f"Expected str or datetime.datetime type for target_time. Got {type(target_time)}"
  60. )
  61. def poke(self, context: Context) -> bool:
  62. self.log.info("Checking if the time (%s) has come", self.target_time)
  63. return timezone.utcnow() > timezone.parse(self.target_time)
  64. class DateTimeSensorAsync(DateTimeSensor):
  65. """
  66. Wait until the specified datetime occurs.
  67. Deferring itself to avoid taking up a worker slot while it is waiting.
  68. It is a drop-in replacement for DateTimeSensor.
  69. :param target_time: datetime after which the job succeeds. (templated)
  70. :param start_from_trigger: Start the task directly from the triggerer without going into the worker.
  71. :param trigger_kwargs: The keyword arguments passed to the trigger when start_from_trigger is set to True
  72. during dynamic task mapping. This argument is not used in standard usage.
  73. :param end_from_trigger: End the task directly from the triggerer without going into the worker.
  74. """
  75. start_trigger_args = StartTriggerArgs(
  76. trigger_cls="airflow.triggers.temporal.DateTimeTrigger",
  77. trigger_kwargs={"moment": "", "end_from_trigger": False},
  78. next_method="execute_complete",
  79. next_kwargs=None,
  80. timeout=None,
  81. )
  82. start_from_trigger = False
  83. def __init__(
  84. self,
  85. *,
  86. start_from_trigger: bool = False,
  87. end_from_trigger: bool = False,
  88. trigger_kwargs: dict[str, Any] | None = None,
  89. **kwargs,
  90. ) -> None:
  91. super().__init__(**kwargs)
  92. self.end_from_trigger = end_from_trigger
  93. self.start_from_trigger = start_from_trigger
  94. if self.start_from_trigger:
  95. self.start_trigger_args.trigger_kwargs = dict(
  96. moment=timezone.parse(self.target_time),
  97. end_from_trigger=self.end_from_trigger,
  98. )
  99. def execute(self, context: Context) -> NoReturn:
  100. self.defer(
  101. method_name="execute_complete",
  102. trigger=DateTimeTrigger(
  103. moment=timezone.parse(self.target_time),
  104. end_from_trigger=self.end_from_trigger,
  105. ),
  106. )
  107. def execute_complete(self, context: Context, event: Any = None) -> None:
  108. """Handle the event when the trigger fires and return immediately."""
  109. return None