Skip to content

Commit

Permalink
[dagit] Support special cron strings (#7717)
Browse files Browse the repository at this point in the history
### 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
hellendag committed May 4, 2022
1 parent 857079d commit 7504407
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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 js_modules/dagit/packages/core/src/schedules/humanCronString.ts
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;
}
};

0 comments on commit 7504407

Please sign in to comment.