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_taskandend_task. - The
end_taskis the one we will wait for indag_two.
- Contains two dummy tasks:
DAG 2 (
dag_two):- Uses the
ExternalTaskSensorto wait for theend_taskindag_one. - The
modecan be set topoke(checks periodically) orreschedule(reschedules the task when it’s not yet done). - You can set
timeoutto specify how long to wait for the external task to complete andpoke_intervalfor how often to check.
- Uses the
Notes
- Ensure both DAGs are in the same Airflow instance.
- The
start_datein both DAGs should be set appropriately for theExternalTaskSensorto work as expected. - The
external_task_idmust match thetask_idin 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