The ExternalTaskSensor
in Apache Airflow is used to wait for a task in a different DAG (Directed Acyclic Graph) to complete before proceeding. This operator is useful when you have dependencies between tasks across multiple DAGs.
How to Use ExternalTaskSensor
To use the ExternalTaskSensor
, you need to specify the task_id
of the task you want to wait for and the dag_id
of the DAG that contains that task. You can also define the execution date for which the task should be checked.
Example
Here's a basic example of how to use ExternalTaskSensor
in an Airflow DAG:
Setup
- DAG 1: The first DAG, which contains the task that will be waited on.
- DAG 2: The second DAG, which includes the
ExternalTaskSensor
.
DAG 1: dag_one.py
DAG 2: dag_two.py
Explanation
DAG 1 (
dag_one
):- Contains two dummy tasks:
start_task
andend_task
. - The
end_task
is the one we will wait for indag_two
.
- Contains two dummy tasks:
DAG 2 (
dag_two
):- Uses the
ExternalTaskSensor
to wait for theend_task
indag_one
. - The
mode
can be set topoke
(checks periodically) orreschedule
(reschedules the task when it’s not yet done). - You can set
timeout
to specify how long to wait for the external task to complete andpoke_interval
for how often to check.
- Uses the
Notes
- Ensure both DAGs are in the same Airflow instance.
- The
start_date
in both DAGs should be set appropriately for theExternalTaskSensor
to work as expected. - The
external_task_id
must match thetask_id
in the external DAG.
This setup allows dag_two
to execute next_task
only after end_task
in dag_one
has completed successfully.
No comments:
Post a Comment