Skip to main content
 首页 » 编程设计

python之当下游任务定义依赖于上游结果时如何设置 DAG

2024年09月07日8www_RR

我的问题是关于动态定义 group of parallel tasks 的 DAG基于计算上游任务删除和重建的 MySQL 表中的行数。我遇到的困难是,在我的上游任务中,我TRUNCATE 该表以在再次重建之前清除它。这是 sherlock_join_and_export_task。当我这样做时,行数下降到零,并且我的动态生成的任务不再被定义。当表恢复时,图形的结构也恢复了,但任务不再执行。相反,它们在 TreeView 中显示为黑框:

这是在 sherlock_join_and_export_task 删除 count = worker.count_online_table() 行中引用的表之后的 DAG:

sherlock_join_and_export_task 完成后,DAG 看起来像这样:

不过,没有任何任务被排队和执行。 DAG 一直在运行,什么也没有发生。

在这种情况下我会使用子 DAG 吗?关于如何设置或重写现有 DAG 的任何见解?我使用 LocalExecutor 在 AWS ECS 上运行它。以下代码供引用:

from datetime import datetime 
import os 
import sys 
 
from airflow.models import DAG 
from airflow.operators.python_operator import PythonOperator 
 
BATCH_SIZE = 75000 
 
from preprocessing.marketing.minimalist.table_builder import OnlineOfflinePreprocess 
 
worker = OnlineOfflinePreprocess() 
 
def partial_process_flow(batch_size, offset): 
    worker = OnlineOfflinePreprocess() 
    worker.import_offline_data() 
    worker.import_online_data(batch_size, offset) 
    worker.merge_aurum_to_sherlock() 
    worker.upload_table('aurum_to_sherlock') 
 
def batch_worker(batch_size, offset, DAG): 
    return PythonOperator( 
        task_id="{0}_{1}".format(offset, batch_size), 
        python_callable=partial_process_flow, 
        op_args=[batch_size, offset], 
        dag=DAG) 
 
DAG = DAG( 
  dag_id='minimalist_data_preproc', 
  start_date=datetime(2018, 1, 7, 2, 0, 0, 0), #..EC2 time. Equal to 11pm hora México 
  max_active_runs=1, 
  concurrency=4, 
  schedule_interval='0 9 * * *', #..4am hora mexico 
  catchup=False 
) 
 
clear_table_task = PythonOperator( 
    task_id='clear_table_task', 
    python_callable=worker.clear_marketing_table, 
    op_args=['aurum_to_sherlock'], 
    dag=DAG 
) 
 
sherlock_join_and_export_task = PythonOperator( 
    task_id='sherlock_join_and_export_task', 
    python_callable=worker.join_online_and_send_to_galileo, 
    dag=DAG 
) 
 
sherlock_join_and_export_task >> clear_table_task 
 
count = worker.count_online_table() 
if count == 0: 
    sherlock_join_and_export_task >> batch_worker(-99, -99, DAG) #..dummy task for when left join deleted 
else: 
    format_table_task = PythonOperator( 
        task_id='format_table_task', 
        python_callable=worker.format_final_table, 
        dag=DAG 
    ) 
 
    build_attributions_task = PythonOperator( 
        task_id='build_attributions_task', 
        python_callable=worker.build_attribution_weightings, 
        dag=DAG 
    ) 
 
    update_attributions_task = PythonOperator( 
        task_id='update_attributions_task', 
        python_callable=worker.update_attributions, 
        dag=DAG 
    ) 
 
    first_task = batch_worker(BATCH_SIZE, 0, DAG) 
    clear_table_task >> first_task 
    for offset in range(BATCH_SIZE, count, BATCH_SIZE): 
        first_task >> batch_worker(BATCH_SIZE, offset, DAG) >> format_table_task 
 
    format_table_task >> build_attributions_task >> update_attributions_task 

这是 DAG 正在做的事情的简化概念:

... 
 
def batch_worker(batch_size, offset, DAG): 
    #..A function the dynamically generates tasks based on counting the reference table 
    return dag_task 
 
worker = ClassMethodsForDAG() 
count = worker.method_that_counts_reference table() 
 
if count == 0: 
    delete_and_rebuild_reference_table_task >> batch_worker(-99, -99, DAG)  
else: 
    first_task = batch_worker(BATCH_SIZE, 0, DAG) 
    clear_table_task >> first_task 
    for offset in range(BATCH_SIZE, count, BATCH_SIZE): 
        first_task >> batch_worker(BATCH_SIZE, offset, DAG) >> downstream_task 

请您参考如下方法:

查看您的 dag,我认为您已经实现了一个非幂等流程, Airflow 并未真正针对该流程进行配置。不要截断/更新您正在构建的表,您可能应该保留配置的任务并仅更新 start_date/end_date 以启用和禁用它们以在任务级别进行调度,或者甚至在每次迭代和如果作业被禁用,您的脚本会检查表以运行 hello world。