Cly*_*row 7 apache slack airflow
有人可以给我逐步指南,介绍如何将Apache Airflow连接到Slack工作区。我为频道创建了webhook,接下来该怎么做?
亲切的问候
kax*_*xil 11
SlackAPIPostOperatorDAG中的运算符SlackAPIPostOperator(
task_id='failure',
token='YOUR_TOKEN',
text=text_message,
channel=SLACK_CHANNEL,
username=SLACK_USER)
Run Code Online (Sandbox Code Playgroud)
上面是使用Airflow向Slack发送消息的最简单方法。
但是,如果要配置Airflow在任务失败时向Slack发送消息,请创建一个函数,并on_failure_callback使用创建的slack函数的名称添加到您的任务中。下面是一个示例:
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)
Run Code Online (Sandbox Code Playgroud)
使用SlackWebHook(仅适用于气流> = 1.10.0的情况):如果要以类似方式使用SlackWebHookuse SlackWebhookOperator:
尝试新的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)
Run Code Online (Sandbox Code Playgroud)
注意:确保您已将slack_connectionAirflow 连接添加为
host=https://hooks.slack.com/services/
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
正如@Deep Nirmal 注意:确保您在 Airflow 连接中添加了 slack_connection 作为
host=https://hooks.slack.com/services/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5434 次 |
| 最近记录: |