Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(autoscaling): introduce Schedule classes for scaling #2902

Merged
merged 3 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions packages/@aws-cdk/aws-applicationautoscaling/lib/schedule.ts
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) {
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;
}
}
96 changes: 96 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* 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, '*');

// 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(`${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;
}