【问题标题】:clear an upstream task in airflow within the dag清除 dag 中气流的上游任务
【发布时间】:2017-12-17 08:08:59
【问题描述】:

我在气流 DAG 中有一个任务。它有三个子任务。不幸的是,在某些情况下这个父任务会成功,但三个子任务中有两个会失败(并且对子任务的重试不会修复它们)。

它要求父母重试(即使它没有失败)。

所以我尽职尽责地进入 dag 运行的图形视图并“清除”这个父任务和所有下游任务(+递归)。

有没有办法我可以在 dag 本身内做到这一点?

【问题讨论】:

    标签: parent-child airflow directed-acyclic-graphs apache-airflow


    【解决方案1】:

    如果您的任务是 subdag 的一部分,则在 on_retry_callback 的 on_retry_callback 中调用 dag.clear() 应该可以解决问题:

    SubDagOperator(
        subdag=subdag,
        task_id="...",
        on_retry_callback=lambda context: subdag.clear(
            start_date=context['execution_date'],
            end_date=context['execution_date']),
        dag=dag
    )
    

    【讨论】:

      【解决方案2】:

      我们有一个类似的问题,我们通过将具有我们想要重复的依赖项的任务放入一个子 dag 来解决这个问题。然后当 sub dag 重试时,我们使用 on_retry_callback 清除 sub dag 任务状态,以便它们都再次运行。

      sub_dag = SubDagOperator(
          retry_delay=timedelta(seconds=30),
          subdag=create_sub_dag(),
          on_retry_callback=callback_subdag_clear,
          task_id=sub_dag_name,
          dag=dag,
      )
      
      def callback_subdag_clear(context):
          """Clears a sub-dag's tasks on retry."""
          dag_id = "{}.{}".format(
              context['dag'].dag_id,
              context['ti'].task_id,
          )
          execution_date = context['execution_date']
          sub_dag = DagBag().get_dag(dag_id)
          sub_dag.clear(
              start_date=execution_date,
              end_date=execution_date,
              only_failed=False,
              only_running=False,
              confirm_prompt=False,
              include_subdags=False
          )
      

      (原文取自这里https://gist.github.com/nathairtras/6ce0b0294be8c27d672e2ad52e8f2117)

      【讨论】:

        【解决方案3】:

        它不会直接回答您的问题,但我可以建议一个更好的解决方法:

        default_args = {
            'start_date': datetime(2017, 12, 16),
            'depends_on_past': True,
        }
        
        dag = DAG(
            dag_id='main_dag',
            schedule_interval='@daily',
            default_args=default_args,
            max_active_runs=1,
            retries=100,
            retry_delay= timedelta(seconds=120)
        )
        

        在 DAG 中将 depends_on_past 设置为 True。

        然后在这个dag的任务中,使用retries限制重试次数

          DummyOperator(
                task_id='bar',
                retries=0
                dag=child)
        

        这样,当任何任务失败时,DAG 都会被标记为失败。然后将重试 DAG。

        【讨论】:

        • 据我所知这是行不通的。根据文档,depends_on_past 会阻塞后续的任务请求,直到当前任务完成。
        【解决方案4】:

        我们选择使用taskinstance 的clear_task_instances 方法:

        @provide_session
        def clear_tasks_fn(tis,session=None,activate_dag_runs=False,dag=None) -> None:
             """
             Wrapper for `clear_task_instances` to be used in callback function
             (that accepts only `context`)
            """
        
            taskinstance.clear_task_instances(tis=tis,
                                              session=session,
                                              activate_dag_runs=activate_dag_runs,
                                              dag=dag)
        
        def clear_tasks_callback(context) -> None:
            """
            Clears tasks based on list passed as `task_ids_to_clear` parameter
        
            To be used as `on_retry_callback`
            """
        
            all_tasks = context["dag_run"].get_task_instances()
            dag = context["dag"]
            task_ids_to_clear = context["params"].get("task_ids_to_clear", [])
        
            tasks_to_clear = [ ti for ti in all_tasks if ti.task_id in task_ids_to_clear ]
        
            clear_tasks_fn(tasks_to_clear,
                       dag=dag)
        

        您需要提供要在回调中清除的任务列表,例如任何子任务:

        DummyOperator('some_child',
                      on_retry_callback=clear_tasks_callback,
                      params=dict(
                          task_ids_to_clear=['some_child', 'parent']
                      ),
                      retries=1
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-07
          • 2017-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-28
          • 1970-01-01
          相关资源
          最近更新 更多