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

C3: don't retry the project creation multiple times when it fails because the project's name is already used #4220

Merged
merged 3 commits into from
Oct 18, 2023
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/witty-trainers-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-cloudflare": patch
---

don't retry the project creation multiple times when it fails because the project's name is already used
11 changes: 10 additions & 1 deletion packages/create-cloudflare/src/helpers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,23 @@ export const printAsyncStatus = async <T>({
return promise;
};

export const retry = async <T>(times: number, fn: () => Promise<T>) => {
export const retry = async <T>(
{
times,
exitCondition,
}: { times: number; exitCondition?: (e: unknown) => boolean },
fn: () => Promise<T>
) => {
let error: unknown = null;
while (times > 0) {
try {
return await fn();
} catch (e) {
error = e;
times--;
if (exitCondition?.(e)) {
break;
}
}
}
throw error;
Expand Down
35 changes: 24 additions & 11 deletions packages/create-cloudflare/src/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,29 @@ const createProject = async (ctx: PagesGeneratorContext) => {
const productionBranch = await getProductionBranch(ctx.project.path);
const cmd = `${npx} wrangler pages project create ${ctx.project.name} --production-branch ${productionBranch} ${compatFlagsArg}`;

await retry(CREATE_PROJECT_RETRIES, async () =>
runCommand(cmd, {
// Make this command more verbose in test mode to aid
// troubleshooting API errors
silent: process.env.VITEST == undefined,
cwd: ctx.project.path,
env: { CLOUDFLARE_ACCOUNT_ID },
startText: "Creating Pages project",
doneText: `${brandColor("created")} ${dim(`via \`${cmd.trim()}\``)}`,
})
await retry(
{
times: CREATE_PROJECT_RETRIES,
exitCondition: (e) => {
return (
e instanceof Error &&
// if the error is regarding name duplication we can exist as retrying is not going to help
e.message.includes(
"A project with this name already exists. Choose a different project name."
)
);
},
},
async () =>
runCommand(cmd, {
// Make this command more verbose in test mode to aid
// troubleshooting API errors
silent: process.env.VITEST == undefined,
cwd: ctx.project.path,
env: { CLOUDFLARE_ACCOUNT_ID },
startText: "Creating Pages project",
doneText: `${brandColor("created")} ${dim(`via \`${cmd.trim()}\``)}`,
})
);
} catch (error) {
crash("Failed to create pages project. See output above.");
Expand All @@ -187,7 +200,7 @@ const createProject = async (ctx: PagesGeneratorContext) => {
try {
const verifyProject = `${npx} wrangler pages deployment list --project-name ${ctx.project.name}`;

await retry(VERIFY_PROJECT_RETRIES, async () =>
await retry({ times: VERIFY_PROJECT_RETRIES }, async () =>
runCommand(verifyProject, {
silent: process.env.VITEST == undefined,
cwd: ctx.project.path,
Expand Down
Loading