-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathschedule.ts
159 lines (136 loc) · 4.02 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Duration } from '@aws-cdk/core';
/**
* 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 Application AutoScaling will recognize
*/
public static expression(expression: string): Schedule {
return new LiteralSchedule(expression);
}
/**
* Construct a schedule from an interval and a time unit
*/
public static rate(duration: Duration): Schedule {
if (duration.toSeconds() === 0) {
throw new Error('Duration cannot be 0');
}
let rate = maybeRate(duration.toDays({ integral: false }), 'day');
if (rate === undefined) { rate = maybeRate(duration.toHours({ integral: false }), 'hour'); }
if (rate === undefined) { rate = makeRate(duration.toMinutes({ integral: true }), 'minute'); }
return new LiteralSchedule(rate);
}
/**
* Construct a Schedule from a moment in time
*/
public static at(moment: Date): Schedule {
return new LiteralSchedule(`at(${formatISO(moment)})`);
}
/**
* 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 year = fallback(options.year, '*');
// Weekday defaults to '?' if not supplied. If it is supplied, day must become '?'
const day = fallback(options.day, options.weekDay !== undefined ? '?' : '*');
const weekDay = fallback(options.weekDay, '?');
return new LiteralSchedule(`cron(${minute} ${hour} ${day} ${month} ${weekDay} ${year})`);
}
/**
* 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 https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions
*/
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 year to run this rule at
*
* @default - Every year
*/
readonly year?: 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;
}
function formatISO(date?: Date) {
if (!date) { return undefined; }
return date.getUTCFullYear() +
'-' + pad(date.getUTCMonth() + 1) +
'-' + pad(date.getUTCDate()) +
'T' + pad(date.getUTCHours()) +
':' + pad(date.getUTCMinutes()) +
':' + pad(date.getUTCSeconds());
function pad(num: number) {
if (num < 10) {
return '0' + num;
}
return num;
}
}
/**
* Return the rate if the rate is whole number
*/
function maybeRate(interval: number, singular: string) {
if (interval === 0 || !Number.isInteger(interval)) { return undefined; }
return makeRate(interval, singular);
}
/**
* Return 'rate(${interval} ${singular}(s))` for the interval
*/
function makeRate(interval: number, singular: string) {
return interval === 1 ? `rate(1 ${singular})` : `rate(${interval} ${singular}s)`;
}