Skip to content

Commit

Permalink
LiteralSchedule extending consumer schedule for public rate method
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-wilton committed Sep 23, 2023
1 parent ab52bb5 commit 9795d1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
13 changes: 12 additions & 1 deletion packages/aws-cdk-lib/aws-applicationautoscaling/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export abstract class Schedule extends CoreSchedule {
* Construct a schedule from an interval and a time unit. Must be a whole number of seconds.
*/
public static rate(duration: Duration): Schedule {
return super.protectedRate(duration);
return new LiteralSchedule(super.protectedRate(duration));
}

/**
Expand Down Expand Up @@ -45,3 +45,14 @@ export abstract class Schedule extends CoreSchedule {
* @deprecated use core.CronOptions instead
*/
export interface CronOptions extends CoreCronOptions {}

class LiteralSchedule extends Schedule {
constructor(
public readonly expressionString: string,
public readonly timeZone?: TimeZone,
) {
super();
}

public _bind() {}
}
15 changes: 13 additions & 2 deletions packages/aws-cdk-lib/aws-events/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duration, Schedule as CoreSchedule } from '../../core';
import { Duration, Schedule as CoreSchedule, TimeZone } from '../../core';

/**
* Schedule for scheduled event rules
Expand All @@ -23,7 +23,7 @@ export abstract class Schedule extends CoreSchedule {
* Rates may be defined with any unit of time, but when converted into minutes, the duration must be a positive whole number of minutes.
*/
public static rate(duration: Duration): Schedule {
return super.protectedRate(duration);
return new LiteralSchedule(super.protectedRate(duration));
}

/**
Expand All @@ -34,6 +34,17 @@ export abstract class Schedule extends CoreSchedule {
}
}

class LiteralSchedule extends Schedule {
constructor(
public readonly expressionString: string,
public readonly timeZone?: TimeZone,
) {
super();
}

public _bind() {}
}

/**
* Options to configure a cron expression
*
Expand Down
6 changes: 3 additions & 3 deletions packages/aws-cdk-lib/core/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ export abstract class Schedule {
*
* Rates may be defined with any unit of time, but when converted into minutes, the duration must be a positive whole number of minutes.
*/
protected static protectedRate(duration: Duration): Schedule {
protected static protectedRate(duration: Duration): string {
if (duration.isUnresolved()) {
const validDurationUnit = ['minute', 'minutes', 'hour', 'hours', 'day', 'days'];
if (validDurationUnit.indexOf(duration.unitLabel()) === -1) {
throw new Error("Allowed units for scheduling are: 'minute', 'minutes', 'hour', 'hours', 'day', 'days'");
}
return new LiteralSchedule(`rate(${duration.formatTokenToNumber()})`);
return `rate(${duration.formatTokenToNumber()})`;
}
if (duration.toMinutes() === 0) {
throw new Error('Duration cannot be 0');
Expand All @@ -59,7 +59,7 @@ export abstract class Schedule {
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);
return rate;
}

/**
Expand Down

0 comments on commit 9795d1b

Please sign in to comment.