ram4sof
ram4sof

Reputation: 465

NCron expression "0 0/5 19-7 * * Mon,Tue,Wed" failed to parse azure functions

My Cron expression is: 0 0/5 19-7 * * Mon,Tue,Wed

This passed in the following cron expression helper websites https://ncrontab.swimburger.net/ https://crontab.guru

However this fails with following error when printing print next 50 schedules:

[2021-06-07T17:11:38.690Z] Executed 'Function1' (Failed, Id=1005492c-88bd-4cc6-ada5-355cdabdf156, Duration=488ms) [2021-06-07T17:11:38.692Z] System.Private.CoreLib: Exception while executing function: Function1. NCrontab.Signed: '0 0/5 19-7 * * Mon,Tue,Wed' is an invalid crontab expression. It must contain 5 components of a schedule in the sequence of minutes, hours, days, months, and days of week.

Following is the Azure function code that prints next 50 schedules:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using NCrontab;

namespace FunctionApp1
{
    public static class Function1
    {
        private const string SCHEDULE_CRON_EXPRESSION = "0 0/5 19-7 * * Mon,Tue,Wed";
        private const int NUMBER_OF_FUTURE_SCHEDULES = 50;
        #if DEBUG
                const bool RUN_ON_STARTUP = true;
        #else
                const bool RUN_ON_STARTUP = false;
        #endif
        [FunctionName("Function1")]
        public static void Run([TimerTrigger(SCHEDULE_CRON_EXPRESSION, RunOnStartup = RUN_ON_STARTUP)]TimerInfo myTimer, ILogger log)
        {
            DateTime theScheduleDateTime = DateTime.Now;
            var schedule = CrontabSchedule.Parse(SCHEDULE_CRON_EXPRESSION);
            for(int i = 0; i < NUMBER_OF_FUTURE_SCHEDULES; i++)
            {
                theScheduleDateTime = schedule.GetNextOccurrence(theScheduleDateTime);
                log.LogInformation($"Next Schedule {i}: {theScheduleDateTime} ");
            }
        }
    }
}

What am i missing here? And is there any official Azure tool that helps to create and validate the CRON expressions?

Upvotes: 0

Views: 868

Answers (1)

ram4sof
ram4sof

Reputation: 465

The issue is CRON Expression includes seconds which makes 6 expressions values

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

however as per the error

"It must contain 5 components of a schedule in the sequence of minutes, hours, days, months, and days of week"

Not Working private const string SCHEDULE_CRON_EXPRESSION = "0 0/5 19-7 * * Mon,Tue,Wed";

Working private const string SCHEDULE_CRON_EXPRESSION = "0/5 19-7 * * Mon,Tue,Wed";

I am not sure how and why documentation says to include {seconds} and failed to parse by ncron sdk strange! https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#ncrontab-expressions

Upvotes: 0

Related Questions