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): start and end time for schedule construct #28306

Merged
merged 16 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ new Schedule(this, 'Schedule', {
});
```

### Configuring a start and end time of the Schedule

If you choose a recurring schedule, you can set the start and end time of the Schedule by specifying the `start` and `end`.

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

new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(cdk.Duration.hours(12)),
target: target,
start: new Date('2023-01-01T00:00:00.000Z'),
end: new Date('2023-02-01T00:00:00.000Z'),
});
```

## Scheduler Targets

Expand Down
28 changes: 27 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ export interface ScheduleProps {
* @default - All events in Scheduler are encrypted with a key that AWS owns and manages.
*/
readonly key?: kms.IKey;

/**
* The date, in UTC, after which the schedule can begin invoking its target.
* EventBridge Scheduler ignores start for one-time schedules.
*
* @default - no value
*/
readonly start?: Date;

/**
* The date, in UTC, before which the schedule can invoke its target.
* EventBridge Scheduler ignores end for one-time schedules.
*
* @default - no value
*/
readonly end?: Date;
}

/**
Expand Down Expand Up @@ -254,6 +270,8 @@ export class Schedule extends Resource implements ISchedule {

this.retryPolicy = targetConfig.retryPolicy;

this.validateTimeFrame(props.start, props.end);

const resource = new CfnSchedule(this, 'Resource', {
name: this.physicalName,
flexibleTimeWindow: { mode: 'OFF' },
Expand All @@ -276,6 +294,8 @@ export class Schedule extends Resource implements ISchedule {
sageMakerPipelineParameters: targetConfig.sageMakerPipelineParameters,
sqsParameters: targetConfig.sqsParameters,
},
startDate: props.start?.toISOString(),
endDate: props.end?.toISOString(),
});

this.scheduleName = this.getResourceNameAttribute(resource.ref);
Expand Down Expand Up @@ -306,4 +326,10 @@ export class Schedule extends Resource implements ISchedule {
const isEmptyPolicy = Object.values(policy).every(value => value === undefined);
return !isEmptyPolicy ? policy : undefined;
}
}

private validateTimeFrame(start?: Date, end?: Date) {
if (start && end && start >= end) {
throw new Error(`start must precede end, got start: ${start.toISOString()}, end: ${end.toISOString()}`);
}
}
}

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,38 @@
}
}
}
},
"ScheduleWithTimeFrameC1C8BDCC": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"EndDate": "2025-10-01T00:00:00.000Z",
"FlexibleTimeWindow": {
"Mode": "OFF"
},
"ScheduleExpression": "rate(12 hours)",
"ScheduleExpressionTimezone": "Etc/UTC",
"StartDate": "2024-04-15T06:30:00.000Z",
"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.

8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ new scheduler.Schedule(stack, 'CustomerKmsSchedule', {
key,
});

const currentYear = new Date().getFullYear();
new scheduler.Schedule(stack, 'ScheduleWithTimeFrame', {
schedule: expression,
target: target,
start: new Date(`${currentYear + 1}-04-15T06:30:00.000Z`),
end: new Date(`${currentYear + 2}-10-01T00:00:00.000Z`),
});

new IntegTest(app, 'integtest-schedule', {
testCases: [stack],
});
Expand Down
35 changes: 34 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,37 @@ describe('Schedule', () => {
},
});
});
});

describe('schedule timeFrame', () => {
test.each([
{ StartDate: '2023-04-15T06:20:00.000Z', EndDate: '2023-10-01T00:00:00.000Z' },
{ StartDate: '2023-04-15T06:25:00.000Z' },
{ EndDate: '2023-10-01T00:00:00.000Z' },
])('schedule can set start and end', (timeFrame) => {
new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, role),
start: timeFrame.StartDate ? new Date(timeFrame.StartDate) : undefined,
end: timeFrame.EndDate ? new Date(timeFrame.EndDate) : undefined,
});

Template.fromStack(stack).hasResourceProperties('AWS::Scheduler::Schedule', {
...timeFrame,
});
});

test.each([
{ start: '2023-10-01T00:00:00.000Z', end: '2023-10-01T00:00:00.000Z' },
{ start: '2023-10-01T00:00:00.000Z', end: '2023-09-01T00:00:00.000Z' },
])('throw error when start does not come before end', ({ start, end }) => {
expect(() => {
new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, role),
start: new Date(start),
end: new Date(end),
});
}).toThrow(`start must precede end, got start: ${start}, end: ${end}`);
});
});
});