Search code examples
python-2.7airflow

How to get dag status like running or success or failure


I want to know the status of dag whether it is running or failure or success. I am triggering dag through CL argument airflow trigger and after the execution of job, I want to know the status of the run. I couldn't find any way.

I tried airflow dag_state but it is giving none. What should I do if there are more than one runs in a day to get status of latest run through command line argument or through python code.


Solution

  • You can use list_dag_runs command with the CLI to list the dag runs for a given dag ID. The information returned includes the state of each run.

    You can also retrieve the information via python code a few different ways. One such way that I've used in the past is the 'find' method in airflow.models.dagrun.DagRun

    An example with python3 on how to get the state of dag_runs via DagRun.find():

    dag_id = 'fake_dag_id'
    dag_runs = DagRun.find(dag_id=dag_id)
    for dag_run in dag_runs:
          print(dag_run.state)