Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions airflow-core/src/airflow/timetables/_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,51 @@ def __init__(self, cron: str, timezone: str | Timezone | FixedTimezone) -> None:
self._timezone = timezone

try:
descriptor = ExpressionDescriptor(
expression=self._expression, casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True
)
# checking for more than 5 parameters in Cron and avoiding evaluation for now,
# as Croniter has inconsistent evaluation with other libraries
if len(croniter(self._expression).expanded) > 5:
raise FormatException()
interval_description: str = descriptor.get_description()

self.description = self._describe_with_dom_dow_fix(self._expression)

except (CroniterBadCronError, FormatException, MissingFieldException):
interval_description = ""
self.description: str = interval_description
self.description = ""

def _describe_with_dom_dow_fix(self, expression: str) -> str:
"""
Return cron description with fix for DOM+DOW conflicts.
If both DOM and DOW are restricted, explain them as OR.
"""
cron_fields = expression.split()

if len(cron_fields) < 5:
return ExpressionDescriptor(
expression, casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True
).get_description()

dom = cron_fields[2]
dow = cron_fields[4]

if dom != "*" and dow != "*":
# Case: conflict → DOM OR DOW
cron_fields_dom = cron_fields.copy()
cron_fields_dom[4] = "*"
day_of_month_desc = ExpressionDescriptor(
" ".join(cron_fields_dom), casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True
).get_description()

cron_fields_dow = cron_fields.copy()
cron_fields_dow[2] = "*"
day_of_week_desc = ExpressionDescriptor(
" ".join(cron_fields_dow), casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True
).get_description()

return f"{day_of_month_desc} (or) {day_of_week_desc}"

# no conflict → return normal description
return ExpressionDescriptor(
expression, casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True
).get_description()

def __eq__(self, other: object) -> bool:
"""
Expand Down
Loading