The TimeSensor
operator in Apache Airflow is used to pause a DAG's execution until a specified time of day. This is useful when you want a task to run only after a certain time, regardless of when the DAG starts. Once the specified time is reached, the TimeSensor
allows the DAG to proceed to the next tasks.
Example: Using TimeSensor
to Wait Until a Specific Time
Let’s say we want a DAG to start with a task that waits until 2:00 PM each day before running the actual data processing tasks. We’ll use TimeSensor
to accomplish this.
Explanation of the DAG
TimeSensor:
- The
wait_until_2pm
task is aTimeSensor
that pauses the DAG until it’s 2:00 PM (14:00). - The
target_time
argument specifies the time to wait until. In this example, it’s set totime(14, 0)
, which is 2:00 PM. - The
poke_interval
is set to 5 minutes (300 seconds), meaning the sensor will check every 5 minutes if the current time has reached 2:00 PM. - The
mode
is set to"reschedule"
, which frees up worker slots while waiting, allowing other tasks to run.
- The
Data Processing Task:
- The
data_processing
task runs only after theTimeSensor
has allowed the DAG to proceed, ensuring it starts after 2:00 PM.
- The
Dependencies:
- The DAG only proceeds to the
data_processing
task once theTimeSensor
condition (2:00 PM) is met.
- The DAG only proceeds to the
Benefits
- Controlled Timing:
TimeSensor
ensures tasks don’t run until a specific time. - Efficient Resource Management: Using
"reschedule"
mode helps free up resources while waiting, reducing the load on the Airflow scheduler.
No comments:
Post a Comment