From 80d960dd4cdea1f7edb037bdcf6838c6a4f32e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=89=B3?= Date: Tue, 11 Nov 2025 17:08:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0API=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/office/dag_office_rent.py | 35 ++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) 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]