如何将Apache Airflow与Slack集成在一起?

时间:2018-08-28 09:25:06

标签: apache airflow slack

有人可以给我逐步的手册,介绍如何将Apache Airflow连接到Slack工作区。 我在频道上创建了webhook,接下来该怎么做?

亲切的问候

3 个答案:

答案 0 :(得分:5)

SlackAPIPostOperator(
      task_id='failure',
      token='YOUR_TOKEN',
      text=text_message,
      channel=SLACK_CHANNEL,
      username=SLACK_USER)

以上是使用Airflow向Slack发送消息的最简单方法。

但是,如果您要配置Airflow以在任务失败时向Slack发送消息,请创建一个函数,并使用创建的slack函数的名称将on_failure_callback添加到您的任务中。下面是一个示例:

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)

使用SlackWebHook (仅适用于Airflow> = 1.10.0): 如果要使用SlackWebHook,请以类似方式使用SlackWebhookOperator

https://github.com/apache/incubator-airflow/blob/master/airflow/contrib/operators/slack_webhook_operator.py#L25

答案 1 :(得分:1)

尝试使用新的 SlackWebhookOperator (在Airflow版本> = 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)

注意:确保在气流连接中添加了slack_connection

host=https://hooks.slack.com/services/

答案 2 :(得分:0)

使用SlackWebhookOperator的完整示例,如@kaxil答案:

def slack_failed_task(task_name):
  failed_alert = SlackWebhookOperator(
    task_id='slack_failed_alert',
    http_conn_id='slack_connection',
    webhook_token=Variable.get("slackWebhookToken", default_var=""),
    message='@here DAG Failed {}'.format(task_name),
    channel='#epm-marketing-dev',
    username='Airflow_{}'.format(ENVIRONMENT_SUFFIX),
    icon_emoji=':red_circle:',
    link_names=True,
  )
  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)

为@Deep Nirmal注意:确保在气流连接中添加了slack_connection,

host=https://hooks.slack.com/services/