From 37ecd3095d1574ed0d656d6c070634608ad3d0e6 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:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=94=AE=E4=BB=B7=E5=AF=B9ap?= =?UTF-8?q?i=E7=9A=84=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_sell.py | 86 +++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/dags/office/dag_office_sell.py b/dags/office/dag_office_sell.py index bd8f88f..89d7edd 100644 --- a/dags/office/dag_office_sell.py +++ b/dags/office/dag_office_sell.py @@ -1,37 +1,101 @@ from airflow.operators.bash import BashOperator -from airflow.operators.python import PythonOperator +from airflow.operators.python import PythonOperator, BranchPythonOperator +from airflow.operators.empty import EmptyOperator from office.sell.base_price import calc from office.sell.fill_base_price import fill_base_price 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 == 'office': + # 只执行写字楼基础价格计算 (t1 -> t2) + return 'office_sell_base_price_calc' + elif task_type == 'city_region': + # 只执行城市区域价格计算 (t3 -> t4) + return 'office_city_region_sell_base_price_calc' + else: + # both 或其他情况: 执行所有任务 (t1 -> t2 -> t3 -> t4) + return 'office_sell_base_price_calc' + + +def choose_after_office(**context): + """t2 执行完后判断是否继续到 t3""" + conf = context['dag_run'].conf or {} + task_type = conf.get('task_type', 'both') + + if task_type == 'both': + # both 模式继续执行 t3 + return 'office_city_region_sell_base_price_calc' + else: + # office 模式结束,不执行 t3 + return 'skip_city_region' + + with DAG( - dag_id="office_sell_dag", - # 每月24日1点运行 - schedule_interval="0 3 25 * *", - start_date=pendulum.datetime(2025, 10, 28, tz="Asia/Shanghai"), - catchup=True, - description="写字楼出售价格计算", - tags=["office", "sell"], + dag_id="office_sell_dag", + # 每月25日3点运行 + schedule_interval="0 3 25 * *", + start_date=pendulum.datetime(2025, 1, 1, tz="Asia/Shanghai"), + catchup=False, + description="写字楼出售价格计算", + tags=["office", "sell"], ) as dag: + # 第一个分支任务:选择起始任务 + branch_task = BranchPythonOperator( + task_id='choose_branch', + python_callable=choose_task, + provide_context=True + ) + + # 任务1: 写字楼出售基础价格计算 t1 = PythonOperator( task_id='office_sell_base_price_calc', python_callable=calc, provide_context=True ) + + # 任务2: 无样本写字楼出售基础价格填充 t2 = PythonOperator( task_id='office_no_sample_sell_base_price_calc', python_callable=fill_base_price, provide_context=True ) + + # 第二个分支任务:t2 后判断是否继续 + branch_after_office = BranchPythonOperator( + task_id='choose_after_office', + python_callable=choose_after_office, + provide_context=True + ) + + # 跳过标记(office 模式结束点) + skip_city_region = EmptyOperator( + task_id='skip_city_region' + ) + + # 任务3: 城市区域写字楼出售价格计算 t3 = BashOperator( task_id='office_city_region_sell_base_price_calc', - bash_command='python /opt/airflow/dags/repo/dags/office/sell/price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}} ', + bash_command='python /opt/airflow/dags/repo/dags/office/sell/price.py {{ dag_run.conf.get("yearmonth", "") }} ', ) + + # 任务4: 城市区域无样本写字楼出售价格填充 t4 = BashOperator( task_id='office_city_region_sell_no_sample_base_price_calc', - bash_command='python /opt/airflow/dags/repo/dags/office/sell/fill_price.py {{dag_run.conf["yearmonth"] if "yearmonth" in dag_run.conf}}', + bash_command='python /opt/airflow/dags/repo/dags/office/sell/fill_price.py {{ dag_run.conf.get("yearmonth", "") }}', ) - t1 >> t2 >> t3 >> t4 + # 定义任务依赖关系 + # office 路径: branch -> t1 -> t2 -> branch_after_office -> skip_city_region + # city_region 路径: branch -> t3 -> t4 + # both 路径: branch -> t1 -> t2 -> branch_after_office -> t3 -> t4 + + branch_task >> [t1, t3] + t1 >> t2 >> branch_after_office >> [t3, skip_city_region] + t3 >> t4 \ No newline at end of file