This commit is contained in:
沈健
2025-04-23 15:41:41 +08:00
parent 66bb733a0b
commit f8c27a33a2
+3 -16
View File
@@ -8,26 +8,13 @@ import json
def send_dingding_alert(context):
print('监测到错误')
task_instance = context.get('task_instance')
execution_date = context.get('execution_date').strftime("%Y-%m-%d %H:%M:%S") if context.get(
'execution_date') else "N/A"
message = {
"msgtype": "markdown",
"markdown": {
"title": "Airflow 任务错误告警",
"text": f"""### ⚠️ Airflow 任务失败告警 ⚠️
**DAG**: {task_instance.dag_id}
**任务**: {task_instance.task_id}
**执行时间**: {execution_date}
**日志链接**: [点击查看]({task_instance.log_url})"""
},
"at": {"isAtAll": True}
}
url = task_instance.log_url.replace("http://localhost:8080", "https://airflow.fangjia.com")
message = f"【airflow错误】任务{task_instance.task_id}执行失败,请点击{url}查看!"
print(message)
http_hook = HttpHook(method="POST", http_conn_id="dingding_webhook")
http_hook.run(
endpoint="robot/send",
data=json.dumps(message),
data=message,
headers={"Content-Type": "application/json"}
)
print("success")