How to delay an Airflow DAG until a given hour using the DateTimeSensor

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (36/100)

The DateTimeSensor is another part of the Airflow API that seemingly makes no sense. Why would I want to wait until a specified time? For sure, there is little usage of configuration looking like this:

1
2
3
4
5
6
from airflow.sensors.date_time_sensor import DateTimeSensor

time_sensor = DateTimeSensor(
    task_id='wait_until_new_year',
    target_time='2021-01-01T00:00:00',
)

I could imagine only one situation when it is useful. For example, we may have a DAG that will execute only once when we notify the customers that we have just launched a new product. Such mailing will require a few preparation steps, so we can perform those tasks earlier and then wait until the launch date to send the emails.

However, the DateTimeSensor can be used with templates, and that makes it way more powerful. If I have a DAG that must wait until 10 am to execute some tasks, I can configure the sensor using the execution_date parameter:

1
2
3
4
time_sensor = DateTimeSensor(
    task_id='are_we_there_yet',
     target_time='{{ execution_date.replace(hour=10) }}', 
)

What is the advantage of this configuration? When I run a backfill for some past date, it will not get stuck at the time sensor (because the date has already passed). If I used TimeDeltaSensor, it would wait the specified number of minutes even during the backfills, which probably makes no sense.

Did you enjoy reading this article?
Would you like to learn more about software craft in data engineering and MLOps?

Subscribe to the newsletter or add this blog to your RSS reader (does anyone still use them?) to get a notification when I publish a new essay!

Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.

Bartosz Mikulski

Bartosz Mikulski

  • Data/MLOps engineer by day
  • DevRel/copywriter by night
  • Python and data engineering trainer
  • Conference speaker
  • Contributed a chapter to the book "97 Things Every Data Engineer Should Know"
  • Twitter: @mikulskibartosz
Newsletter

Do you enjoy reading my articles?
Subscribe to the newsletter if you don't want to miss the new content, business offers, and free training materials.