-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dagit] Support special cron strings (#7717)
### Summary & Motivation Support special cron strings like `@daily`, which are parsed as invalid by `cronstrue`. ### How I Tested These Changes Force aliases to be the values for all rendered cron strings, verify that the parsed result is correct.
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
js_modules/dagit/packages/core/src/schedules/humanCronString.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {humanCronString} from './humanCronString'; | ||
|
||
describe('humanCronString', () => { | ||
it('parses special strings correctly', () => { | ||
expect(humanCronString('@daily')).toBe('At 12:00 AM'); | ||
expect(humanCronString('@weekly')).toBe('At 12:00 AM, only on Sunday'); | ||
expect(humanCronString('@monthly')).toBe('At 12:00 AM, on day 1 of the month'); | ||
}); | ||
}); |
23 changes: 22 additions & 1 deletion
23
js_modules/dagit/packages/core/src/schedules/humanCronString.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
import cronstrue from 'cronstrue'; | ||
|
||
export const humanCronString = (cronSchedule: string) => { | ||
const standardCronString = convertIfSpecial(cronSchedule); | ||
try { | ||
return cronstrue.toString(cronSchedule); | ||
return cronstrue.toString(standardCronString); | ||
} catch { | ||
return 'Invalid cron string'; | ||
} | ||
}; | ||
|
||
// https://en.wikipedia.org/wiki/Cron#Nonstandard_predefined_scheduling_definitions | ||
const convertIfSpecial = (maybeSpecial: string) => { | ||
switch (maybeSpecial) { | ||
case '@yearly': | ||
case '@annually': | ||
return '0 0 1 1 *'; | ||
case '@monthly': | ||
return '0 0 1 * *'; | ||
case '@weekly': | ||
return '0 0 * * 0'; | ||
case '@daily': | ||
case '@midnight': | ||
return '0 0 * * *'; | ||
case '@hourly': | ||
return '0 * * * *'; | ||
default: | ||
return maybeSpecial; | ||
} | ||
}; |