datetime.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import datetime
  19. import warnings
  20. from typing import TYPE_CHECKING, Iterable
  21. from airflow.exceptions import AirflowException, RemovedInAirflow3Warning
  22. from airflow.operators.branch import BaseBranchOperator
  23. from airflow.utils import timezone
  24. if TYPE_CHECKING:
  25. from airflow.utils.context import Context
  26. class BranchDateTimeOperator(BaseBranchOperator):
  27. """
  28. Branches into one of two lists of tasks depending on the current datetime.
  29. For more information on how to use this operator, take a look at the guide:
  30. :ref:`howto/operator:BranchDateTimeOperator`.
  31. True branch will be returned when ``datetime.datetime.now()`` falls below
  32. ``target_upper`` and above ``target_lower``.
  33. :param follow_task_ids_if_true: task_id, task_group_id, or a list of task_ids and/or task_group_ids
  34. to follow if ``datetime.datetime.now()`` falls above target_lower and below target_upper.
  35. :param follow_task_ids_if_false: task_id, task_group_id, or a list of task_ids and/or task_group_ids
  36. to follow if ``datetime.datetime.now()`` falls below target_lower or above target_upper.
  37. :param target_lower: target lower bound.
  38. :param target_upper: target upper bound.
  39. :param use_task_logical_date: If ``True``, uses task's logical date to compare with targets.
  40. Execution date is useful for backfilling. If ``False``, uses system's date.
  41. """
  42. def __init__(
  43. self,
  44. *,
  45. follow_task_ids_if_true: str | Iterable[str],
  46. follow_task_ids_if_false: str | Iterable[str],
  47. target_lower: datetime.datetime | datetime.time | None,
  48. target_upper: datetime.datetime | datetime.time | None,
  49. use_task_logical_date: bool = False,
  50. use_task_execution_date: bool = False,
  51. **kwargs,
  52. ) -> None:
  53. super().__init__(**kwargs)
  54. if target_lower is None and target_upper is None:
  55. raise AirflowException(
  56. "Both target_upper and target_lower are None. At least one "
  57. "must be defined to be compared to the current datetime"
  58. )
  59. self.target_lower = target_lower
  60. self.target_upper = target_upper
  61. self.follow_task_ids_if_true = follow_task_ids_if_true
  62. self.follow_task_ids_if_false = follow_task_ids_if_false
  63. self.use_task_logical_date = use_task_logical_date
  64. if use_task_execution_date:
  65. self.use_task_logical_date = use_task_execution_date
  66. warnings.warn(
  67. "Parameter ``use_task_execution_date`` is deprecated. Use ``use_task_logical_date``.",
  68. RemovedInAirflow3Warning,
  69. stacklevel=2,
  70. )
  71. def choose_branch(self, context: Context) -> str | Iterable[str]:
  72. if self.use_task_logical_date:
  73. now = context["logical_date"]
  74. else:
  75. now = timezone.coerce_datetime(timezone.utcnow())
  76. lower, upper = target_times_as_dates(now, self.target_lower, self.target_upper)
  77. lower = timezone.coerce_datetime(lower, self.dag.timezone)
  78. upper = timezone.coerce_datetime(upper, self.dag.timezone)
  79. if upper is not None and upper < now:
  80. return self.follow_task_ids_if_false
  81. if lower is not None and lower > now:
  82. return self.follow_task_ids_if_false
  83. return self.follow_task_ids_if_true
  84. def target_times_as_dates(
  85. base_date: datetime.datetime,
  86. lower: datetime.datetime | datetime.time | None,
  87. upper: datetime.datetime | datetime.time | None,
  88. ):
  89. """Ensure upper and lower time targets are datetimes by combining them with base_date."""
  90. if isinstance(lower, datetime.datetime) and isinstance(upper, datetime.datetime):
  91. return lower, upper
  92. if lower is not None and isinstance(lower, datetime.time):
  93. lower = datetime.datetime.combine(base_date, lower)
  94. if upper is not None and isinstance(upper, datetime.time):
  95. upper = datetime.datetime.combine(base_date, upper)
  96. if lower is None or upper is None:
  97. return lower, upper
  98. if upper < lower:
  99. upper += datetime.timedelta(days=1)
  100. return lower, upper