Triggering an Airflow DAG from terminal not working
Asked Answered
A

6

24

I'm trying to use airflow to define a specific workflow that I want to manually trigger from the command line.

I create the DAG and add a bunch of tasks.

dag = airflow.DAG(
    "DAG_NAME",
    start_date=datetime(2015, 1, 1),
    schedule_interval=None,
    default_args=args)

I then run in the terminal

airflow trigger_dag DAG_NAME

and nothing happens. The scheduler is running in another thread. Any direction is much appreciated. Thank You

Aldrin answered 5/5, 2016 at 1:45 Comment(3)
can you see your dag when u make a airflow list_dags?Traction
Also make sure that the DAG itself is switched on in the UI.Hedgcock
By default, the DAGs are in the 'paused' state, you will have to unpause them before you can trigger a DAG. To unpause a DAG from the CLI, airflow unpause dag_id. To unpause a DAG from the Web UI, go to the Web UI and turn on the DAG.Skidproof
S
38

I just encountered the same issue.

Assuming you are able to see your dag in airflow list_dags or via the web server then:

Not only did I have to turn on the dag in the web UI, but I also had to ensure that airflow scheduler was running as a separate process.

Once I had the scheduler running I was able to successfully execute my dag using airflow trigger_dag <dag_id>

My dag configuration is not significantly different from yours. I also have schedule_interval=None

Stencil answered 13/9, 2016 at 21:34 Comment(3)
I recently encountered the same issue as the poster - switching the DAG on in the UI allowed me to trigger a manual run just fine. Thanks!Pertussis
I had a similar issue trying to run one of the stock examples with airflow trigger_dag. In that case the missing bit was that even though the DAGs were loaded from the example_dags module they also have to be visible to the scheduler in the DAGS_FOLDER or it won't check them.Lowboy
@Aa'Koshh I know it's old, but just to clarify that with the dag examples, they don't have to be in the DAGS_FOLDER. You just need to turn on the toggle from UI of the dag you want to trigger and it will work.Brackett
M
10

You may have disabled the workflow. To enable the workflow manually. Open up the airflow web server by

$ airflow webserver -p 8080

Go to http://localhost:8080 . You should see the list of all available dags with a toggle button on/off. By default everything is set to off. Search for your dag and toggle your workflow. Now try triggering the workflow from terminal. It should work now.

Moreta answered 29/6, 2016 at 11:1 Comment(1)
sitting like 5 hours searching why my scheduler is not working and then found the dag was off... thanks mate!Cohleen
P
6

first make sure your database connection string on the airflow is working, weather it be on postgres, sqlite(by default) or any other database. Then run the command

 airflow initdb

This command should not be showing any connection errors

Secondly make sure your webserver is running on a separate thread

airflow webserver

Then run your schdeuler on a different thread

airflow scheduler

Finally trigger your dag on a different thread after the scheduler is running

airflow trigger_dag dag_id

Also make sure the dag name and task are present in the dag and task list

airflow list_dags
airflow list_tasks dag_id

And if the dag is switched off in your UI then toggle it on.

Periclean answered 8/6, 2017 at 9:7 Comment(0)
W
1

You should 'unpause' the drag you what to trigger. use airflow unpause xxx_drag and then airflow trigger_dag xxx_drag and it should work.

Wyeth answered 22/3, 2019 at 10:31 Comment(0)
D
0

airflow trigger_dag -e <execution_date> <dag_id>

Deprived answered 8/7, 2021 at 20:8 Comment(1)
An explanation would make your answer much more helpfulZeldazelde
A
0

Now you can use the following command to trigger the dag from the cli.

airflow dags trigger -e 2024-06-02T01:10:00+00:00 <dag_id>

I was struggling with the time format but figured it out eventually so I thought I'll post it here if someone else is facing the same issue. Also if you are not sure about the dag_id you can use the airflow dags list command to get the dag_id

PS: Also make sure your DAG start date is small than the date that you're trying to trigger the dag for. For example, if your dag start date is 10th of June 2024, you won't be able to trigger dag for any date prior to 10th of june 2024.

Aftershaft answered 25/6 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.