Files
airflow/dags/office/dag_office_rent.py
T
2026-07-22 15:42:34 +08:00

59 lines
1.8 KiB
Python

"""
小区周边配套DAG
"""
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator, BranchPythonOperator
from office.rent.base_price import calc
import pendulum
from airflow import DAG
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
)
# 任务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=calc,
provide_context=True
)
branch_task >> [t1, t2]