How to mark an Airflow DAG run as failed if any task fails?

后端 未结 2 1517
梦毁少年i
梦毁少年i 2021-02-19 15:48

Is it possible to make an Airflow DAG fail if any task fails?

I usually have some cleaning up tasks at the end of a DAG and as it is now, whenever the last task succeeds

2条回答
  •  走了就别回头了
    2021-02-19 16:13

    Another solution can be to add a final PythonOperator that checks the status of all tasks in this run:

    final_status = PythonOperator(
        task_id='final_status',
        provide_context=True,
        python_callable=final_status,
        trigger_rule=TriggerRule.ALL_DONE, # Ensures this task runs even if upstream fails
        dag=dag,
    )
    
    def final_status(**kwargs):
        for task_instance in kwargs['dag_run'].get_task_instances():
            if task_instance.current_state() != State.SUCCESS and \
                    task_instance.task_id != kwargs['task_instance'].task_id:
                raise Exception("Task {} failed. Failing this DAG run".format(task_instance.task_id))
    

提交回复
热议问题