Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry deployments #6801

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-trainers-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

feat: implement retries within `wrangler deploy` and `wrangler versions upload` to workaround spotty network connections and service flakes
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ describe("versions upload", () => {
)
);
}
function mockUploadVersion(has_preview: boolean) {
function mockUploadVersion(has_preview: boolean, flakeCount = 1) {
msw.use(
http.post(
`*/accounts/:accountId/workers/scripts/:scriptName/versions`,
({ params }) => {
if (flakeCount > 0) {
flakeCount--;
return HttpResponse.error();
}

expect(params.scriptName).toEqual("test-worker");

return HttpResponse.json(
Expand All @@ -53,8 +58,7 @@ describe("versions upload", () => {
},
})
);
},
{ once: true }
}
)
);
}
Expand Down
61 changes: 33 additions & 28 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
maybeRetrieveFileSourceMap,
} from "../sourcemap";
import triggersDeploy from "../triggers/deploy";
import { retryOnError } from "../utils/retry";
import {
createDeployment,
patchNonVersionedScriptSettings,
Expand Down Expand Up @@ -814,13 +815,15 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
// If we're using the new APIs, first upload the version
if (canUseNewVersionsDeploymentsApi) {
// Upload new version
const versionResult = await fetchResult<ApiVersion>(
`/accounts/${accountId}/workers/scripts/${scriptName}/versions`,
{
method: "POST",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
}
const versionResult = await retryOnError(async () =>
fetchResult<ApiVersion>(
`/accounts/${accountId}/workers/scripts/${scriptName}/versions`,
{
method: "POST",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
}
)
);

// Deploy new version to 100%
Expand Down Expand Up @@ -852,27 +855,29 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
startup_time_ms: versionResult.startup_time_ms,
};
} else {
result = await fetchResult<{
available_on_subdomain: boolean;
id: string | null;
etag: string | null;
pipeline_hash: string | null;
mutable_pipeline_id: string | null;
deployment_id: string | null;
startup_time_ms: number;
}>(
workerUrl,
{
method: "PUT",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
},
new URLSearchParams({
include_subdomain_availability: "true",
// pass excludeScript so the whole body of the
// script doesn't get included in the response
excludeScript: "true",
})
result = await retryOnError(async () =>
fetchResult<{
available_on_subdomain: boolean;
id: string | null;
etag: string | null;
pipeline_hash: string | null;
mutable_pipeline_id: string | null;
deployment_id: string | null;
startup_time_ms: number;
}>(
workerUrl,
{
method: "PUT",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
},
new URLSearchParams({
include_subdomain_availability: "true",
// pass excludeScript so the whole body of the
// script doesn't get included in the response
excludeScript: "true",
})
)
);
}

Expand Down
18 changes: 18 additions & 0 deletions packages/wrangler/src/utils/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { setTimeout } from "node:timers/promises";

export async function retryOnError<T>(
action: () => T | Promise<T>,
backoff = 2_000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this signature means you have to pass a backoff if you want to define attempts, which isn't ideal. Can we put backoff and attempts into an options object instead, with both fields being optional?

attempts = 3
): Promise<T> {
try {
return await action();
} catch (err) {
if (attempts <= 1) {
throw err;
}

await setTimeout(backoff);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the default be 0? what would a 2 second wait (by default) accomplish?

return retryOnError(action, backoff, attempts - 1);
}
}
25 changes: 14 additions & 11 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
getSourceMappedString,
maybeRetrieveFileSourceMap,
} from "../sourcemap";
import { retryOnError } from "../utils/retry";
import type { AssetsOptions } from "../assets";
import type { Config } from "../config";
import type { Rule } from "../config/environment";
Expand Down Expand Up @@ -466,17 +467,19 @@ See https://developers.cloudflare.com/workers/platform/compatibility-dates for m
try {
const body = createWorkerUploadForm(worker);

const result = await fetchResult<{
id: string;
startup_time_ms: number;
metadata: {
has_preview: boolean;
};
}>(`${workerUrl}/versions`, {
method: "POST",
body,
headers: await getMetricsUsageHeaders(config.send_metrics),
});
const result = await retryOnError(async () =>
fetchResult<{
id: string;
startup_time_ms: number;
metadata: {
has_preview: boolean;
};
}>(`${workerUrl}/versions`, {
method: "POST",
body,
headers: await getMetricsUsageHeaders(config.send_metrics),
})
);

logger.log("Worker Startup Time:", result.startup_time_ms, "ms");
bindingsPrinted = true;
Expand Down
Loading