Maayan
Maayan

Reputation: 303

How to get the DAG chain execution time in Airflow?

Lets say I have two DAG, where dag2 executed dag1 as part of it's flow using TriggerDagRunOperator as follows:

Now lets say dag2 is scheduled for once a day at 5PM. Is there a way for me to get the execution timestamp for dag2 (the parent DAG) while I'm running dag1? Is there any built-in parameter that holds that value?

And if something happened and dag2 was triggered later than usual, lets say 6PM same day, then I still want to get the original scheduling time - that is 5PM while I'm in dag1.

Upvotes: 0

Views: 1160

Answers (1)

joebeeson
joebeeson

Reputation: 4366

Pass a function to the python_callable argument of TriggerDagRunOperator that injects the execution_date into the triggered DAG:

def inject_execution_date(context, dag_run_obj):
  dag_run_obj.payload = {"parent_execution_date": context["execution_date"]}
  return dag_run_obj

[...]

trigger_dro = TriggerDagRunOperator(python_callable=inject_execution_date, [...])

You can access this in the child DAG with context["conf"]["parent_execution_date"]

Upvotes: 1

Related Questions