Skip to content
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

Merged
merged 1 commit into from
May 22, 2020

Conversation

Acehaidrey
Copy link
Contributor

@Acehaidrey Acehaidrey commented May 4, 2020

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]

  • Description above provides context of the change
  • Unit tests coverage for changes (not needed for documentation changes)
  • Target Github ISSUE in description if exists
  • Commits follow "How to write a good git commit message"
  • Relevant documentation is updated including usage instructions.
  • I will engage committers as explained in Contribution Workflow Example.

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.

@Acehaidrey
Copy link
Contributor Author

@jhtimmins if you don't mind when you get a chance to look at some of this and share your thoughts as well

@jhtimmins
Copy link
Contributor

@Acehaidrey Happy to take a look!

@Acehaidrey
Copy link
Contributor Author

@jhtimmins thank you so much :) I know I've been taking a lot of your time these days so appreciate it!

@jhtimmins
Copy link
Contributor

@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:

  1. Current behavior w just execution_date value
  2. Proposed behavior w context variables
  3. Proposed behavior w empty context dictionary

Worth getting a few more pairs of eyes on this.

What do you think about this approach?

@dimberman dimberman self-requested a review May 5, 2020 01:31
Copy link
Contributor

@jhtimmins jhtimmins left a 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)

@dimberman
Copy link
Contributor

@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.

@mik-laj
Copy link
Member

mik-laj commented May 5, 2020

@Acehaidrey
Copy link
Contributor Author

@jhtimmins @dimberman thank you both for your feedback.
I would really like that as well but I'm sorry I'm not seeing how to do this exactly.

Say a user has an execution_date_fn be

def myfunc(dt):
    print(dt)

So they pass this to the operator. Now in the operator execute code that we are making the changes, in the

def _handle_execution_date_fn(self, execution_date=None, context={})

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.
Does that make sense? It could be obvious but I'm not seeing how to implement this so any help on this would be great.

@Acehaidrey
Copy link
Contributor Author

@jhtimmins @dimberman can you please comment when you have a minute?

@Acehaidrey
Copy link
Contributor Author

@jhtimmins sorry for pestering but would love to get these in and close out

@Acehaidrey
Copy link
Contributor Author

@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

@Acehaidrey
Copy link
Contributor Author

@jhtimmins @ashb or anyone you think can. add to this convo mind doing that so we can get this closed out too?

@Acehaidrey
Copy link
Contributor Author

@dimberman can you please review this again so we can get it completed. Thank you @ashb for looking over this and approving

@dimberman dimberman merged commit b055151 into apache:master May 22, 2020
@dimberman dimberman added this to the Airflow 1.10.11 milestone May 22, 2020
@dimberman
Copy link
Contributor

Hi @Acehaidrey apologies for the delay. I've merged the PR and will work to backport it on 1.10.11 :)

@Acehaidrey
Copy link
Contributor Author

No problem thank you @dimberman !

kaxil pushed a commit that referenced this pull request Jun 28, 2020
Co-authored-by: Ace Haidrey <[email protected]>
(cherry picked from commit b055151)
potiuk pushed a commit that referenced this pull request Jun 29, 2020
Co-authored-by: Ace Haidrey <[email protected]>
(cherry picked from commit b055151)
@kaxil kaxil added the type:improvement Changelog: Improvements label Jul 1, 2020
kaxil pushed a commit that referenced this pull request Jul 1, 2020
Co-authored-by: Ace Haidrey <[email protected]>
(cherry picked from commit b055151)
elif num_fxn_params == 2:
return self.execution_date_fn(context['execution_date'], context)
else:
raise AirflowException(
Copy link
Contributor

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: ...,
        )

cfei18 pushed a commit to cfei18/incubator-airflow that referenced this pull request Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:improvement Changelog: Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants