Airflow failed slack message

前端 未结 4 957

How can I configure Airflow so that any failure in the DAG will (immediately) result in a slack message?

At this moment I manage it by creating a slack_failed_task:<

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 11:16

    I would prefer to add the callback to the DAG and to be inhered by all its tasks:

    def on_failure_callback(context):
        webhook_url = os.getenv('SLACK_WEBHOOK_TOKEN')
        slack_data = {
            'text': "@here DAG {} Failed".format(context['dag'].dag_id)
        }
    
        response = requests.post(
            webhook_url, data=json.dumps(slack_data),
            headers={'Content-Type': 'application/json'}
        )
    
    dag = DAG(
        dag_id='dag_with_templated_dir',
        start_date=datetime(2020, 1, 1),
        on_failure_callback=on_failure_callback
    )
    

提交回复
热议问题