成功或失败时运行任务,但跳过时不运行 [英] Run Task on Success or Fail but not on Skipped
问题描述
如果上游任务成功或失败,是否有办法运行任务,但是如果上游任务被跳过,则无法运行?
Is there a way to run a task if the upstream task succeeded or failed but not if the upstream was skipped?
我熟悉 trigger_rule
和 all_done
参数, (在另一个问题中提到),但是当跳过上游时会触发任务。我只希望任务在上游任务的成功或失败时触发。
I am familiar with trigger_rule
with the all_done
parameter, as mentioned in this other question, but that triggers the task when the upstream has been skipped. I only want the task to fire on the success or failure of the upstream task.
推荐答案
我不相信触发成功和失败的规则。您可以做的是设置重复任务,一项任务使用触发规则 all_success
,另一项任务使用触发规则 all_failed
。这样,仅当重复任务之前的父项失败/成功时才触发重复任务。
I don't believe there is a trigger rule for success and failed. What you could do is set up duplicate tasks, one with the trigger rule all_success
and one with the trigger rule all_failed
. That way, the duplicate task is only triggered if the parents ahead of it fails / succeeds.
我提供了以下代码供您轻松测试预期结果。
I have included code below for you to test for expected results easily.
因此,假设您有三个任务。
So, say you have three tasks.
- task1是您的成功/失败
- task2是您成功的唯一任务
-
task3仅是您的失败
- task1 is your success / fail
- task2 is your success only task
task3 is your failure only
#dags/latest_only_with_trigger.py
import datetime as dt
from airflow.models import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.utils.trigger_rule import TriggerRule
dag = DAG(
dag_id='stackoverflowtest',
schedule_interval=dt.timedelta(minutes=5),
start_date=dt.datetime(2019, 2, 20)
)
task1 = DummyOperator(task_id='task1', dag=dag)
task2 = DummyOperator(task_id='task2', dag=dag,
trigger_rule=TriggerRule.all_success)
task3 = DummyOperator(task_id='task3', dag=dag
trigger_rule=TriggerRule.all_failed)
###### ORCHESTRATION ###
task2.set_upstream(task1)
task3.set_upstream(task1)
何pe这有帮助!
这篇关于成功或失败时运行任务,但跳过时不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!