Pari
Pari

Reputation: 51

Cron expression which run on the last day of the every month

I need to create a cron expression that will run on the every last day of every month. I need this for scheduling the webjob.

I am using this expression but the webjob is not taking L for finding Last day of the every month.

"0 0 11 L * *"

Thanks.

Upvotes: 5

Views: 13189

Answers (2)

Rukai
Rukai

Reputation: 1

We don't currently support 'L'. We use ncrontab to parse the cron expression and the author has noted that this is not supported: atifaziz/NCrontab#9

One alternative I've seen: Set a cron expression for the 28-31 or each month and have the function itself check to see if it's actually the last day of the month. If not, exit; if so, continue.

Upvotes: 0

Bruce Chen
Bruce Chen

Reputation: 18465

According to your description, I checked this issue on my side. For a simple way, firstly I tested it on azure portal as follows:

enter image description here

Note: Based on my test, the special characters (?,L,W) for {day} is invalid.

Also, I ran the webjob on my side with the 0/5 * * L * * expression, then I got the following error:

Unhandled Exception: Microsoft.Azure.WebJobs.Host.Indexers.FunctionIndexingException: Error indexing method 'Functions.CronJob' ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NCrontab.CrontabException: 'L' is not a valid [Day] crontab field expression. ---> NCrontab.CrontabException: 'L' is not a valid [Day] crontab field value. It must be a numeric value between 1 and 31 (all inclusive).

Then I followed this sample TimerSamples.cs and found that we could only override the build-in DailySchedule and WeeklySchedule, but they could not meet your requirement, I assumed that you may need to build your custom schedule inherits TimerSchedule to achieve your purpose.

AFAIK, we could also leverage Azure Scheduler to trigger our webjob on some schedule in addition to set the cron expression with your webjob. Here is my test, you could refer to it:

  • Configure your webjob as manual triggered

  • Log into azure portal, add your Azure Scheduler, then configure the Action settings point to your webjob endpoint that allows is to get triggered. For more details, you could refer to the section about adding a scheduler job in this tutorial

  • Then configure the schedule as follows:

    enter image description here

Additionally, for full cron expression support, you could add your feedback at issues of Azure/azure-webjobs-sdk-extensions.

Upvotes: 2

Related Questions