Airflow - how to get all the future run date

Viewed 4052

I am working on scheduling a airflow job. However to verify if I have scheduled the correct job I need to see when it will run in the future.

Airflow has following command which gives me the next run. however, that's not sufficient for some use cases. for example, I have scheduled a job run every other Friday. How do I verify that.

airflow next_execution <dag_id>

Is there a way, I can get all the future dates when this dag will run. or atleast couple of ?

2 Answers

While most processes use croniter, if you have access to your installation it's always best to get the information from the "source" via existing interfaces:

from airflow import models
from datetime import datetime, timedelta


dag_bag = models.DagBag()

dag_id = "dag_name"
dag = dag_bag.get_dag(dag_id)

now = datetime.now()
until = now + timedelta(days=21)

runs = dag.get_run_dates(start_date=now, end_date=until)
print(runs)

Airflow uses under the hook croniter, for an example. Following the example on the documentation of croniter, this could work as follows (as example, consider that the dag run at 12 pm every Friday and that our base date is yesterday the 20th of August).

from croniter import croniter 
from datetime import datetime

# Specify current date
base = datetime(2020, 8, 20, 0, 0)

# Set croniter
iter = croniter('0 12 * * 5', base)  

# Get next execution 
iter.get_next(datetime)
>>>
datetime.datetime(2020, 8, 21, 12, 0)

where you can specify base as the latest execution date of your dag (dag.latest_execution_date). And you can get it's following executions by calling n times iter.get_next(datetime).

Related