0
votes

I need to run 2 tasks on below condition

  1. 1st task will run every alternate Thursday at 3 AM
  2. 2nd task will run every day at 12 AM except on the days 1st job will run.

I'm new to cron job expressions but tried few approaches. I'm using Spark, Scala for creating tasks and Airflow for scheduling the task.

schedule_interval= '0 0 3 1-5,15-19,29-31 * THU'

I've tried above expression for first task in Airflow but it will fail if alternate Thursday will occur on 6-14, 20-28 For example in January 2022 Thursdays will come on 6th, 13th, 20th and 27th. The other issue will come for example if alternate Thursday comes between 29-31 and next Thursday comes between 1-5 job will run for 2 consecutive Thursday.

1

1 Answers

0
votes

I resolved the above use-case using below mentioned piece of code

def is_alternate_thursday(starting_day, date_to_check):
    set_day = THURSDAY

    if (date_to_check < starting_day) or (date_to_check.weekday() != set_day):
        return False

    base_week = starting_day.isocalendar()[1]
    checked_week = date_to_check.isocalendar()[1]

    return (checked_week - base_week) % 2 == 0

Based on the flag now we can run the task.