Skip to content

Commit

Permalink
PR review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmenPopoviciu committed May 14, 2024
1 parent d27cbb3 commit 85b2070
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/wrangler/src/pages/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const MAX_BUCKET_FILE_COUNT = 5000;
export const BULK_UPLOAD_CONCURRENCY = 3;
export const MAX_UPLOAD_ATTEMPTS = 5;
export const MAX_DEPLOYMENT_ATTEMPTS = 3;
export const MAX_DEPLOYMENT_STATUS_ATTEMPTS = 5;
export const MAX_CHECK_MISSING_ATTEMPTS = 5;
export const SECONDS_TO_WAIT_FOR_PROXY = 5;
export const isInPagesCI = !!process.env.CF_PAGES;
Expand Down
41 changes: 35 additions & 6 deletions packages/wrangler/src/pages/deploy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { FatalError } from "../errors";
import { logger } from "../logger";
import * as metrics from "../metrics";
import { requireAuth } from "../user";
import { PAGES_CONFIG_CACHE_FILENAME } from "./constants";
import {
MAX_DEPLOYMENT_STATUS_ATTEMPTS,
PAGES_CONFIG_CACHE_FILENAME,
} from "./constants";
import { EXIT_CODE_INVALID_PAGES_CONFIG } from "./errors";
import { listProjects } from "./projects";
import { promptSelectProject } from "./prompt-select-project";
Expand All @@ -20,8 +23,13 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { Deployment, DeploymentStage, PagesConfigCache } from "./types";
import type { Project, UnifiedDeploymentLogMessages } from "@cloudflare/types";
import type { PagesConfigCache } from "./types";
import type {
Deployment,
DeploymentStage,
Project,
UnifiedDeploymentLogMessages,
} from "@cloudflare/types";

type PagesDeployArgs = StrictYargsOptionsToInterface<typeof Options>;

Expand Down Expand Up @@ -338,30 +346,45 @@ export const Handler = async (args: PagesDeployArgs) => {
});

let latestDeploymentStage: DeploymentStage | undefined;
let attempts = 0;

// can this ever become an infinite loop?
while (
attempts < MAX_DEPLOYMENT_STATUS_ATTEMPTS &&
latestDeploymentStage?.name !== "deploy" &&
latestDeploymentStage?.status !== "success" &&
latestDeploymentStage?.status !== "failure"
) {
try {
/*
* Exponential backoff
* On every retry, exponentially increase the wait time: 1 second, then
* 2s, then 4s, then 8s, etc.
*/
await new Promise((resolvePromise) =>
setTimeout(resolvePromise, Math.pow(2, attempts++) * 1000)
);

logger.debug(
`attempt #${attempts}: Attempting to fetch status for deployment with id "${deploymentResponse.id}" ...`
);

const deployment = await fetchResult<Deployment>(
`/accounts/${accountId}/pages/projects/${projectName}/deployments/${deploymentResponse.id}`
);
latestDeploymentStage = deployment.latest_stage;
} catch (err) {
// don't retry if API call retruned an error
logger.debug(
`Attempt to get deployment status for deployment with id "${deploymentResponse.id}" failed: ${err}`
);
}
}

if (latestDeploymentStage.status === "success") {
if (latestDeploymentStage?.status === "success") {
logger.log(
`✨ Deployment complete! Take a peek over at ${deploymentResponse.url}`
);
} else {
} else if (latestDeploymentStage?.status === "failure") {
// get persistent logs so we can show users the failure message
const logs = await fetchResult<UnifiedDeploymentLogMessages>(
`/accounts/${accountId}/pages/projects/${projectName}/deployments/${deploymentResponse.id}/history/logs?size=10000000`
Expand All @@ -371,6 +394,12 @@ export const Handler = async (args: PagesDeployArgs) => {

logger.error(failureMessage.replace("Error:", "").trim());
logger.log("❌ Deployment failed!");
} else {
logger.log(
`✨ Deployment complete! However, we couldn't ascertain the final status of your deployment.\n\n` +
`⚡️ Visit your deployment at ${deploymentResponse.url}\n` +
`⚡️ Check the deployment logs on the Cloudflare dashboard: https://dash.cloudflare.com/${accountId}/pages/view/${projectName}/${deploymentResponse.id}`
);
}

await metrics.sendMetricsEvent("create pages deployment");
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/pages/deployment-tails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setTimeout } from "node:timers/promises";
import { Deployment } from "@cloudflare/types";
import onExit from "signal-exit";
import { printWranglerBanner } from "..";
import { fetchResult } from "../cfetch";
Expand All @@ -22,7 +23,7 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { Deployment, PagesConfigCache } from "./types";
import type { PagesConfigCache } from "./types";

const statusChoices = ["ok", "error", "canceled"] as const;
type StatusChoice = typeof statusChoices[number];
Expand Down
8 changes: 6 additions & 2 deletions packages/wrangler/src/pages/deployments.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Deployment } from "@cloudflare/types";
import Table from "ink-table";
import React from "react";
import { format as timeagoFormat } from "timeago.js";
Expand All @@ -14,7 +15,7 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { Deployment, PagesConfigCache } from "./types";
import type { PagesConfigCache } from "./types";

type ListArgs = StrictYargsOptionsToInterface<typeof ListOptions>;

Expand Down Expand Up @@ -54,7 +55,10 @@ export async function ListHandler({ projectName }: ListArgs) {

const getStatus = (deployment: Deployment) => {
// Return a pretty time since timestamp if successful otherwise the status
if (deployment.latest_stage.status === `success`) {
if (
deployment.latest_stage.status === `success` &&
deployment.latest_stage.ended_on
) {
return timeagoFormat(deployment.latest_stage.ended_on);
}
return titleCase(deployment.latest_stage.status);
Expand Down
15 changes: 0 additions & 15 deletions packages/wrangler/src/pages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ export type Project = {
};
};

export type Deployment = {
id: string;
created_on: string;
environment: string;
deployment_trigger: {
metadata: {
commit_hash: string;
branch: string;
};
};
url: string;
latest_stage: DeploymentStage;
project_name: string;
};

export type UploadPayloadFile = {
key: string;
value: string;
Expand Down

0 comments on commit 85b2070

Please sign in to comment.