38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
from airflow import DAG
|
|
from airflow.operators.python_operator import PythonOperator
|
|
from datetime import datetime
|
|
from goojia_utils import dingutils
|
|
|
|
|
|
|
|
def send_dingding_alert(context):
|
|
print('监测到错误')
|
|
task_instance = context.get('task_instance')
|
|
url = task_instance.log_url.replace("http://localhost:8080", "https://airflow.fangjia.com")
|
|
message = f"【airflow】 任务{task_instance.task_id}错误,请点击{url}查看!"
|
|
print(message)
|
|
dingutils.send_text(message, "ae14f50045e9375c354940a2afb86f14e453bd1f08f78da0ba6e788d35bccec4")
|
|
print("success")
|
|
|
|
def fail_task():
|
|
raise ValueError("This task is designed to fail")
|
|
|
|
default_args = {
|
|
"owner": "airflow",
|
|
"retries": 0,
|
|
"on_failure_callback": send_dingding_alert, # 任务失败时触发钉钉告警
|
|
}
|
|
|
|
|
|
dag = DAG(
|
|
'test_failure_notification',
|
|
default_args=default_args,
|
|
start_date=datetime(2025, 4, 22),
|
|
schedule_interval=None
|
|
)
|
|
|
|
task = PythonOperator(
|
|
task_id='failing_task',
|
|
python_callable=fail_task,
|
|
dag=dag
|
|
) |