Airflow - Get start time of dag run
Asked Answered
P

4

11

Is it possible to get the actual start time of a dag in Airflow? By start time I mean the exact time the first task of a dag starts running.

I know I can use macros to get the execution date. If the job is ran using trigger_dag this is what I would call a start time but if the job is ran on a daily schedule then {{ execution_date }} returns yesterdays date.

I have also tried to place datetime.now().isoformat() in the body of the dag code and then pass it to a task but this seems to return the time the task is first called rather than when the dag itself started.

Pascal answered 3/11, 2017 at 13:24 Comment(0)
P
13

{{ dag_run.start_date }} provides the actual start time of the dag

Pascal answered 7/11, 2017 at 8:35 Comment(0)
P
15

This is an old question, but I am answering it because the accepted answer did not work for me. {{ dag_run.start_date }} changes if the DAG run fails and some tasks are retried.

The solution was to use: {{ dag_run.get_task_instance('start').start_date }} which uses the start date of the first task (DummyOperator task with task_id: start).

Pappano answered 21/1, 2020 at 16:54 Comment(1)
This is very interesting.. can't try it right now but can't help to leave a comment, very useful if this is the ONLY way to get the actual start time of a DAG (regardless of retrying tasks). It does make start_time unclear if you had to retry tasks though.Compatible
P
13

{{ dag_run.start_date }} provides the actual start time of the dag

Pascal answered 7/11, 2017 at 8:35 Comment(0)
E
0

I am following the way as you stated:

By start time I mean the exact time the first task of a dag starts running

You can still do this with macros on your first task, try this {{ task.start_date }}

All the variables can be found in TaskInstance class: https://github.com/apache/incubator-airflow/blob/master/airflow/models.py#L746

Exist answered 3/11, 2017 at 16:53 Comment(1)
This link is broken.Majors
S
0

Since version 2.2 it is possible to use {{ data_interval_end }}, which is effectively also the DAG activation time and does not change if any DAG steps are retried. Details about data intervals here.

Spanish answered 13/6 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.