airflowairflow-2.xairflow-webserver

dependent tasks in different dags not triggering


I have two dags in which i want to trigger another task in a dag once the first one finishes.

Parent dag:
from airflow import DAG
from datetime import datetime, timedelta
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.dagrun_operator import TriggerDagRunOperator
import logging


SCHEDULE = "59 9 * * 1-5" 
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2025, 2, 26),
'schedule_interval': SCHEDULE
 }

dag = DAG('first_dag', catchup=False, default_args=default_args)

first_task = DummyOperator(
task_id='first_task',
dag=dag
)

second task = TriggerDagRunOperator(
task_id='second task',
trigger_dag_id='second_dag',
dag=dag
 )

 first_task >> second task

  Child Dag:

   from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.sensors.external_task import ExternalTaskSensor
import logging

default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2025, 2, 26)
 }

dag = DAG('second_dag', default_args=default_args)

third_task = ExternalTaskSensor(
task_id='third_task',
external_dag_id='first_dag',
external_task_id='second_task',
start_date=datetime(2025, 2, 26)

 )

fourth_task = DummyOperator(
task_id='fourth_task',
dag=dag,
  
 third_task >> fourth_task

So first dag has first task and second task while second dag has third task and fourth task, I want to schedule first dag in which the first task should run followed by second task, and then from there the third task from the second dag should triggered followed by fourth task. How to achieve this? I tried the below code but the second dag is in paused state..


Solution

  • There is a bit of circular logic in the code provided with task 2 triggering task 3 which is looking for task 2 to complete. Since Dag 1 is sending an event to Dag 2 to run when it is completed, there isn't a need to check if Dag 1 task 2 has completed. To make this work efficiently for Dag 1's completion to be immediately followed by task 4's execution, get rid of task 3 in Dag 2 so that when task 2 runs, it triggers Dag 2 running only task 4.

    If you only want Dag 2 to run after Dag 1 completes, make sure to set the schedule for Dag 2 to None. Giving Dag 2 its own schedule will make it run regardless of what happens in Dag 1. If the schedule is set to None you won't need to have a sensor pointing back at Dag 1 ensuring its completion since the only time it will run is when triggered by task 2.