Skip to content
Advertisement

How to set multiple schedule in azure timer function?

I have a working sample azure timer function, but about setting it in multiple schedule, I’m not sure if doing it correctly, because I can’t test it immediately because it is scheduled hours gap.

My goal is, to display context.log every 8:00AM, and 8:00PM every day.

note: my code below actually don’t work because parameter hour does not accept array (for demonstration purpose only)


Here is my code:

export const TimerTrigger1 = TypedAzFunc.createFunctionBuilder(__dirname)
  .with(
    TimerTriggerPlugin.init({
      schedule: {
        crontab: {
          second: 0,
          minute: { interval: 1 },
          hour: [{ interval: 8 }, { interval: 20 }],
          day: '*',
          month: '*',
          dayOfWeek: '*',
        },
      },
    })
  )
  .build(async (context, timer) => {
    var timeStamp = new Date().toISOString()

    if (timer.isPastDue) {
      context.log('timer has already triggered')
    }
    context.log('timer has triggered', timeStamp)
  })

export const run = TimerTrigger1.run

Advertisement

Answer

you can achieve this, by passing an array in the hour property.

TimerTriggerPlugin.init({
  schedule: {
    crontab: {
      second: 0,
      minute: 0,
      hour: [8, 20],
      day: '*',
      month: '*',
      dayOfWeek: '*',
    },
  },
})

this will result a schedule for example:

if today is february 18, 2022 7:00am

02/18/2022 08:00:00Z
02/18/2022 20:00:00Z
02/18/2022 08:00:00Z
02/18/2022 20:00:00Z
02/18/2022 08:00:00Z
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement