Airflow failed slack message

前端 未结 4 918

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

    The BaseOperator supports 'on_failure_callback' parameter:

    on_failure_callback (callable) – a function to be called when a task instance of this task fails. a context dictionary is passed as a single parameter to this function. Context contains references to related objects to the task instance and is documented under the macros section of the API.

    I have not tested this but you should be able to define a function which posts to slack on failure and pass it to each task definition. To get the name of the current task, you can use the {{ task_id }} template.

    0 讨论(0)
  • 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
    )
    
    0 讨论(0)
  • 2021-01-03 11:20

    Maybe this example will be helpful:

    def slack_failed_task(contextDictionary, **kwargs):  
           failed_alert = SlackAPIPostOperator(
             task_id='slack_failed',
             channel="#datalabs",
             token="...",
             text = ':red_circle: DAG Failed',
             owner = '_owner',)
             return failed_alert.execute
    
    
    task_with_failed_slack_alerts = PythonOperator(
    task_id='task0',
    python_callable=<file to execute>,
    on_failure_callback=slack_failed_task,
    provide_context=True,
    dag=dag)
    
    0 讨论(0)
  • 2021-01-03 11:36

    Try the new SlackWebhookOperator which is there in Airflow version>=1.10.0

    from airflow.contrib.operators.slack_webhook_operator import SlackWebhookOperator
    
    slack_msg="Hi Wssup?"
    
    slack_test =  SlackWebhookOperator(
        task_id='slack_test',
        http_conn_id='slack_connection',
        webhook_token='/1234/abcd',
        message=slack_msg,
        channel='#airflow_updates',
        username='airflow_'+os.environ['ENVIRONMENT'],
        icon_emoji=None,
        link_names=False,
        dag=dag)
    

    Note: Make sure you have slack_connection added in your Airflow connections as

    host=https://hooks.slack.com/services/
    
    0 讨论(0)
提交回复
热议问题