weekday.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 warnings
  20. from typing import TYPE_CHECKING, Iterable
  21. from airflow.exceptions import RemovedInAirflow3Warning
  22. from airflow.sensors.base import BaseSensorOperator
  23. from airflow.utils import timezone
  24. from airflow.utils.weekday import WeekDay
  25. if TYPE_CHECKING:
  26. from airflow.utils.context import Context
  27. class DayOfWeekSensor(BaseSensorOperator):
  28. """
  29. Waits until the first specified day of the week.
  30. For example, if the execution day of the task is '2018-12-22' (Saturday)
  31. and you pass 'FRIDAY', the task will wait until next Friday.
  32. **Example** (with single day): ::
  33. weekend_check = DayOfWeekSensor(
  34. task_id="weekend_check", week_day="Saturday", use_task_logical_date=True, dag=dag
  35. )
  36. **Example** (with multiple day using set): ::
  37. weekend_check = DayOfWeekSensor(
  38. task_id="weekend_check", week_day={"Saturday", "Sunday"}, use_task_logical_date=True, dag=dag
  39. )
  40. **Example** (with :class:`~airflow.utils.weekday.WeekDay` enum): ::
  41. # import WeekDay Enum
  42. from airflow.utils.weekday import WeekDay
  43. weekend_check = DayOfWeekSensor(
  44. task_id="weekend_check",
  45. week_day={WeekDay.SATURDAY, WeekDay.SUNDAY},
  46. use_task_logical_date=True,
  47. dag=dag,
  48. )
  49. :param week_day: Day of the week to check (full name). Optionally, a set
  50. of days can also be provided using a set.
  51. Example values:
  52. * ``"MONDAY"``,
  53. * ``{"Saturday", "Sunday"}``
  54. * ``{WeekDay.TUESDAY}``
  55. * ``{WeekDay.SATURDAY, WeekDay.SUNDAY}``
  56. To use ``WeekDay`` enum, import it from ``airflow.utils.weekday``
  57. :param use_task_logical_date: If ``True``, uses task's logical date to compare
  58. with week_day. Execution Date is Useful for backfilling.
  59. If ``False``, uses system's day of the week. Useful when you
  60. don't want to run anything on weekdays on the system.
  61. :param use_task_execution_day: deprecated parameter, same effect as `use_task_logical_date`
  62. .. seealso::
  63. For more information on how to use this sensor, take a look at the guide:
  64. :ref:`howto/operator:DayOfWeekSensor`
  65. """
  66. def __init__(
  67. self,
  68. *,
  69. week_day: str | Iterable[str] | WeekDay | Iterable[WeekDay],
  70. use_task_logical_date: bool = False,
  71. use_task_execution_day: bool = False,
  72. **kwargs,
  73. ) -> None:
  74. super().__init__(**kwargs)
  75. self.week_day = week_day
  76. self.use_task_logical_date = use_task_logical_date
  77. if use_task_execution_day:
  78. self.use_task_logical_date = use_task_execution_day
  79. warnings.warn(
  80. "Parameter ``use_task_execution_day`` is deprecated. Use ``use_task_logical_date``.",
  81. RemovedInAirflow3Warning,
  82. stacklevel=2,
  83. )
  84. self._week_day_num = WeekDay.validate_week_day(week_day)
  85. def poke(self, context: Context) -> bool:
  86. self.log.info(
  87. "Poking until weekday is in %s, Today is %s",
  88. self.week_day,
  89. WeekDay(timezone.utcnow().isoweekday()).name,
  90. )
  91. if self.use_task_logical_date:
  92. return context["logical_date"].isoweekday() in self._week_day_num
  93. else:
  94. return timezone.utcnow().isoweekday() in self._week_day_num