How to wait for a specific task in Apache Airflow?

A common way to control task sequentiality consists on using data sensors. The idea is to wait for the data generated by the previous DAG execution. Actually, there is also a second way to implement sequentiality between DAG executions. It uses ExternalTaskSensor.

dag = DAG(
    dag_id='external_task_sensor_test',
    default_args={
        'start_date':  datetime(2019, 8, 1,  0, 0)
    },
    schedule_interval=timedelta(hours=1)
)
 
 
with dag:
    wait_for_previous_operator = ExternalTaskSensor(
        task_id='wait_for_previous',
        external_dag_id=dag.dag_id,
        execution_delta=timedelta(hours=1),
        external_task_id='task3')
 
    task1 = DummyOperator(
        task_id='task1')
 
    task2 = DummyOperator(
        task_id='task2')
 
    task3 = DummyOperator(
        task_id='task3')
 
    wait_for_previous_operator >> task1 >> task2 >> task3

With that configuration, wait_for_previous_operator will always wait for the task3 of the previous run to be terminated. Please notice one important thing that execution_delta of the sensor has the same value as the schedule_interval of the DAG. Otherwise, your next execution will never happen. Another point is the first execution of the DAG. In that situation you will need to either mark the wait_for_previous_operator as successful manually, or use a branching and execute this task only when the execution time is different than the ''start_date' of the DAG.