Azure / azure-functions-templates

Azure functions templates for the azure portal, CLI, and VS
MIT License
344 stars 196 forks source link

nchron expression should be validated / type-safe #4167 #1538

Open spotlesscoder opened 3 months ago

spotlesscoder commented 3 months ago

As suggested in https://github.com/microsoft/vscode-azurefunctions/issues/4167, I am recreating the issue here.

Issue description:

I have written a function in Typescript with a timer trigger. The first function I wrote had the cron expression 0 0 13 * * 1-5 Then I copied the function for another task which I chose the cron expression 0 0 3 * * 1-7 for. I made the wrong assumption that 1-5 would be Monday-Friday and derived that 7 would then be Sunday The problem was that the second function was never run and thus part of the software was never working at all. The weekdays start at 0, not at 1

I would expect that the schedule property from the TimerTriggerOptions interface in @azure/functions library is migrated from type string to a complex type that validates each nchron expression element Pseudocode (incomplete)

export type SecondsOrMinutes = '*' | 0 | 1 | 2 // .... continue until 59
export type Hours = '*' | 0 | 1 // ... continue to 23
export type Weekdays = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 'Mon' | 'Tue' //  ... continue to 'Sun'
export interface WeekdayRange {
  start: Weekdays;
  end: Weekdays;
}

export interface NChronExpression {
   seconds: SecondsOrMinutes;
   minutes: SecondsOrMinutes;
   hours: Hours;
   days: Days;
   months: Months;
   weekDays: '*' | Weekdays[] | WeekdayRange;
}

In addition (or as a replacement if the suggestion above is not feasible), the vscode azure functions extension should validate the nchron strings it finds in the codebase and show invalid expressions in the Problems view. If this is also not possible, it should at least scan functions before the deploy action is done in vscode so invalid nchron expressions can never be deployed