Skip to content

Commit

Permalink
update non-versioned script settings in versions deploy (#5366)
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Mar 26, 2024
1 parent 426906a commit e11e169
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-trees-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: save non-versioned script-settings (logpush, tail_consumers) on `wrangler versions deploy`. This command still requires `--experimental-versions`.
20 changes: 20 additions & 0 deletions packages/wrangler/src/__tests__/helpers/msw/handlers/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,23 @@ export const mswPostNewDeployment = rest.post(
);
}
);

export const mswPatchNonVersionedScriptSettings = rest.patch(
"*/accounts/:accountId/workers/scripts/:workerName/script-settings",
async (request, response, context) => {
return response(context.json(createFetchResult(await request.json())));
}
);
export const mswGetNonVersionedScriptSettings = rest.patch(
"*/accounts/:accountId/workers/scripts/:workerName/script-settings",
(request, response, context) => {
return response(
context.json(
createFetchResult({
logpush: false,
tail_consumers: [],
})
)
);
}
);
4 changes: 4 additions & 0 deletions packages/wrangler/src/__tests__/helpers/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { mswSuccessR2handlers } from "./handlers/r2";
import { default as mswSucessScriptHandlers } from "./handlers/script";
import { mswSuccessUserHandlers } from "./handlers/user";
import {
mswGetNonVersionedScriptSettings,
mswGetVersion,
mswListNewDeployments,
mswListVersions,
mswPatchNonVersionedScriptSettings,
mswPostNewDeployment,
} from "./handlers/versions";
import { default as mswZoneHandlers } from "./handlers/zones";
Expand Down Expand Up @@ -61,4 +63,6 @@ export {
mswGetVersion,
mswListNewDeployments,
mswListVersions,
mswGetNonVersionedScriptSettings,
mswPatchNonVersionedScriptSettings,
};
128 changes: 127 additions & 1 deletion packages/wrangler/src/__tests__/versions/versions.deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
mswGetVersion,
mswListNewDeployments,
mswListVersions,
mswPatchNonVersionedScriptSettings,
mswPostNewDeployment,
} from "../helpers/msw";
import { runInTempDir } from "../helpers/run-in-tmp";
Expand Down Expand Up @@ -52,7 +53,8 @@ describe("versions deploy", () => {
mswListNewDeployments,
mswListVersions,
mswGetVersion,
mswPostNewDeployment
mswPostNewDeployment,
mswPatchNonVersionedScriptSettings
);
});

Expand Down Expand Up @@ -95,9 +97,14 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed named-worker version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);

expect(normalizeOutput(std.out)).toContain(
"No non-versioned settings to sync. Skipping..."
);
});

test("fails without --name arg", async () => {
Expand Down Expand Up @@ -184,6 +191,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);
Expand Down Expand Up @@ -227,6 +235,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);
Expand Down Expand Up @@ -278,6 +287,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 50% and version 00000000-0000-0000-0000-000000000000 at 50% (TIMINGS)"
`);
Expand Down Expand Up @@ -321,6 +331,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);
Expand Down Expand Up @@ -372,6 +383,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 30% and version 00000000-0000-0000-0000-000000000000 at 70% (TIMINGS)"
`);
Expand Down Expand Up @@ -423,6 +435,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 40% and version 00000000-0000-0000-0000-000000000000 at 60% (TIMINGS)"
`);
Expand Down Expand Up @@ -530,6 +543,7 @@ describe("versions deploy", () => {
├ Add a deployment message (skipped)
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 33.333%, version 00000000-0000-0000-0000-000000000000 at 33.334%, and version 00000000-0000-0000-0000-000000000000 at 33.333% (TIMINGS)"
`);
Expand Down Expand Up @@ -577,6 +591,118 @@ describe("versions deploy", () => {
│ Deployment message My versioned deployment message
│ No non-versioned settings to sync. Skipping...
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);
});

