为何Airflow DAG触发日期与执行日期不一致?执行日期为前一日
Airflow Execution Date 对齐问题解答
问题原因
Airflow的execution_date(Airflow 2.2+中又称logical_date)核心设计是代表任务处理的数据时间段的起始时间,而非任务实际运行的时间。对于你设置的schedule_interval='0 15 * * *',默认逻辑是每天15点处理前一天0点到当天0点的数据,因此触发日期为2月16日15点时,execution_date会设为2月15日。
解决方案
方案1:直接使用实际触发日期变量
如果只是需要在任务中获取触发日期,无需修改execution_date逻辑,可使用Airflow内置的{{ data_interval_end }}模板变量,它对应数据时间段的结束时间(即任务实际触发时间)。修改你的BashOperator:
task = BashOperator( task_id='task', bash_command='echo "触发日期: {{ data_interval_end.strftime(\'%Y-%m-%d\') }}"', dag=dag, )
2月16日15点触发时,会输出触发日期: 2024-02-16。
方案2:调整DAG配置让execution_date与触发日期对齐
若必须让execution_date等于触发日期,可通过调整start_date和schedule_interval实现:
- 保持
schedule_interval='0 15 * * *',将start_date设置为原起始日期的次日(比如datetime(2024, 1, 2)),同时确保catchup=False。这样第一次运行会在2024-01-02 15:00触发,execution_date为2024-01-02。 - 或者使用
@daily作为调度间隔,配合时区参数指定15点运行:
from airflow.utils.timezone import datetime, utc dag = DAG( 'dag_test', default_args=default_args, description="DAG test", schedule_interval='@daily', concurrency=1, catchup=False, start_date=datetime(2024, 1, 1, tzinfo=utc), timezone='Europe/Paris' # 替换为你的目标时区 )
此配置下,每天当地时间15点触发,execution_date即为当天日期。
注意:修改
execution_date逻辑可能影响depends_on_past=True的依赖关系,需确认历史任务依赖不受破坏。
内容的提问来源于stack exchange,提问作者AlexLM
相关产品推荐
相关产品推荐

