Skip to content

Commit

Permalink
fix: use cli script-name arg when deploying a worker with queue consu…
Browse files Browse the repository at this point in the history
…mers (#6263)
  • Loading branch information
petebacondarwin authored Jul 15, 2024
1 parent 8bbd824 commit fa1016c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-beans-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
"wrangler": patch
---

fix: use cli script-name arg when deploying a worker with queue consumers
56 changes: 56 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9346,6 9346,62 @@ export default{
`);
});

it("should post worker queue consumers on deploy, using command line script name arg", async () => {
const expectedScriptName = "command-line-arg-script-name";
writeWranglerToml({
queues: {
consumers: [
{
queue: queueName,
dead_letter_queue: "myDLQ",
max_batch_size: 5,
max_batch_timeout: 3,
max_retries: 10,
retry_delay: 5,
},
],
},
});
await fs.promises.writeFile("index.js", `export default {};`);
mockSubDomainRequest();
mockUploadWorkerRequest({ expectedScriptName });
const existingQueue: QueueResponse = {
queue_id: queueId,
queue_name: queueName,
created_on: "",
producers: [],
consumers: [],
producers_total_count: 0,
consumers_total_count: 0,
modified_on: "",
};
mockGetQueueByName(queueName, existingQueue);
mockPostConsumerById(queueId, {
dead_letter_queue: "myDLQ",
type: "worker",
script_name: expectedScriptName,
settings: {
batch_size: 5,
max_retries: 10,
max_wait_time_ms: 3000,
retry_delay: 5,
},
});
await runWrangler(`deploy index.js --name ${expectedScriptName}`);
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Uploaded command-line-arg-script-name (TIMINGS)
Published command-line-arg-script-name (TIMINGS)
https://command-line-arg-script-name.test-sub-domain.workers.dev
Consumer for queue1
Current Deployment ID: Galaxy-Class
Current Version ID: Galaxy-Class
Note: Deployment ID has been renamed to Version ID. Deployment ID is present to maintain compatibility with the previous behavior of this command. This output will change in a future version of Wrangler. To learn more visit: https://developers.cloudflare.com/workers/configuration/versions-and-deployments"
`);
});

it("should update worker queue consumers on deploy", async () => {
writeWranglerToml({
queues: {
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 27,15 @@ export function mockUploadWorkerRequest(
keepSecrets?: boolean;
tag?: string;
expectedDispatchNamespace?: string;
expectedScriptName?: string;
} = {}
) {
const expectedScriptName = (options.expectedScriptName ??= "test-name");
const handleUpload: HttpResponseResolver = async ({ params, request }) => {
const url = new URL(request.url);
expect(params.accountId).toEqual("some-account-id");
expect(params.scriptName).toEqual(
legacyEnv && env ? `test-name-${env}` : "test-name"
legacyEnv && env ? `${expectedScriptName}-${env}` : expectedScriptName
);
if (!legacyEnv) {
expect(params.envName).toEqual(env);
Expand Down Expand Up @@ -169,7 171,7 @@ export function mockUploadWorkerRequest(
({ params }) => {
expect(params.accountId).toEqual("some-account-id");
expect(params.scriptName).toEqual(
legacyEnv && env ? `test-name-${env}` : "test-name"
legacyEnv && env ? `${expectedScriptName}-${env}` : expectedScriptName
);
if (!legacyEnv) {
expect(params.envName).toEqual(env);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 41,7 @@ import type {
import type { ValidatorFn } from "./validation-helpers";

export type NormalizeAndValidateConfigArgs = {
name?: string;
env?: string;
"legacy-env"?: boolean;
// This is not relevant in dev. It's only purpose is loosening Worker name validation when deploying to a dispatch namespace
Expand Down
9 changes: 5 additions & 4 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 1054,7 @@ export async function updateQueueProducers(
}

export async function updateQueueConsumers(
scriptName: string | undefined,
config: Config
): Promise<Promise<string[]>[]> {
const consumers = config.queues.consumers || [];
Expand Down Expand Up @@ -1091,7 1092,7 @@ export async function updateQueueConsumers(
])
);
} else {
if (config.name === undefined) {
if (scriptName === undefined) {
// TODO: how can we reliably get the current script name?
throw new UserError(
"Script name is required to update queue consumers"
Expand All @@ -1101,7 1102,7 @@ export async function updateQueueConsumers(
const body: PostTypedConsumerBody = {
type: "worker",
dead_letter_queue: consumer.dead_letter_queue,
script_name: config.name,
script_name: scriptName,
settings: {
batch_size: consumer.max_batch_size,
max_retries: consumer.max_retries,
Expand All @@ -1117,12 1118,12 @@ export async function updateQueueConsumers(
// Current script already assigned to queue?
const existingConsumer =
queue.consumers.filter(
(c) => c.script === config.name || c.service === config.name
(c) => c.script === scriptName || c.service === scriptName
).length > 0;
const envName = undefined; // TODO: script environment for wrangler deploy?
if (existingConsumer) {
updateConsumers.push(
putConsumer(config, consumer.queue, config.name, envName, body).then(
putConsumer(config, consumer.queue, scriptName, envName, body).then(
() => [`Consumer for ${consumer.queue}`]
)
);
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/triggers/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 239,7 @@ export default async function triggersDeploy(props: Props): Promise<void> {
}

if (config.queues.consumers && config.queues.consumers.length) {
const updateConsumers = await updateQueueConsumers(config);
const updateConsumers = await updateQueueConsumers(scriptName, config);

deployments.push(...updateConsumers);
}
Expand Down

0 comments on commit fa1016c

Please sign in to comment.