Skip to content

Commit 97cc5de

Browse files
Rob-Powellbigadsoleiman
authored andcommitted
feat: adding schedule option for sagemaker models
1 parent 8838856 commit 97cc5de

File tree

9 files changed

+508
-1
lines changed

9 files changed

+508
-1
lines changed

cli/aws-cron-expressions.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export const minuteExp = `(0?[0-9]|[1-5][0-9])`; // [0]0-59
2+
export const hourExp = `(0?[0-9]|1[0-9]|2[0-3])`; // [0]0-23
3+
export const dayOfMonthExp = `(0?[1-9]|[1-2][0-9]|3[0-1])`; // [0]1-31
4+
export const monthExp = `(0?[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)`; // [0]1-12 or JAN-DEC
5+
export const dayOfWeekExp = `([1-7]|SUN|MON|TUE|WED|THU|FRI|SAT)`; // 1-7 or SAT-SUN
6+
export const yearExp = `((19[8-9][0-9])|(2[0-1][0-9][0-9]))`; // 1980-2199
7+
export const numbers = `([0-9]*[1-9][0-9]*)`; // whole numbers greater than 0
8+
9+
export function dayOfWeekHash(): string {
10+
return `(${dayOfWeekExp}#[1-5])`; // add hash expression to enable supported use case
11+
}
12+
13+
function rangeRegex(values: string): string {
14+
return `(${values}|(\\*\\-${values})|(${values}\\-${values})|(${values}\\-\\*))`;
15+
}
16+
17+
function listRangeRegex(values: string): string {
18+
const range = rangeRegex(values);
19+
return `(${range}(\\,${range})*)`;
20+
}
21+
22+
function slashRegex(values: string): string {
23+
const range = rangeRegex(values);
24+
return `((\\*|${range}|${values})\\/${numbers})`;
25+
}
26+
27+
function listSlashRegex(values: string): string {
28+
const slash = slashRegex(values);
29+
const slashOrRange = `(${slash}|${rangeRegex(values)})`;
30+
return `(${slashOrRange}(\\,${slashOrRange})*)`;
31+
}
32+
33+
function commonRegex(values: string): string {
34+
return `(${listRangeRegex(values)}|\\*|${listSlashRegex(values)})`;
35+
}
36+
37+
export function minuteRegex(): string {
38+
return `^(${commonRegex(minuteExp)})$`;
39+
}
40+
41+
export function hourRegex(): string {
42+
return `^(${commonRegex(hourExp)})$`;
43+
}
44+
45+
export function dayOfMonthRegex(): string {
46+
return `^(${commonRegex(dayOfMonthExp)}|\\?|L|LW|${dayOfMonthExp}W)$`;
47+
}
48+
49+
export function monthRegex(): string {
50+
return `^(${commonRegex(monthExp)})$`;
51+
}
52+
53+
export function dayOfWeekRegex(): string {
54+
const rangeList = listRangeRegex(dayOfWeekExp);
55+
return `^(${rangeList}|\\*|\\?|${dayOfWeekExp}L|L|L-[1-7]|${dayOfWeekHash()})$`;
56+
}
57+
58+
export function yearRegex(): string {
59+
return `^(${commonRegex(yearExp)})$`;
60+
}

cli/aws-cron-validator.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class AWSCronError extends Error {}
2+
3+
import {
4+
minuteRegex,
5+
hourRegex,
6+
dayOfMonthRegex,
7+
monthRegex,
8+
dayOfWeekRegex,
9+
yearRegex,
10+
} from './aws-cron-expressions';
11+
12+
export class AWSCronValidator {
13+
14+
public static validate(expression: string): string {
15+
if (!expression.trim()) {
16+
throw new AWSCronError(
17+
`No parameters entered, this format is required in UTC: 0 20 ? * SUN-FRI *`
18+
);
19+
}
20+
const valueCount = expression.split(" ").length;
21+
if (valueCount !== 6) {
22+
throw new AWSCronError(
23+
`Incorrect amount of parameters in '${expression}'. 6 required, ${valueCount} provided.`
24+
);
25+
}
26+
27+
const [minute, hour, dayOfMonth, month, dayOfWeek, year] = expression.split(" ");
28+
29+
// special handling for Day of Month and Day of Week
30+
if (!((dayOfMonth === "?" && dayOfWeek !== "?") || (dayOfMonth !== "?" && dayOfWeek === "?"))) {
31+
throw new AWSCronError(
32+
`Invalid combination of day-of-month '${dayOfMonth}' and day-of-week '${dayOfWeek}'. One must be a question mark (?)`
33+
);
34+
}
35+
36+
if (!new RegExp(minuteRegex()).test(minute)) {
37+
throw new AWSCronError(`Invalid minute value '${minute}'.`);
38+
}
39+
if (!new RegExp(hourRegex()).test(hour)) {
40+
throw new AWSCronError(`Invalid hour value '${hour}'.`);
41+
}
42+
if (!new RegExp(dayOfMonthRegex()).test(dayOfMonth)) {
43+
throw new AWSCronError(`Invalid day-of-month value '${dayOfMonth}'.`);
44+
}
45+
if (!new RegExp(monthRegex(), 'i').test(month)) {
46+
throw new AWSCronError(`Invalid month value '${month}'.`);
47+
}
48+
if (!new RegExp(dayOfWeekRegex(), 'i').test(dayOfWeek)) {
49+
throw new AWSCronError(`Invalid day-of-week value '${dayOfWeek}'.`);
50+
}
51+
if (!new RegExp(yearRegex()).test(year)) {
52+
throw new AWSCronError(`Invalid year value '${year}'.`);
53+
}
54+
55+
return expression;
56+
}
57+
}

0 commit comments

Comments
 (0)