2

I am trying to populate a CRON expression that will pretend to be never executed(at least not in this life time).

I went through this SO question https://stackoverflow.com/questions/8324306/cron-job-that-will-never-execute

But each expression in that question gives an exception Microsoft.Azure.WebJobs.Host: Error indexing method 'Cleanup'. Microsoft.Azure.WebJobs.Extensions: The schedule expression '0 0 5 31 2 ?' was not recognized as a valid cron expression or timespan string.

What are the possible expressions that will fullfill the above mentioned expectation with regard to Azure Functions?

Thank you.

Isuru
  • 950
  • 1
  • 13
  • 34

3 Answers3

13

You cannot set never per algorithm, but a leap year with Saturday 29th gives a 28-year gap.

0 0 0 29 Feb Sat

as the first time around this will happen again is 2048.

Good enough?

  • wow... how did you find that solution? – nelion Sep 07 '22 at 08:00
  • 1
    @nelion By solving the problem: what is the longest period CRON is able to define starting from today? Trying out all options it turns out that you have to rely on rare events. Btw we use this expression in production when we need it. –  Sep 08 '22 at 13:06
  • Ah ok. Kudos for thinking out of the box. – nelion Sep 08 '22 at 20:10
7

Please check the Azure CRON expression, it's:

{second} {minute} {hour} {day} {month} {day-of-week}

And it uses the NCronTab library to interpret CRON expressions. In the github page you could find the value column can have a * or a list of elements separated by commas. That means it doesn't support ?.

So just change your expression to 0 0 5 31 2 * it will be approved. And if you don't your function running you could just disable it. You could refer to this tutorial: How to disable functions in Azure Functions.

Update:

Due to the Function will calculate the Timer to get the function running time and the 2/30 and 2/31 will never come, then it will be in a loop calculation and the year will increase until beyond the limit 9999. In this situation the function will send a exception.

George Chen
  • 13,703
  • 2
  • 11
  • 26
  • @Isuru, anyy process now? If this could help you, you mark it as the answer. Thanks! – George Chen Jun 14 '19 at 01:09
  • 1
    Really sorry for the late reply. I used `0 0 5 31 2 *` on TimerTrigger but now I am getting this exception `Microsoft.Azure.WebJobs.Host: Error indexing method 'Cleanup'. System.Private.CoreLib: Valid values are between 1 and 9999, inclusive. Parameter name: year.` Any idea why this happens? – Isuru Jun 14 '19 at 05:55
  • 2
    @Isuru, this is because the TimerTrigger Function would calculate the next run time with the cron expression, and the 2/31 or 2/20 will never run then it it will loop calculate and the year number will beyond the limit. – George Chen Jun 14 '19 at 06:15
  • @Isuru, and in your situation if you want to set 2/31, it will never run so why not disable the function. – George Chen Jun 14 '19 at 06:16
  • @Isuru, also if you set 2/29,in a short term, it won't execute and no exception. – George Chen Jun 14 '19 at 06:22
  • Thanks for the information. I used 2/29 and it worked. It will execute in every 8 years. Will stick to this. – Isuru Jun 14 '19 at 06:33
  • 1
    Could you update the answer to reflect the latest conclusion so that others can get an idea. I will mark this as the answer. Thanks! – Isuru Jun 14 '19 at 06:35
  • @Isuru, I will. So if you don't want it execute, what's the Timer meaning? – George Chen Jun 14 '19 at 06:36
  • If I set the `schedule` appsetting which is used in TimerTrigger to empty string(This we get from user interaction) with disabling the function at the same time still I get an exception for expression not valid. I think we can't set empty string for TimerTrigger expression. That's why I am finding a valid but a long time expression so that the exception from function is not occurred. I disable function as well. – Isuru Jun 14 '19 at 06:46
1

The main format used for the scheduled WebJob

  • The cron expression is composed of 6 fields: {second} {minute} {hour} {day} {month} {day of the week}.
  • The supported operators are: , - * /
  • Each field can have a specific value (1), a range (1-10), a set of values (1,2,3), all values (), an interval value (/2 == 0,2,4,6,...) or a mix of these (1,5-10).
  • Each value represents a point in time, for example: "5 * * * * *" - means on the 5th second of every minutes --> 00:00:05, 00:01:05, 00:02:05, ... (and not every 5 seconds).

hence according to above rules your cron expression will leads to this error

instead of using 0 0 5 31 2 ? use 0 0 5 31 2 *

Thili
  • 748
  • 1
  • 9
  • 21