Repository navigation
Callbacks intricacy #30329
|
Hi All! I am writing regarding callback features. I've been using AWS MWAA Airflow v.2.2.2 - this is the latest version as of now. According to the documentation https://airflow.apache.org/docs/apache-airflow/2.2.2/logging-monitoring/callbacks.html. Callbacks can be defined on a task level or on a DAG level. I found out that the latter is a kind of shortcut only, it does not refer to a success/failure of the DAG, but instead the callbacks are cascaded to every task of the DAG. The problem with this approach is, that if a DAG has N tasks failed in a given dagrun, each task will yield on_failure_callback action, which results in the N actions. Similarly, if on_success_callback is defined on a DAG level (it means it is defined for every task), every succeeded task triggers a callback action. What I need is a feature that allows to trigger exclusively one callback action (any python code, not just sending emails) on every dagrun finished state, ie success or failure. So if a DAG succeeds i want to trigger exactly one success callback action, if a DAG fails i want to trigger exactly one failure callback action. I think that the above goal can only be achieved from a context of the very last one task - the task that has the latest end_time ( i hope there is only one such task, if not, please clarify). My humble request is if you could give me a piece of advice how to do that. I looked for the anwser all over, but with no success. I've been thinking it over and have ended with the below algorithm, that i think might be one of possible solutions. Could you please vet it and verify whether my understanding is right or recommend any better approach, please? Note:
Code: on_failure_callback_action(context): # the code shall work with on_success_callback_action as well
time.sleep(60) # assumption: 1 minute suffices for latency between last task state change and dag state change
the_dag_state = getDagState()
# the below eliminates all the task instances that have finished more than 1 minute before the Dag finishes.
# this shall get rid of the solid bunch of task instances that cannot be the last
if the_dag_state == 'RUNNING':
return False # do_nothing
FINISHED_STATES = ["SUCCESS", "FAILED", "SKIPPED", "UPSTREAM_FAILED"]
frame = inspect.currentframe()
function_name = frame.f_code.co_name
if the_current_task isLeaf():
if all_tasks_except_current_task in FINISHED_STATES:
if function_name == 'on_failure_callback_action':
# current task instance is the last
trigger_a_failure_action()
if function_name == 'on_success_callback_action':
if all_the_other_tasks == "SUCCESS":
trigger_a_success_action()
else:
trigger_a_failure_action()
return True
return False # do nothing, the current task instance cannot be the last one
# the_current_task has dependents
if all_tasks_except_current_task_and_its_dependents in FINISHED_STATES:
if function_name == 'on_failure_callback_action':
# current task instance is the last, its dependents will not be triggered at all (UPSTREAM_FAILED)
trigger_a_failure_action()
if function_name == 'on_success_callback_action':
# the current task instance cannot be the last
# one of the current task instance dependents will trigger a proper action
return False # do_nothing
else
# the current task instance cannot be the last
return False # do_nothing |
Replies: 1 comment
|
All, The below citation is only true for
Also it is explained here: #26760. Thx for that @josh-fell Hence for my use case I should not have used |
All,
I apologize I have made a fatal oversight.
The below citation is only true for
default_args:This https://airflow.apache.org/docs/apache-airflow/2.2.2/concepts/dags.html#default-arguments explains why the callbacks were cascaded to every of the tasks:
"Default Arguments
Often, many Operators inside a DAG need the same set of default arguments (such as their start_date). Rather than having to specify this individually for every Operator, you can instead pass default_args to the DAG when you create it, and it w…