添加API启动支持
This commit is contained in:
@@ -2,13 +2,32 @@
|
|||||||
小区周边配套DAG
|
小区周边配套DAG
|
||||||
"""
|
"""
|
||||||
from airflow.operators.bash import BashOperator
|
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
|
from office.rent.base_price import calc
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
|
|
||||||
from airflow import DAG
|
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(
|
with DAG(
|
||||||
dag_id="office_rent_dag",
|
dag_id="office_rent_dag",
|
||||||
# 每月25日1点运行
|
# 每月25日1点运行
|
||||||
@@ -18,14 +37,22 @@ with DAG(
|
|||||||
description="写字楼租金价格计算",
|
description="写字楼租金价格计算",
|
||||||
tags=["office", "rent"],
|
tags=["office", "rent"],
|
||||||
) as dag:
|
) as dag:
|
||||||
|
# 分支任务:根据 conf 决定执行路径
|
||||||
|
branch_task = BranchPythonOperator(
|
||||||
|
task_id='choose_branch',
|
||||||
|
python_callable=choose_task,
|
||||||
|
provide_context=True
|
||||||
|
)
|
||||||
|
# 任务1:城市区域租金计算
|
||||||
t1 = BashOperator(
|
t1 = BashOperator(
|
||||||
task_id='office_city_region_rent_calc',
|
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}} ',
|
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(
|
t2 = PythonOperator(
|
||||||
task_id='office_base_rent_calc',
|
task_id='office_base_rent_calc',
|
||||||
python_callable=calc,
|
python_callable=calc,
|
||||||
provide_context=True
|
provide_context=True
|
||||||
)
|
)
|
||||||
t1 >> t2
|
branch_task >> [t1, t2]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user