H. Pauwelyn
H. Pauwelyn

Reputation: 14320

NCRONTAB for every first tuesday of every month for an Azure Function

How could I create an timer trigger with Azure Functions (version 3 and .NET Core) that must be executed every first Tuesday from every month at 8 AM. Starting from now (05/08/2020) this must be the next five occurrences:

  1. 2020/06/02 Tue 08:00:00
  2. 2020/07/07 Tue 08:00:00
  3. 2020/08/04 Tue 08:00:00
  4. 2020/09/01 Tue 08:00:00
  5. 2020/10/06 Tue 08:00:00

By using www.cronmaker.com, I've next NCRONTAB:

0 0 8 ? 1/1 TUE#1 *

But then I've next exception:

The schedule expression 0 0 8 ? 1/1 Tue#1 * was not recognized as a valid CRON expression or TimeSpan string.

Then I've start changing the CRON expression to next varations:

CORN Result
0 0 8 ? 1/1 Tue#1 Error from above
0 0 8 * 1/1 Tue#1 Error from above
0 0 8 1/1 Tue#1 * Error from above
0 0 8 * 1/1 Tue 1 Error from above
0 0 8 ? 1/1 Tue 1 Error from above
0 0 8 * 1/1 Tue/1 2020/05/29 08:00:00 - 2020/05/30 08:00:00
0 0 8 * * Tue/2 2020/05/30 08:00:00 - 2020/06/02 08:00:00
0 0 8 * 1/1 Tue/2 2020/05/30 08:00:00 - 2020/06/02 08:00:00
0 0 8 ? 1/1 Tue/2 Error from above

So every expression I've made, would not work as expected. My question is now: What's the correct expression?

Upvotes: 1

Views: 1832

Answers (2)

George Chen
George Chen

Reputation: 14334

The first Monday of the month falls on one (and only one) of the dates from the first to the 7th inclusive. Then the cron expression will be easy to get it.

Suppose it should be 0 0 8 1-7 * Tue and the below is my test, it shows the first five dates.

enter image description here

Upvotes: 3

MarkXA
MarkXA

Reputation: 4384

Unfortunately the timer trigger uses the NCrontab library, which doesn't have the concept of "first Tuesday". I think the best you can do is to set the expression to 0 0 8 * * 2 to trigger at 8am every Tuesday and have a bit of code in the actual function that returns immediately if it's not the first Tuesday of the month.

Upvotes: 1

Related Questions