气流在首次打开时启动两次DAG运行

时间:2019-10-25 17:43:06

标签: airflow

当我在10月25日17:23左右首次启动Airflow Web服务器和调度程序并打开DAG时,我可以看到它在10月23日和10月24日开始了两次运行:

RUN 1 -> 10-23T17:23
RUN 2 -> 10-24T17:23

这是我的DAG配置:

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': '2019-01-01',
    'retries': 0,
}
dag = DAG(
    'my_script',
    default_args=default_args,
    schedule_interval=datetime.timedelta(days=1),
    catchup=False,
)

由于已经过去了start_date + schedule_interval并且我已经设置了catchup=False,所以我希望它可以立即启动一次DAG运行,但是我不希望它运行两个。

  • 为什么要执行两次DAG运行?
  • 如何防止这种行为?

1 个答案:

答案 0 :(得分:2)

我不确定,但这是我最好的猜测-

简而言之,可能是气流的建立方式,而解决方法是将start_date修改为昨天。

TL; DR

我同意,打开电源时在10到24天内开始1 dag听起来会更自然。

但是,根据您的dag运行,RUN 1是10-23。 这向我表明,第一次运行的初始化是不正确的,我已经研究了调度程序代码。

我对此有疑问。

https://github.com/apache/airflow/blob/68b8ec5f415795e4fa4ff7df35a3e75c712a7bad/airflow/jobs/scheduler_job.py#L603

这是一个函数,它创建一个dag运行并设置运行的开始日期。

# The logic is that we move start_date up until
# one period before, so that timezone.utcnow() is AFTER
# the period end, and the job can be created...
now = timezone.utcnow()

# This returns current time + schedule_interval. In your example, this will be tomorrow.
next_start = dag.following_schedule(now)

# This returns current time - schedule_interval. In your example, this will be yesterday.
last_start = dag.previous_schedule(now)

# tomorrow <= today should return False 
if next_start <= now:
    new_start = last_start
else:
    # and this will return last_start - schedule_interval which means 2 days ago.  
    # wondering if this is intended to be dag.previous_schedule(next_start)???
    new_start = dag.previous_schedule(last_start)