[SOLVED] How do we trigger single airflow dag multipler times using TriggerDagRunOperator?


This Content is from Stack Overflow. Question asked by Tarun Bhardwaj

I have a Dag with schedule interval None. I want to trigger this Dag by TriggerDagRunOperator multiple times in a day.

I crated a PreDag with schedule_interval “* 1/12 * * *”
Inside PreDag a task of TriggerDagRunOperator runs that Trigger the main Dag.
As scheduled PreDag runs twice a day 1st time when PreDag runs it trigger the Dag but 2nd time when PreDag runs then task of triggerDagRunOperator show error :
” A Dag Run already exists for dag id {{ dag_id}} at {{ execution_date}} with run id {{ trigger_run_id}}” `

trigger_run = TriggerDagRunOperator(
                trigger_run_id = 'trig__' + str(datetime.now()),
                execution_date = '{{ ds }}',
                # reset_dag_run = True ,
                dag = predag

Is it possible to Trigger a dag multiple times in a day using TriggerDagRunOperator.


Airflow uses execution_date and dag_id as ID for dag run table, so when the dag is triggered for the second time, there is a run with the same execution_date created in the first run.

Why do you have this problem? that’s because you are using {{ ds }} as execution_date for the run:

The DAG run’s logical date as YYYY-MM-DD. Same as {{ dag_run.logical_date | ds }}.

which is the date of your run and not the datetime, and the date of two runs triggered in the same day is the same.

You can fix it by replacing {{ ds }} by {{ ts }}

This Question was asked in StackOverflow by Tarun Bhardwaj and Answered by Hussein Awala It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?