Airflow does not support DAGs with loops. After all, the abbreviation DAG stands for Directed Acyclic Graph, so we can’t have cycles. It is also not the standard usage of Airflow, which was built to support daily batch processing.

All of that does not stop us from using a simple trick that lets us run a DAG in a loop. To do that, we have to add a TriggerDagRunOperator as the last task in the DAG. In the task configuration, we specify the DAG id of the DAG that contains the task:

from airflow.operators.dagrun_operator import TriggerDagRunOperator

trigger_self = TriggerDagRunOperator(
    task_id='repeat'
    trigger_dag_id=dag.dag_id,
    dag=dag
)

the_rest_of_the_dag >> trigger_self  # add it as the last task
Stop AI Hallucinations Before They Cost You.
Join engineering leaders getting weekly tactics to prevent failure in customer-facing AI systems. Straight from real production deployments.
Stop AI Hallucinations Before They Cost You.
Join engineering leaders getting weekly tactics to prevent failure in customer-facing AI systems. Straight from real production deployments.
Older post

How to use xcom_pull to get a variable from another DAG

Get an XCOM variable from another DAG

Newer post

How to check when an Athena table was updated

How to track the time when an Athena table was updated

Engineering leaders: Is your AI failing in production? Take the 10-minute assessment
>
×
Stop AI Hallucinations Before They Cost You.
Join engineering leaders getting weekly tactics to prevent failure in customer-facing AI systems. Straight from real production deployments.