init: airflow_demo 初始化示例 DAG 集合

This commit is contained in:
2026-10-13 14:14:04 +08:00
commit 69a1df7aa0
12 changed files with 1112 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
from datetime import datetime, timedelta
# 导入 Airflow 所需的类
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.bash import BashOperator
# 定义任务最终失败时的回调函数
def task_failure_callback(context):
# 获取当前任务实例
task_instance = context["task_instance"]
# 打印失败任务的名称和所属 DAG 名称
print(f"任务执行失败:{task_instance.task_id}")
print(f"DAG 名称:{task_instance.dag_id}")
# 定义任务的默认参数
default_args = {
"owner": "xiaozhang", # 任务负责人
"depends_on_past": False, # 不要求依赖上一次运行的结果
"email_on_failure": False, # 关闭邮件失败通知
"on_failure_callback": task_failure_callback, # 任务最终失败时调用
"retries": 1, # 失败后最多额外重试 1 次
"retry_delay": timedelta(minutes=5), # 重试前等待 5 分钟
}
# 定义一个普通的 Python 函数
def print_hello():
print("Hello, World!")
# 创建 DAG,也就是工作流
with DAG(
dag_id="hello_world2", # 改成新的 DAG ID
default_args=default_args,
description="这个是一个样例",
start_date=datetime(2024, 10, 1),
schedule_interval="@daily",
catchup=False,
tags=["demo", "xiaozhang"],
) as dag:
# 任务一:执行 Python 函数
task1 = PythonOperator(
task_id="print_hello", # 任务 ID
python_callable=print_hello, # 传入函数本身,不加括号
)
# 任务二:执行 Bash 命令
task2 = BashOperator(
task_id="run_bash_command", # 任务 ID
bash_command='echo "This is a Bash command"', # 要执行的命令
)
# 设置依赖:task1 成功后,task2 才会执行
task1 >> task2