Search code examples
pythonpython-3.xrestairflowairflow-api

Obtaining next scheduled run time of a DAG using Airflow 2.2.5 REST API


I used Airflow 2.4.2 REST API and python apache-airflow-client to retrieve the next scheduled run time of a DAG.

from airflow_client.client.api import dag_api


 with airflow_client.client.ApiClient(conf) as api_client:
        api_instance = dag_api.DAGApi(api_client)
        dag_id = build_dag_id(name)

        response = api_instance.get_dag(dag_id)
        next_run = response["next_dagrun"]

However, now I need to do the same with Airflow 2.2.5 REST API, and it turns out that the next_dagrun value does not exist there. Now I'm looking for an alternative way to obtain this value. I would appreciate any advice.


Solution

  • Hmm, if you have a datetime of the last run, you can use this approach:

    # Obtain the DAG's schedule interval    
    url = 'http://localhost:8080/api/v1/dags/{dag_id}'
    response = requests.get(url)
    dag_info = response.json()
    schedule_interval = dag_info['schedule_interval']
    
    # Calculate the next scheduled run time
    last_run = datetime.now()
    cron = croniter(schedule_interval, last_run)
    next_run = cron.get_next(datetime)