airflowairflow-scheduler

schedule a monthly DAG to run on the next weekday


I have to schedule a DAG that should run on 15th of every month. However, if 15th falls on a Sunday/Saturday then the DAG should skip weekends and run on coming Monday.

For example, May 15 2021 falls on a Saturday. So, instead of running on 15th of May, the DAG should run on 17th, which is Monday.

Can you please help to schedule it in airflow?

Thanks in advance!


Solution

  • For Airflow >= 2.2.0:

    Airflow AIP-39 Richer scheduler_interval has been completed. This allows more scheduling capabilities. You can use Timetables feature for that. This will allow you to customize scheduling time according to your needs. Either use a build-in timetable or create one of yours.

    For Airflow < 2.2.0:

    The logic of scheduling is limited by what you can do with single cron expression. So if you can't say it in cron expression you can't provide such scheduling in Airflow.

    That said, you can still get the desired functionality by writing some code. You can set your dag to start on the 15th of each month and then place a sensor that verify that the date is Mon-Fri (if not it will wait):

    from airflow.sensors.weekday import DayOfWeekSensor
    dag = DAG(
        dag_id='work',
        schedule_interval='0 0 15 * *',
        default_args=default_args,
        description='Schedule a Job on 15 of each month',
    )
    
    weekend_check = DayOfWeekSensor(
        task_id='weekday_check_task',
        week_day={'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'},
        mode='reschedule',
        dag=dag)
    
    op_1 = YourOperator(task_id='op1_task',dag=dag)
    
    weekend_check >> op_1
    

    Note: If you are running airflow<2.0.0 you will need to change the import to:

    from airflow.contrib.sensors.weekday_sensor import DayOfWeekSensor