Skip to content

Fix DateTimeSensorAsync crashing Dag parsing with templated target_time#70310

Open
haseebmalik18 wants to merge 1 commit into
apache:mainfrom
haseebmalik18:fix-datetimesensorasync-templated-target-time
Open

Fix DateTimeSensorAsync crashing Dag parsing with templated target_time#70310
haseebmalik18 wants to merge 1 commit into
apache:mainfrom
haseebmalik18:fix-datetimesensorasync-templated-target-time

Conversation

@haseebmalik18

Copy link
Copy Markdown
Contributor

Fixes DateTimeSensorAsync crashing Dag parsing when target_time is a Jinja template and start_from_trigger=True.

With start_from_trigger=True, target_time gets parsed in __init__ at Dag-parse time, before Jinja renders it. So passing a template (which is the field's documented use) blew up with a pendulum ParserError and took down the whole Dag file. Now a static target_time still works, and a templated one raises a clear ValueError instead.

closes: #70284

:param target_time: datetime after which the job succeeds. (templated)
:param start_from_trigger: Start the task directly from the triggerer without going into the worker.
This requires a static ``target_time`` (a datetime or ISO-8601 string). A templated
``target_time`` is not supported here because the trigger is created at Dag-parse time,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trigger is not created at Dag-parse time, but either in the worker or the triggerer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right, I overlooked that. Will reword the docstring.

Comment on lines +131 to +138
try:
moment = timezone.parse(self.target_time)
except ValueError as e:
raise ValueError(
f"start_from_trigger=True requires a static target_time, not a template: {self.target_time!r}"
) from e
self.start_trigger_args.trigger_kwargs = dict(
moment=timezone.parse(self.target_time),
moment=moment,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it solve the crashing? It seems to improve the error message rather than fix the issue.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shahar1 - I think the goal was to replicate something like this: https://github.com/apache/airflow/pull/69610/changes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. As is, this just turns a cryptic ParserError into a clearer ValueError, the Dag file still fails to parse. And yes, the intent was to mirror #69610

The difference is that for TimeSensor, start_from_trigger could never work, since it computes "today at target_time" from the wall clock at parse time. Here target_time is a template field, so there are I think two ways to go about it:

  1. If target_time is not parseable at parse time, log a warning and fall back to deferring from the worker. The template renders as usual and the Dag parses fine on all supported versions.
  2. On Airflow 3.3+, pass the raw target_time string through trigger_kwargs and let the triggerer render it before the trigger runs. Since Re-enable start_from_trigger feature with rendering of template fields #55068 the triggerer renders any trigger kwarg whose name matches an operator template field, which FileSensor already relies on for filepath. That would make start_from_trigger actually work with a templated target_time, but it means extending DateTimeTrigger to accept a target_time kwarg and gating on the Airflow version, falling back to (1) on older versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTimeSensorAsync crashes Dag parsing with templated target_time + start_from_trigger=True

3 participants