3

I want to call two different functions for dag failure and success. For that i want to use on_failure_callback and on_success_callback in DAG() function.

As per my requirement, this callbacks should be on dag level and not task level. That's why i am writing this callbacks insiled DAG() functions while declaring dag variable.

but this callback function are not being called. Same function if i call on task level , then working fine.

This is my code:

def success():
      print("successful")
    
dag = DAG(dag_id='callback_test',schedule_interval=None,default_args=default_args,on_success_callback=success)
    
    def fun1(**kwargs):
        print("function called")
    
    task1 = PythonOperator(
        task_id='task1',
        provide_context=True,
        python_callable=fun1,
        dag=dag
        )
    
    task1
Joseph D
  • 189
  • 1
  • 12

2 Answers2

0

However, I think it should also work at DAG level, according to this: https://airflow.apache.org/docs/apache-airflow/1.10.10/_api/airflow/models/dag/index.html?highlight=on_failure_callback

right?

I am not being able to make it work, though :(

vrivesmolina
  • 21
  • 2
  • 4
  • if not working please report on https://github.com/apache/airflow/issues with reproduce example. Preferably please test against latest Airflow version 1.10.10 is old version – Elad Kalif Jan 28 '21 at 17:18
  • 1
    I made it work. Be careful when doing: on_failure_callback=myfunction and do *not* include any parameters in the function, the params are those included in the execute context – vrivesmolina Jan 29 '21 at 18:08
0

If you want to pass agrument, you can do it by creating higher order function just like crearing decorator returning actual callback function by outer function.

Anuj Subedi
  • 42
  • 1
  • 8