-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathschedule.ts
94 lines (82 loc) · 2.14 KB
/
schedule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Schedule for scheduled scaling actions
*/
export abstract class Schedule {
/**
* Construct a schedule from a literal schedule expression
*
* @param expression The expression to use. Must be in a format that AutoScaling will recognize
* @see http://crontab.org/
*/
public static expression(expression: string): Schedule {
return new LiteralSchedule(expression);
}
/**
* Create a schedule from a set of cron fields
*/
public static cron(options: CronOptions): Schedule {
if (options.weekDay !== undefined && options.day !== undefined) {
throw new Error('Cannot supply both \'day\' and \'weekDay\', use at most one');
}
const minute = fallback(options.minute, '*');
const hour = fallback(options.hour, '*');
const month = fallback(options.month, '*');
const day = fallback(options.day, '*');
const weekDay = fallback(options.weekDay, '*');
return new LiteralSchedule(`${minute} ${hour} ${day} ${month} ${weekDay}`);
}
/**
* Retrieve the expression for this schedule
*/
public abstract readonly expressionString: string;
protected constructor() {
}
}
/**
* Options to configure a cron expression
*
* All fields are strings so you can use complex expressions. Absence of
* a field implies '*' or '?', whichever one is appropriate.
*
* @see http://crontab.org/
*/
export interface CronOptions {
/**
* The minute to run this rule at
*
* @default - Every minute
*/
readonly minute?: string;
/**
* The hour to run this rule at
*
* @default - Every hour
*/
readonly hour?: string;
/**
* The day of the month to run this rule at
*
* @default - Every day of the month
*/
readonly day?: string;
/**
* The month to run this rule at
*
* @default - Every month
*/
readonly month?: string;
/**
* The day of the week to run this rule at
*
* @default - Any day of the week
*/
readonly weekDay?: string;
}
class LiteralSchedule extends Schedule {
constructor(public readonly expressionString: string) {
super();
}
}
function fallback<T>(x: T | undefined, def: T): T {
return x === undefined ? def : x;
}