diff --git a/dags/office/dag_office_rent.py b/dags/office/dag_office_rent.py index 7fb27cb..c04edcb 100644 --- a/dags/office/dag_office_rent.py +++ b/dags/office/dag_office_rent.py @@ -2,13 +2,32 @@ 小区周边配套DAG """ from airflow.operators.bash import BashOperator -from airflow.operators.python import PythonOperator +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点运行 @@ -18,14 +37,22 @@ with DAG( 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 ) - t1 >> t2 + branch_task >> [t1, t2]