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

feat(scheduler): flexible time windows #28098

Merged
merged 20 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ff675c0
feat(scheduler): add support for flexible time window
sakurai-ryo Nov 21, 2023
85b60d4
feat(scheduler): add warning
sakurai-ryo Nov 22, 2023
5f3496b
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Nov 22, 2023
b71fd8e
feat(scheduler): fix build failed
sakurai-ryo Nov 22, 2023
70362d5
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Nov 24, 2023
21c4331
feat(scheduler): fixed for the review
sakurai-ryo Nov 24, 2023
2a2b00d
feat(scheduler): fix build
sakurai-ryo Nov 24, 2023
24cebc1
feat(scheduler): fix for the review
sakurai-ryo Nov 24, 2023
e2ce811
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Dec 6, 2023
8fc8d33
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Dec 8, 2023
443fa09
feat(scheduler): refactor class
sakurai-ryo Dec 8, 2023
9af2ec8
feat(scheduler): fix build
sakurai-ryo Dec 8, 2023
a8c279a
feat(code): delete new line
sakurai-ryo Dec 8, 2023
ba4630b
feat(scheduler): change property names
sakurai-ryo Dec 8, 2023
540561c
feat(scheduler): fix build
sakurai-ryo Dec 8, 2023
8169a6d
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Dec 13, 2023
f509ef6
feat(scheduler): fix for the review
sakurai-ryo Dec 13, 2023
461f5ba
feat(scheduler): fix tests
sakurai-ryo Dec 13, 2023
e990c2a
Merge branch 'master' of https://github.com/sakurai-ryo/aws-cdk into …
sakurai-ryo Dec 14, 2023
fb820e9
Merge branch 'main' into feat-scheduler-flex-window
kaizencc Dec 15, 2023
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
22 changes: 20 additions & 2 deletions packages/@aws-cdk/aws-scheduler-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ of millions of tasks across many AWS services without provisioning or managing u
2. **Targets**: A target is an API operation that EventBridge Scheduler calls on your behalf every time your schedule runs. EventBridge Scheduler
supports two types of targets: templated targets and universal targets. Templated targets invoke common API operations across a core groups of
services. For example, EventBridge Scheduler supports templated targets for invoking AWS Lambda Function or starting execution of Step Function state
machine. For API operations that are not supported by templated targets you can use customizeable universal targets. Universal targets support calling
machine. For API operations that are not supported by templated targets you can use customizable universal targets. Universal targets support calling
more than 6,000 API operations across over 270 AWS services.
3. **Schedule Group**: A schedule group is an Amazon EventBridge Scheduler resource that you use to organize your schedules. Your AWS account comes
with a default scheduler group. A new schedule will always be added to a scheduling group. If you do not provide a scheduling group to add to, it
Expand Down Expand Up @@ -143,7 +143,7 @@ new Schedule(this, 'Schedule', {

The `@aws-cdk/aws-scheduler-targets-alpha` module includes classes that implement the `IScheduleTarget` interface for
various AWS services. EventBridge Scheduler supports two types of targets: templated targets invoke common API
operations across a core groups of services, and customizeable universal targets that you can use to call more
operations across a core groups of services, and customizable universal targets that you can use to call more
than 6,000 operations across over 270 services. A list of supported targets can be found at `@aws-cdk/aws-scheduler-targets-alpha`.

### Input
Expand Down Expand Up @@ -227,6 +227,24 @@ const schedule = new Schedule(this, 'Schedule', {

> Visit [Data protection in Amazon EventBridge Scheduler](https://docs.aws.amazon.com/scheduler/latest/UserGuide/data-protection.html) for more details.

## Configuring flexible time window

You can configure flexible time windows by specifying the `flexibleTimeWindowMode` and `maximumWindowInMinutes` properties.
By default, the `flexibleTimeWindowMode` is set to `OFF` and this feature is disabled.

```ts
declare const target: targets.LambdaInvoke;

const schedule = new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(Duration.hours(12)),
target,
flexibleTimeWindowMode: FlexibleTimeWindowMode.FLEXIBLE,
maximumWindowInMinutes: Duration.minutes(10),
});
```

> Visit [Configuring flexible time windows](https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-schedule-flexible-time-windows.html) for more details.

## Error-handling

You can configure how your schedule handles failures, when EventBridge Scheduler is unable to deliver an event
Expand Down
61 changes: 59 additions & 2 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ export interface ScheduleTargetProps {
readonly retryAttempts?: number;
}

/**
* FlexibleTimeWindow mode for the schedule.
*/
export enum FlexibleTimeWindowMode {
/**
* FlexibleTimeWindow is disabled.
*/
OFF = 'OFF',

/**
* FlexibleTimeWindow is enabled.
*/
FLEXIBLE = 'FLEXIBLE'
}

/**
* Construction properties for `Schedule`.
*/
Expand Down Expand Up @@ -104,6 +119,7 @@ export interface ScheduleProps {

/**
* Indicates whether the schedule is enabled.
*
* @default true
*/
readonly enabled?: boolean;
Expand All @@ -114,6 +130,24 @@ export interface ScheduleProps {
* @default - All events in Scheduler are encrypted with a key that AWS owns and manages.
*/
readonly key?: kms.IKey;

/**
* Determines whether the schedule is invoked within a flexible time window.
*
* @see https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-schedule-flexible-time-windows.html
*
* @default - FlexibleTimeWindowMode.OFF
*/
readonly flexibleTimeWindowMode?: FlexibleTimeWindowMode;

/**
* The maximum time window during which the schedule can be invoked.
*
* Must be between 1 to 1440 minutes.
*
* @default - Required if flexibleTimeWindowMode is FLEXIBLE.
*/
readonly maximumWindowInMinutes?: Duration;
}

/**
Expand Down Expand Up @@ -256,7 +290,7 @@ export class Schedule extends Resource implements ISchedule {

const resource = new CfnSchedule(this, 'Resource', {
name: this.physicalName,
flexibleTimeWindow: { mode: 'OFF' },
flexibleTimeWindow: this.renderFlexibleTimeWindow(props.flexibleTimeWindowMode, props.maximumWindowInMinutes),
scheduleExpression: props.schedule.expressionString,
scheduleExpressionTimezone: props.schedule.timeZone?.timezoneName,
groupName: this.group?.groupName,
Expand Down Expand Up @@ -306,4 +340,27 @@ export class Schedule extends Resource implements ISchedule {
const isEmptyPolicy = Object.values(policy).every(value => value === undefined);
return !isEmptyPolicy ? policy : undefined;
}
}

private renderFlexibleTimeWindow(
flexibleTimeWindowMode?: FlexibleTimeWindowMode, maximumWindowInMinutes?: Duration,
): CfnSchedule.FlexibleTimeWindowProperty {
const mode = flexibleTimeWindowMode ?? FlexibleTimeWindowMode.OFF;

if (mode === FlexibleTimeWindowMode.OFF) {
return {
mode,
};
}

if (!maximumWindowInMinutes) {
throw new Error('maximumWindowInMinutes must be provided when flexibleTimeWindowMode is set to FLEXIBLE');
}
if (maximumWindowInMinutes.toMinutes() < 1 || maximumWindowInMinutes.toMinutes() > 1440) {
throw new Error(`maximumWindowInMinutes must be between 1 and 1440, got ${maximumWindowInMinutes.toMinutes()}`);
}
return {
mode,
maximumWindowInMinutes: maximumWindowInMinutes.toMinutes(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as targets from '@aws-cdk/aws-scheduler-targets-alpha';
import { App, Stack, TimeZone, Duration } from 'aws-cdk-lib';
import { ScheduleExpression, ScheduleTargetInput, ContextAttribute, Group, Schedule } from '@aws-cdk/aws-scheduler-alpha';
import { ScheduleExpression, ScheduleTargetInput, ContextAttribute, Group, Schedule, FlexibleTimeWindowMode } from '@aws-cdk/aws-scheduler-alpha';

class Fixture extends cdk.Stack {
constructor(scope: Construct, id: string) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,37 @@
}
}
}
},
"UseFlexibleTimeWindowBF55D3ED": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"FlexibleTimeWindow": {
"MaximumWindowInMinutes": 10,
"Mode": "FLEXIBLE"
},
"ScheduleExpression": "rate(12 hours)",
"ScheduleExpressionTimezone": "Etc/UTC",
"State": "ENABLED",
"Target": {
"Arn": {
"Fn::GetAtt": [
"Function76856677",
"Arn"
]
},
"Input": "\"Input Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 180,
"MaximumRetryAttempts": 3
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
"Arn"
]
}
}
}
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading