wi3o
wi3o

Reputation: 1607

Airflow : Trigger a dag from another dag in another node?

We have 2 different nodes running different Airflow dags.

We have a controller_dag (the one that is supposed to trigger the target dag) in one node and a target_dag in another node.

We have been able to trigger target_dags from controller_dags in the same node.

Is it possible to trigger a target_dag in one node from another controller_dag in another node?

Could you please provide an example?

Upvotes: 1

Views: 779

Answers (1)

wi3o
wi3o

Reputation: 1607

I found the answers to my own questions

Yes, it is possible to trigger a target_dag in one node from another controller_dag in another node.

Example:

# Create a python function to trigger a dag using airflow experimental api
def trigger_target_dag(airflow_host_url, **kwargs):
    airflow_url = airflow_host_url + '/api/experimental/dags/target_dag/dag_runs'
    r = requests.post(airflow_url,
                      data=json.dumps({'excution_date':datetime.utcnow().isoformat()})
                     )

# Call the function above using PythonOperator
trigger_target_dag_task = PythonOperator(
    task_id = 'trigger_target_dag',
    python_callable = trigger_target_dag,
    op_kwargs = {'airflow_host_url': airflow_host_url},
    dag = dag
)

airflow experimental api

Upvotes: 1

Related Questions