36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
# airflow_local_settings.py
|
|
import json
|
|
from airflow.operators.http_operator import SimpleHttpOperator
|
|
|
|
|
|
def send_dingding_notification(context):
|
|
task_instance = context.get('task_instance')
|
|
|
|
message = {
|
|
"msgtype": "markdown",
|
|
"markdown": {
|
|
"title": "Airflow任务错误告警",
|
|
"text": f"""### ⚠️ Airflow任务错误告警 ⚠️
|
|
|
|
**DAG**: {task_instance.dag_id}
|
|
**任务**: {task_instance.task_id}
|
|
**执行时间**: {context.get('execution_date')}
|
|
**重试次数**: {task_instance.try_number - 1}/{task_instance.max_tries}
|
|
**日志链接**: [{task_instance.log_url}]({task_instance.log_url})
|
|
"""
|
|
},
|
|
"at": {
|
|
"isAtAll": True
|
|
}
|
|
}
|
|
|
|
alert = SimpleHttpOperator(
|
|
task_id='dingding_alert',
|
|
http_conn_id='dingding_webhook',
|
|
endpoint='robot/send',
|
|
method='POST',
|
|
data=json.dumps(message),
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
alert.execute(context)
|