Skip to content

Commit d0a5ba7

Browse files
authored
Merge pull request #120 from t-richard/support-delay-queues
Support delay on queues
2 parents 29132d1 + 07d041d commit d0a5ba7

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

docs/queue.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ constructs:
299299
# The retry delay on the queue will be 10*6 => 60 seconds
300300
```
301301

302+
### Delivery delay
303+
304+
When a message is sent to the queue, it will be available immediately to the worker.
305+
306+
You can postpone the delivery of messages by a given amount of seconds using the `delay` option.
307+
308+
The maximum value is 900 seconds (15 minutes).
309+
310+
```yaml
311+
constructs:
312+
my-queue:
313+
# ...
314+
worker:
315+
handler: src/worker.handler
316+
# Messages delivery will be delayed by 1 minute
317+
delay: 60
318+
```
319+
302320
### Batch size
303321

304322
```yaml

src/constructs/aws/Queue.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const QUEUE_DEFINITION = {
3838
maximum: 10,
3939
},
4040
fifo: { type: "boolean" },
41+
delay: { type: "number" },
4142
},
4243
additionalProperties: false,
4344
required: ["worker"],
@@ -128,6 +129,18 @@ export class Queue extends AwsConstruct {
128129

129130
const maxRetries = configuration.maxRetries ?? 3;
130131

132+
let delay = undefined;
133+
if (configuration.delay !== undefined) {
134+
if (configuration.delay < 0 || configuration.delay > 900) {
135+
throw new ServerlessError(
136+
`Invalid configuration in 'constructs.${this.id}': 'delay' must be between 0 and 900, '${configuration.delay}' given.`,
137+
"LIFT_INVALID_CONSTRUCT_CONFIGURATION"
138+
);
139+
}
140+
141+
delay = Duration.seconds(configuration.delay);
142+
}
143+
131144
const baseName = `${this.provider.stackName}-${id}`;
132145

133146
const dlq = new CdkQueue(this, "Dlq", {
@@ -147,6 +160,7 @@ export class Queue extends AwsConstruct {
147160
queue: dlq,
148161
},
149162
fifo: configuration.fifo,
163+
deliveryDelay: delay,
150164
contentBasedDeduplication: configuration.fifo,
151165
});
152166

test/unit/queues.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { DeleteMessageBatchResult, ReceiveMessageResult, SendMessageBatchRe
44
import * as CloudFormationHelpers from "../../src/CloudFormation";
55
import { pluginConfigExt, runServerless } from "../utils/runServerless";
66
import { mockAws } from "../utils/mockAws";
7+
import ServerlessError from "../../src/utils/error";
78

89
describe("queues", () => {
910
afterEach(() => {
@@ -192,6 +193,49 @@ describe("queues", () => {
192193
});
193194
});
194195

196+
it("allows changing the delivery delay", async () => {
197+
const { cfTemplate, computeLogicalId } = await runServerless({
198+
fixture: "queues",
199+
configExt: merge(pluginConfigExt, {
200+
constructs: {
201+
emails: {
202+
delay: 10,
203+
},
204+
},
205+
}),
206+
command: "package",
207+
});
208+
expect(cfTemplate.Resources[computeLogicalId("emails", "Queue")]).toMatchObject({
209+
Properties: {
210+
DelaySeconds: 10,
211+
},
212+
});
213+
});
214+
215+
it("should throw an error if the delay is invalid", async () => {
216+
expect.assertions(2);
217+
218+
try {
219+
await runServerless({
220+
fixture: "queues",
221+
configExt: merge({}, pluginConfigExt, {
222+
constructs: {
223+
emails: {
224+
delay: 901,
225+
},
226+
},
227+
}),
228+
command: "package",
229+
});
230+
} catch (error) {
231+
expect(error).toBeInstanceOf(ServerlessError);
232+
expect(error).toHaveProperty(
233+
"message",
234+
"Invalid configuration in 'constructs.emails': 'delay' must be between 0 and 900, '901' given."
235+
);
236+
}
237+
});
238+
195239
it("allows defining a DLQ email alarm", async () => {
196240
const { cfTemplate, computeLogicalId } = await runServerless({
197241
fixture: "queues",

0 commit comments

Comments
 (0)