Skip to content

Commit cdcded2

Browse files
TaraGargsdnts
authored andcommitted
feat(wrangler): allow overriding message retention period when creating a queue
1 parent 0fe9ea6 commit cdcded2

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

.changeset/gentle-tables-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Allow overriding message retention duration when creating Queues

packages/wrangler/src/__tests__/queues.test.ts

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ describe("wrangler", () => {
196196
describe("create", () => {
197197
function mockCreateRequest(
198198
queueName: string,
199-
queueSettings: { delivery_delay?: number } | undefined = undefined
199+
queueSettings:
200+
| { delivery_delay?: number; message_retention_period?: number }
201+
| undefined = undefined
200202
) {
201203
const requests = { count: 0 };
202204

@@ -210,6 +212,7 @@ describe("wrangler", () => {
210212
queue_name: string;
211213
settings: {
212214
delivery_delay: number;
215+
message_retention_period: number;
213216
};
214217
};
215218
expect(body.queue_name).toEqual(queueName);
@@ -249,7 +252,8 @@ describe("wrangler", () => {
249252
-v, --version Show version number [boolean]
250253
251254
OPTIONS
252-
--delivery-delay-secs How long a published message should be delayed for, in seconds. Must be a positive integer [number]"
255+
--delivery-delay-secs How long a published message should be delayed for, in seconds. Must be between 0 and 42300 [number]
256+
--message-retention-period-secs How long to retain a message in the queue, in seconds. Must be between 60 and 1209600 [number]"
253257
`);
254258
});
255259
describe.each(["wrangler.json", "wrangler.toml"])("%s", (configPath) => {
@@ -301,18 +305,18 @@ describe("wrangler", () => {
301305
runWrangler(`queues create ${queueName}`)
302306
).rejects.toThrowError();
303307
expect(std.out).toMatchInlineSnapshot(`
304-
"🌀 Creating queue 'testQueue'
305-
Queues is not currently enabled on this account. Go to https://dash.cloudflare.com/some-account-id/workers/queues to enable it.
308+
"🌀 Creating queue 'testQueue'
309+
Queues is not currently enabled on this account. Go to https://dash.cloudflare.com/some-account-id/workers/queues to enable it.
306310
307-
[31mX [41;31m[[41;97mERROR[41;31m][0m [1mA request to the Cloudflare API (/accounts/some-account-id/queues) failed.[0m
311+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1mA request to the Cloudflare API (/accounts/some-account-id/queues) failed.[0m
308312
309-
workers.api.error.unauthorized [code: 10023]
313+
workers.api.error.unauthorized [code: 10023]
310314
311-
If you think this is a bug, please open an issue at:
312-
[4mhttps://github.com/cloudflare/workers-sdk/issues/new/choose[0m
315+
If you think this is a bug, please open an issue at:
316+
[4mhttps://github.com/cloudflare/workers-sdk/issues/new/choose[0m
313317
314-
"
315-
`);
318+
"
319+
`);
316320
});
317321

318322
it("should show an error when two delivery delays are set", async () => {
@@ -339,6 +343,76 @@ describe("wrangler", () => {
339343

340344
expect(requests.count).toEqual(0);
341345
});
346+
347+
it("should send queue settings with message retention period", async () => {
348+
const requests = mockCreateRequest("testQueue", {
349+
message_retention_period: 100,
350+
});
351+
await runWrangler(
352+
"queues create testQueue --message-retention-period-secs=100"
353+
);
354+
expect(std.out).toMatchInlineSnapshot(`
355+
"🌀 Creating queue 'testQueue'
356+
✅ Created queue 'testQueue'
357+
358+
Configure your Worker to send messages to this queue:
359+
360+
{
361+
\\"queues\\": {
362+
\\"producers\\": [
363+
{
364+
\\"queue\\": \\"testQueue\\",
365+
\\"binding\\": \\"testQueue\\"
366+
}
367+
]
368+
}
369+
}
370+
Configure your Worker to consume messages from this queue:
371+
372+
{
373+
\\"queues\\": {
374+
\\"consumers\\": [
375+
{
376+
\\"queue\\": \\"testQueue\\"
377+
}
378+
]
379+
}
380+
}"
381+
`);
382+
expect(requests.count).toEqual(1);
383+
});
384+
385+
it("should show an error when two message retention periods are set", async () => {
386+
const requests = mockCreateRequest("testQueue", {
387+
message_retention_period: 60,
388+
});
389+
390+
await expect(
391+
runWrangler(
392+
"queues create testQueue --message-retention-period-secs=70 --message-retention-period-secs=80"
393+
)
394+
).rejects.toThrowErrorMatchingInlineSnapshot(
395+
`[Error: Cannot specify --message-retention-period-secs multiple times]`
396+
);
397+
398+
expect(requests.count).toEqual(0);
399+
});
400+
401+
it("should show an error when invalid message retention period is set", async () => {
402+
const requests = mockCreateRequest("testQueue", {
403+
message_retention_period: 100,
404+
});
405+
await expect(
406+
runWrangler(
407+
"queues create testQueue --message-retention-period-secs=0"
408+
)
409+
).rejects.toThrowErrorMatchingInlineSnapshot(
410+
`[Error: Invalid --message-retention-period-secs value: 0. Must be between 60 and 1209600]`
411+
);
412+
413+
expect(requests.count).toEqual(0);
414+
});
415+
});
342416
});
343417

344418
describe("delete", () => {

packages/wrangler/src/queues/cli/commands/create.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { getValidBindingName } from "../../../utils/getValidBindingName";
66
import { createQueue } from "../../client";
77
import {
88
MAX_DELIVERY_DELAY_SECS,
9+
MAX_MESSAGE_RETENTION_PERIOD_SECS,
910
MIN_DELIVERY_DELAY_SECS,
11+
MIN_MESSAGE_RETENTION_PERIOD_SECS,
1012
} from "../../constants";
1113
import { handleFetchError } from "../../utils";
1214
import type {
@@ -29,6 +31,12 @@ export function options(yargs: CommonYargsArgv) {
2931
"How long a published message should be delayed for, in seconds. Must be between 0 and 42300",
3032
default: 0,
3133
},
34+
"message-retention-period-secs": {
35+
type: "number",
36+
describe:
37+
"How long to retain a message in the queue, in seconds. Must be between 60 and 1209600",
38+
default: 345600,
39+
},
3240
});
3341
}
3442

@@ -45,6 +53,12 @@ function createBody(
4553
);
4654
}
4755

56+
if (Array.isArray(args.messageRetentionPeriodSecs)) {
57+
throw new CommandLineArgsError(
58+
"Cannot specify --message-retention-period-secs multiple times"
59+
);
60+
}
61+
4862
body.settings = {};
4963

5064
if (args.deliveryDelaySecs != undefined) {
@@ -59,6 +73,18 @@ function createBody(
5973
body.settings.delivery_delay = args.deliveryDelaySecs;
6074
}
6175

76+
if (args.messageRetentionPeriodSecs != undefined) {
77+
if (
78+
args.messageRetentionPeriodSecs < MIN_MESSAGE_RETENTION_PERIOD_SECS ||
79+
args.messageRetentionPeriodSecs > MAX_MESSAGE_RETENTION_PERIOD_SECS
80+
) {
81+
throw new CommandLineArgsError(
82+
`Invalid --message-retention-period-secs value: ${args.messageRetentionPeriodSecs}. Must be between ${MIN_MESSAGE_RETENTION_PERIOD_SECS} and ${MAX_MESSAGE_RETENTION_PERIOD_SECS}`
83+
);
84+
}
85+
body.settings.message_retention_period = args.messageRetentionPeriodSecs;
86+
}
87+
6288
if (Object.keys(body.settings).length === 0) {
6389
body.settings = undefined;
6490
}

packages/wrangler/src/queues/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface WorkerService {
1919

2020
export interface QueueSettings {
2121
delivery_delay?: number;
22+
message_retention_period?: number;
2223
}
2324

2425
export interface PostQueueResponse {

packages/wrangler/src/queues/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ export const INVALID_QUEUE_SETTINGS_ERROR = 100128;
33

44
export const MIN_DELIVERY_DELAY_SECS = 0;
55
export const MAX_DELIVERY_DELAY_SECS = 42300;
6+
7+
export const MIN_MESSAGE_RETENTION_PERIOD_SECS = 60;
8+
export const MAX_MESSAGE_RETENTION_PERIOD_SECS = 1209600;

0 commit comments

Comments
 (0)