forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integ.schedule.ts
113 lines (99 loc) · 3.85 KB
/
integ.schedule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/// !cdk-integ aws-cdk-scheduler-schedule
import { ExpectedResult, IntegTest, Match } from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as scheduler from '../lib';
class SomeSqsTarget implements scheduler.IScheduleTarget {
public constructor(
private readonly queue: sqs.IQueue,
private readonly role: iam.IRole,
private readonly input?: scheduler.ScheduleTargetInput) {
}
public bind(): scheduler.ScheduleTargetConfig {
return {
arn: this.queue.queueArn,
role: this.role,
input: this.input,
retryPolicy: {
maximumEventAgeInSeconds: 180,
maximumRetryAttempts: 3,
},
};
}
}
const app = new cdk.App();
const stack = new cdk.Stack(app, 'aws-cdk-scheduler-schedule');
const queue = new sqs.Queue(stack, 'ScheduleTargetQueue');
const role = new iam.Role(stack, 'Role', {
assumedBy: new iam.ServicePrincipal('scheduler.amazonaws.com'),
});
queue.grantSendMessages(role);
new scheduler.Schedule(stack, 'ScheduleToSendMessageToQueue', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
description: 'test description from the ScheduleToSendMessageToQueue',
target: new SomeSqsTarget(queue, role,
scheduler.ScheduleTargetInput.fromText(`valueA-${stack.region}`)),
});
const key = new kms.Key(stack, 'ScheduleKey');
new scheduler.Schedule(stack, 'ScheduleWithCMK', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: new SomeSqsTarget(queue, role,
scheduler.ScheduleTargetInput.fromText(`valueB-${stack.region}`)),
key,
});
const namedGroup = new scheduler.Group(stack, 'NamedGroup', {
groupName: 'TestGroup',
});
namedGroup.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
const unnamedGroup = new scheduler.Group(stack, 'UnnamedGroup', {});
unnamedGroup.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);
const randomTargetThatWontGetInvoked = new SomeSqsTarget(queue, role);
new scheduler.Schedule(stack, 'ScheduleToTestWithNamedGroup', {
schedule: scheduler.ScheduleExpression.at(new Date('2060-04-15T06:30:00.000Z')),
target: randomTargetThatWontGetInvoked,
group: namedGroup,
});
new scheduler.Schedule(stack, 'ScheduleToTestWithUnnamedGroup', {
schedule: scheduler.ScheduleExpression.at(new Date('2060-04-15T06:30:00.000Z')),
target: randomTargetThatWontGetInvoked,
group: unnamedGroup,
});
new scheduler.Schedule(stack, 'TestDisabledSchedule', {
schedule: scheduler.ScheduleExpression.at(new Date('2060-04-15T06:30:00.000Z')),
target: randomTargetThatWontGetInvoked,
enabled: false,
});
new scheduler.Schedule(stack, 'UseFlexibleTimeWindow', {
schedule: scheduler.ScheduleExpression.at(new Date('2060-04-15T06:30:00.000Z')),
target: randomTargetThatWontGetInvoked,
timeWindow: scheduler.TimeWindow.flexible(cdk.Duration.minutes(10)),
});
new scheduler.Schedule(stack, 'ScheduleWithTimeFrame', {
schedule: scheduler.ScheduleExpression.rate(cdk.Duration.minutes(1)),
target: randomTargetThatWontGetInvoked,
start: new Date('2060-04-15T06:30:00.000Z'),
end: new Date('2060-10-01T00:00:00.000Z'),
});
new cloudwatch.Alarm(stack, 'AllSchedulerErrorsAlarm', {
metric: scheduler.Schedule.metricAllErrors(),
threshold: 1,
evaluationPeriods: 1,
});
const integ = new IntegTest(app, 'integtest-schedule', {
testCases: [stack],
});
integ.assertions.awsApiCall('SQS', 'receiveMessage', {
QueueUrl: queue.queueUrl,
MaxNumberOfMessages: 10,
}).expect(ExpectedResult.objectLike({
Messages: Match.arrayWith([
Match.objectLike({ Body: `valueA-${stack.region}` }),
Match.objectLike({ Body: `valueB-${stack.region}` }),
]),
})).waitForAssertions({
totalTimeout: cdk.Duration.minutes(5),
interval: cdk.Duration.seconds(20),
});