test("with logpush in wrangler.toml", async () => {
writeWranglerToml({
logpush: true,
});

const result = runWrangler(
"versions deploy 10000000-0000-0000-0000-000000000000 --yes --experimental-gradual-rollouts"
);

await expect(result).resolves.toBeUndefined();

expect(normalizeOutput(std.out)).toMatchInlineSnapshot(`
"╭ Deploy Worker Versions by splitting traffic between multiple versions
├ Your current deployment has 2 version(s):
│ (10%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
│ (90%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
├ Which version(s) do you want to deploy?
├ 1 Worker Version(s) selected
├ Worker Version 1: 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
├ What percentage of traffic should Worker Version 1 receive?
├ 100% of traffic
├ Add a deployment message (skipped)
│ Synced non-versioned settings:
│ logpush: true
│ tail_consumers: <skipped>
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);

expect(normalizeOutput(std.out)).toContain("logpush: true");
});

test("with logpush and tail_consumers in wrangler.toml", async () => {
writeWranglerToml({
logpush: false,
tail_consumers: [
{ service: "worker-1" },
{ service: "worker-2", environment: "preview" },
{ service: "worker-3", environment: "staging" },
],
});

const result = runWrangler(
"versions deploy 10000000-0000-0000-0000-000000000000 --yes --experimental-gradual-rollouts"
);

await expect(result).resolves.toBeUndefined();

expect(normalizeOutput(std.out)).toMatchInlineSnapshot(`
"╭ Deploy Worker Versions by splitting traffic between multiple versions
├ Your current deployment has 2 version(s):
│ (10%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
│ (90%) 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
├ Which version(s) do you want to deploy?
├ 1 Worker Version(s) selected
├ Worker Version 1: 00000000-0000-0000-0000-000000000000
│ Created: TIMESTAMP
│ Tag: -
│ Message: -
├ What percentage of traffic should Worker Version 1 receive?
├ 100% of traffic
├ Add a deployment message (skipped)
│ Synced non-versioned settings:
│ logpush: false
│ tail_consumers: worker-1
│ worker-2 (preview)
│ worker-3 (staging)
╰ SUCCESS Deployed test-name version 00000000-0000-0000-0000-000000000000 at 100% (TIMINGS)"
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/utils/render-labelled-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function formatLabelledValues(
spacerCount -
labelLengthsWithoutANSI[i] -
labelAlignment.length
: valuesAlignment + spacerCount
: valuesAlignment + spacerCount + indentationCount
);

return prefixSpacing + line;
Expand Down
37 changes: 37 additions & 0 deletions packages/wrangler/src/versions/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fetchResult } from "../cfetch";
import type { TailConsumer } from "../config/environment";
import type {
ApiDeployment,
ApiVersion,
Expand Down Expand Up @@ -125,3 +126,39 @@ export async function createDeployment(

return res;
}

type NonVersionedScriptSettings = {
logpush: boolean;
tail_consumers: TailConsumer[];
};

export async function patchNonVersionedScriptSettings(
accountId: string,
workerName: string,
settings: Partial<NonVersionedScriptSettings>
) {
const res = await fetchResult<typeof settings>(
`/accounts/${accountId}/workers/scripts/${workerName}/script-settings`,
{
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(settings),
}
);

// TODO: handle specific errors

return res;
}

export async function fetchNonVersionedScriptSettings(
accountId: string,
workerName: string
): Promise<NonVersionedScriptSettings> {
const res = await fetchResult<NonVersionedScriptSettings>(
`/accounts/${accountId}/workers/scripts/${workerName}/script-settings`,
{ method: "GET" }
);

return res;
}
56 changes: 56 additions & 0 deletions packages/wrangler/src/versions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import { UserError } from "../errors";
import * as metrics from "../metrics";
import { printWranglerBanner } from "../update-check";
import { requireAuth } from "../user";
import formatLabelledValues from "../utils/render-labelled-values";
import {
createDeployment,
fetchLatestDeploymentVersions,
fetchLatestUploadedVersions,
fetchVersions,
patchNonVersionedScriptSettings,
} from "./api";
import type { Config } from "../config";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
Expand Down Expand Up @@ -174,6 +177,8 @@ export async function versionsDeployHandler(args: VersionsDeployArgs) {
},
});

await maybePatchSettings(accountId, workerName, config);

const elapsedMilliseconds = Date.now() - start;
const elapsedSeconds = elapsedMilliseconds / 1000;
const elapsedString = `${elapsedSeconds.toFixed(2)} sec`;
Expand Down Expand Up @@ -433,6 +438,57 @@ async function promptPercentages(
return confirmedVersionTraffic;
}

async function maybePatchSettings(
accountId: string,
workerName: string,
config: Pick<Config, "logpush" | "tail_consumers">
) {
const maybeUndefinedSettings = {
logpush: config.logpush,
tail_consumers: config.tail_consumers,
};
const definedSettings = Object.fromEntries(
Object.entries(maybeUndefinedSettings).filter(
([, value]) => value !== undefined
)
);

const hasZeroSettingsToSync = Object.keys(definedSettings).length === 0;
if (hasZeroSettingsToSync) {
cli.log("No non-versioned settings to sync. Skipping...");
return;
}

const patchedSettings = await spinnerWhile({
startMessage: `Syncing non-versioned settings`,
async promise() {
return await patchNonVersionedScriptSettings(
accountId,
workerName,
definedSettings
);
},
});

const formattedSettings = formatLabelledValues(
{
logpush: String(patchedSettings.logpush ?? "<skipped>"),
tail_consumers:
patchedSettings.tail_consumers
?.map((tc) =>
tc.environment ? `${tc.service} (${tc.environment})` : tc.service
)
.join("\n") ?? "<skipped>",
},
{
labelJustification: "right",
indentationCount: 4,
}
);

cli.log("Synced non-versioned settings:\n" + formattedSettings);
}

// ***********
// UNITS
// ***********
Expand Down

0 comments on commit e11e169

Please sign in to comment.