-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(autoscaling): introduce Schedule classes for scaling (#2902)
Like for CloudWatch Events, introduce a class for expressing scheduling expressions for both AutoScaling and AppScaling. BREAKING CHANGES: * **autoscaling**: `schedule` is now a `Schedule` object instead of a string. * **application autoscaling**: `schedule` is now a `Schedule` object instead of a string.
- Loading branch information
Showing
16 changed files
with
277 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
157 changes: 157 additions & 0 deletions
157
packages/@aws-cdk/aws-applicationautoscaling/lib/schedule.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,157 @@ | ||
/** | ||
* 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(interval: number, unit: TimeUnit): Schedule { | ||
const unitStr = interval !== 1 ? `${unit}s` : unit; | ||
|
||
return new LiteralSchedule(`rate(${interval} ${unitStr})`); | ||
} | ||
|
||
/** | ||
* 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() { | ||
} | ||
} | ||
|
||
/** | ||
* What unit to interpret the rate in | ||
*/ | ||
export enum TimeUnit { | ||
/** | ||
* The rate is in minutes | ||
*/ | ||
Minute = 'minute', | ||
|
||
/** | ||
* The rate is in hours | ||
*/ | ||
Hour = 'hour', | ||
|
||
/** | ||
* The rate is in days | ||
*/ | ||
Day = 'day' | ||
} | ||
|
||
/** | ||
* Options to configure a cron expression | ||
* | ||
* All fields are strings so you can use complex expresions. 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; | ||
} | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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
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,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 expresions. 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; | ||
} |
Oops, something went wrong.