-
Notifications
You must be signed in to change notification settings - Fork 14.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add context to execution_date_fn in ExternalTaskSensor #8702
Conversation
a1bcc9a
to
d9a5798
Compare
@jhtimmins if you don't mind when you get a chance to look at some of this and share your thoughts as well |
@Acehaidrey Happy to take a look! |
@jhtimmins thank you so much :) I know I've been taking a lot of your time these days so appreciate it! |
@Acehaidrey Of course :) Always happy to take a look. I think the use case makes sense, but the implementation could be made more explicit. Personally, I think it'd make more sense to use keyword arguments for both, and then determine behavior by the presence/absence of values. def _handle_execution_date_fn(self, execution_date=None, context={}):
# Assert that either execution_date or context is None
# Otherwise, throw an exception.
# if execution_date
# return self.execution_date_fn(execution_date)
# return self.execution_date_fn(context) This would cover the following cases:
Worth getting a few more pairs of eyes on this. What do you think about this approach? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For recommendation details, see the top-level conversation.
#8702 (comment)
@Acehaidrey I agree with @jhtimmins. I think having one function is cleaner and will make it easier if we decide to deprecate one of the methods later. |
Can you add some docs to guide? |
@jhtimmins @dimberman thank you both for your feedback. Say a user has an execution_date_fn be
So they pass this to the operator. Now in the operator execute code that we are making the changes, in the
I will be passing both from the execute method, date and context. But how do I know from the users point of view, if they were expecting the execution_date or the context. |
@jhtimmins @dimberman can you please comment when you have a minute? |
@jhtimmins sorry for pestering but would love to get these in and close out |
@jhtimmins @dimberman @ashb mind chiming in on how to make the changes or reconsider these? would love to just advance this and close it out |
@jhtimmins @ashb or anyone you think can. add to this convo mind doing that so we can get this closed out too? |
@dimberman can you please review this again so we can get it completed. Thank you @ashb for looking over this and approving |
Hi @Acehaidrey apologies for the delay. I've merged the PR and will work to backport it on 1.10.11 :) |
No problem thank you @dimberman ! |
Co-authored-by: Ace Haidrey <ahaidrey@pinterest.com> (cherry picked from commit b055151)
Co-authored-by: Ace Haidrey <ahaidrey@pinterest.com> (cherry picked from commit b055151)
Co-authored-by: Ace Haidrey <ahaidrey@pinterest.com> (cherry picked from commit b055151)
elif num_fxn_params == 2: | ||
return self.execution_date_fn(context['execution_date'], context) | ||
else: | ||
raise AirflowException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change stops people from doing loop variable capture. There are more friendly ways to allow compatibility, such as using inspect.signature
to figure out the actual argument names.
for day in range(1, 7):
sensor = ExternalTaskSensor(
task_id="sensor_{}".format(day),
external_dag_id=external_dag_id,
external_task_id=external_task_id,
execution_date_fn=lambda execution_date, local_day=day: ...,
)
Co-authored-by: Ace Haidrey <ahaidrey@pinterest.com> (cherry picked from commit b055151)
The purpose of this pr is to get more flexibility with the ExternalTaskSensor to be able to pass the context in the execution_date_fn for the purpose of doing some logic that cannot just be handled with the execution date or the time delta.
One user case that regularly comes up is that the users want to get the external task/dag id's schedule run time and do some automated logic vs trying to manually go look up the time and do a timedelta.
This allows us to build smarter functions. This pr also keeps the legacy way to keep backwards compatibility if only one arg is passed, and throws an error if more than 2 args are passed.
A test case is written to pass two args to our function and make sure it passes/runs to success.
Make sure to mark the boxes below before creating PR: [x]
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.
Read the Pull Request Guidelines for more information.