60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
"""
|
|
小区周边配套DAG
|
|
"""
|
|
from airflow.providers.standard.operators.bash import BashOperator
|
|
from airflow.operators.python import PythonOperator, BranchPythonOperator
|
|
import pendulum
|
|
from airflow import DAG
|
|
|
|
|
|
def run_base_rent_calc(**kwargs):
|
|
from office.rent.base_price import calc
|
|
return calc(**kwargs)
|
|
|
|
|
|
def choose_task(**context):
|
|
"""根据 dag_run.conf 决定执行哪个任务"""
|
|
conf = context['dag_run'].conf or {}
|
|
task_type = conf.get('task_type', 'both') # 默认执行两个任务
|
|
|
|
if task_type == 'city_region':
|
|
# 只执行城市区域租金计算
|
|
return 'office_city_region_rent_calc'
|
|
elif task_type == 'office':
|
|
# 只执行基础租金计算
|
|
return 'office_base_rent_calc'
|
|
elif task_type == 'both':
|
|
# 执行两个任务
|
|
return ['office_city_region_rent_calc', 'office_base_rent_calc']
|
|
else:
|
|
# 默认执行两个任务
|
|
return ['office_city_region_rent_calc', 'office_base_rent_calc']
|
|
|
|
|
|
with DAG(
|
|
dag_id="office_rent_dag",
|
|
# 每月25日1点运行
|
|
schedule="0 3 25 * *",
|
|
start_date=pendulum.datetime(2025, 10, 28, tz="Asia/Shanghai"),
|
|
catchup=True,
|
|
description="写字楼租金价格计算",
|
|
tags=["office", "rent"],
|
|
) as dag:
|
|
# 分支任务:根据 conf 决定执行路径
|
|
branch_task = BranchPythonOperator(
|
|
task_id='choose_branch',
|
|
python_callable=choose_task,
|
|
# 🛡️ 已移除 provide_context=True,防止 Airflow 3.3.0 报错崩溃
|
|
)
|
|
# 任务1:城市区域租金计算
|
|
t1 = BashOperator(
|
|
task_id='office_city_region_rent_calc',
|
|
bash_command='python /opt/airflow/dags/repo/dags/office/rent/price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}} ',
|
|
)
|
|
# 任务2:写字楼租金计算
|
|
t2 = PythonOperator(
|
|
task_id='office_base_rent_calc',
|
|
python_callable=run_base_rent_calc,
|
|
# 🛡️ 已移除 provide_context=True,防止 Airflow 3.3.0 报错崩溃
|
|
)
|
|
branch_task >> [t1, t2] |