Skip to content

Commit 8bda729

Browse files
committed
update scheduler with async calls
1 parent c1e21cb commit 8bda729

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/scheduler.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ const LOGGER = console;
1818
* @param handlerRequest additional context which the handler can provide itself
1919
* for re-invocation
2020
*/
21-
export function rescheduleAfterMinutes(
21+
export const rescheduleAfterMinutes = async (
2222
session: SessionProxy,
2323
functionArn: string,
2424
minutesFromNow: number,
2525
handlerRequest: HandlerRequest,
26-
): void {
26+
): Promise<void> => {
2727
const client: CloudWatchEvents = session.client('CloudWatchEvents') as CloudWatchEvents;
2828
const cron = minToCron(Math.max(minutesFromNow, 1));
2929
const identifier = uuidv4();
@@ -33,19 +33,19 @@ export function rescheduleAfterMinutes(
3333
handlerRequest.requestContext.cloudWatchEventsTargetId = targetId;
3434
const jsonRequest = JSON.stringify(handlerRequest);
3535
LOGGER.debug(`Scheduling re-invoke at ${cron} (${identifier})`);
36-
client.putRule({
36+
await client.putRule({
3737
Name: ruleName,
3838
ScheduleExpression: cron,
3939
State: 'ENABLED',
40-
});
41-
client.putTargets({
40+
}).promise();
41+
await client.putTargets({
4242
Rule: ruleName,
4343
Targets: [{
4444
Id: targetId,
4545
Arn: functionArn,
4646
Input: jsonRequest,
4747
}],
48-
});
48+
}).promise();
4949
}
5050

5151
/**
@@ -56,16 +56,16 @@ export function rescheduleAfterMinutes(
5656
* @param ruleName the name of the CWE rule which triggered a re-invocation
5757
* @param targetId the target of the CWE rule which triggered a re-invocation
5858
*/
59-
export function cleanupCloudwatchEvents(
59+
export const cleanupCloudwatchEvents = async (
6060
session: SessionProxy, ruleName: string, targetId: string
61-
): void {
61+
): Promise<void> => {
6262
const client: CloudWatchEvents = session.client('CloudWatchEvents') as CloudWatchEvents;
6363
try {
6464
if (targetId && ruleName) {
65-
client.removeTargets({
65+
await client.removeTargets({
6666
Rule: ruleName,
6767
Ids: [targetId],
68-
});
68+
}).promise();
6969
}
7070
} catch(err) {
7171
LOGGER.error(
@@ -75,10 +75,10 @@ export function cleanupCloudwatchEvents(
7575

7676
try {
7777
if (ruleName) {
78-
client.deleteRule({
78+
await client.deleteRule({
7979
Name: ruleName,
8080
Force: true,
81-
});
81+
}).promise();
8282
}
8383
} catch(err) {
8484
LOGGER.error(

tests/lib/scheduler.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,28 @@ describe('when getting scheduler', () => {
7171
jest.restoreAllMocks();
7272
});
7373

74-
test('reschedule after minutes zero', () => {
74+
test('reschedule after minutes zero', async () => {
7575
// if called with zero, should call cron with a 1
76-
rescheduleAfterMinutes(session, 'arn:goes:here', 0, handlerRequest);
76+
await rescheduleAfterMinutes(session, 'arn:goes:here', 0, handlerRequest);
7777

7878
expect(cwEvents).toHaveBeenCalledTimes(1);
7979
expect(cwEvents).toHaveBeenCalledWith('CloudWatchEvents');
8080
expect(spyMinToCron).toHaveBeenCalledTimes(1);
8181
expect(spyMinToCron).toHaveBeenCalledWith(1);
8282
});
8383

84-
test('reschedule after minutes not zero', () => {
84+
test('reschedule after minutes not zero', async () => {
8585
// if called with another number, should use that
86-
rescheduleAfterMinutes(session, 'arn:goes:here', 2, handlerRequest);
86+
await rescheduleAfterMinutes(session, 'arn:goes:here', 2, handlerRequest);
8787

8888
expect(cwEvents).toHaveBeenCalledTimes(1);
8989
expect(cwEvents).toHaveBeenCalledWith('CloudWatchEvents');
9090
expect(spyMinToCron).toHaveBeenCalledTimes(1);
9191
expect(spyMinToCron).toHaveBeenCalledWith(2);
9292
});
9393

94-
test('reschedule after minutes success', () => {
95-
rescheduleAfterMinutes(session, 'arn:goes:here', 2, handlerRequest);
94+
test('reschedule after minutes success', async () => {
95+
await rescheduleAfterMinutes(session, 'arn:goes:here', 2, handlerRequest);
9696

9797
expect(cwEvents).toHaveBeenCalledTimes(1);
9898
expect(cwEvents).toHaveBeenCalledWith('CloudWatchEvents');
@@ -115,9 +115,9 @@ describe('when getting scheduler', () => {
115115
});
116116
});
117117

118-
test('cleanup cloudwatch events empty', () => {
118+
test('cleanup cloudwatch events empty', async () => {
119119
// cleanup should silently pass if rule/target are empty
120-
cleanupCloudwatchEvents(session, '', '');
120+
await cleanupCloudwatchEvents(session, '', '');
121121

122122
expect(cwEvents).toHaveBeenCalledTimes(1);
123123
expect(cwEvents).toHaveBeenCalledWith('CloudWatchEvents');
@@ -126,10 +126,10 @@ describe('when getting scheduler', () => {
126126
expect(spyConsoleError).toHaveBeenCalledTimes(0);
127127
});
128128

129-
test('cleanup cloudwatch events success', () => {
129+
test('cleanup cloudwatch events success', async () => {
130130
// when rule_name and target_id are provided we should call events client and not
131131
// log errors if the deletion succeeds
132-
cleanupCloudwatchEvents(session, 'rulename', 'targetid');
132+
await cleanupCloudwatchEvents(session, 'rulename', 'targetid');
133133

134134
expect(spyConsoleError).toHaveBeenCalledTimes(0);
135135
expect(cwEvents).toHaveBeenCalledTimes(1);
@@ -140,13 +140,13 @@ describe('when getting scheduler', () => {
140140
expect(spyConsoleError).toHaveBeenCalledTimes(0);
141141
});
142142

143-
test('cleanup cloudwatch events client error', () => {
143+
test('cleanup cloudwatch events client error', async () => {
144144
// cleanup should catch and log client failures
145145
const error = awsUtil.error(new Error(), { code: '1' });
146146
mockRemoveTargets.mockImplementation(() => {throw error});
147147
mockDeleteRule.mockImplementation(() => {throw error});
148148

149-
cleanupCloudwatchEvents(session, 'rulename', 'targetid');
149+
await cleanupCloudwatchEvents(session, 'rulename', 'targetid');
150150

151151
expect(cwEvents).toHaveBeenCalledTimes(1);
152152
expect(cwEvents).toHaveBeenCalledWith('CloudWatchEvents');

0 commit comments

Comments
 (0)