Skip to content

Commit 4507835

Browse files
committed
Apply some good 🐰 suggestions
1 parent 8e85316 commit 4507835

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.deployments/route.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ function CancelDeploymentDialog({
594594
}: RollbackDeploymentDialogProps) {
595595
const navigation = useNavigation();
596596

597-
const formAction = `/resources/${projectId}/deployments/${deploymentShortCode}/promote`;
597+
const formAction = `/resources/${projectId}/deployments/${deploymentShortCode}/cancel`;
598598
const isLoading = navigation.formAction === formAction;
599599

600600
return (
@@ -605,10 +605,7 @@ function CancelDeploymentDialog({
605605
<DialogClose asChild>
606606
<Button variant="tertiary/medium">Back</Button>
607607
</DialogClose>
608-
<Form
609-
action={`/resources/${projectId}/deployments/${deploymentShortCode}/cancel`}
610-
method="post"
611-
>
608+
<Form action={formAction} method="post">
612609
<Button
613610
type="submit"
614611
name="redirectUrl"

apps/webapp/app/routes/api.v1.deployments.$deploymentId.cancel.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
2-
import { CancelDeploymentRequestBody } from "@trigger.dev/core/v3";
2+
import { CancelDeploymentRequestBody, tryCatch } from "@trigger.dev/core/v3";
33
import { z } from "zod";
44
import { authenticateRequest } from "~/services/apiAuth.server";
55
import { logger } from "~/services/logger.server";
@@ -34,8 +34,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
3434
const { environment: authenticatedEnv } = authenticationResult.result;
3535
const { deploymentId } = parsedParams.data;
3636

37-
const rawBody = await request.json();
38-
const body = CancelDeploymentRequestBody.safeParse(rawBody);
37+
const [, rawBody] = await tryCatch(request.json());
38+
const body = CancelDeploymentRequestBody.safeParse(rawBody ?? {});
3939

4040
if (!body.success) {
4141
return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 });
@@ -55,6 +55,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
5555
switch (error.type) {
5656
case "deployment_not_found":
5757
return json({ error: "Deployment not found" }, { status: 404 });
58+
case "failed_to_delete_deployment_timeout":
59+
return new Response(null, { status: 204 }); // not a critical error, ignore
5860
case "deployment_cannot_be_cancelled":
5961
return json(
6062
{ error: "Deployment is already in a final state and cannot be canceled" },

apps/webapp/app/routes/api.v1.deployments.$deploymentId.progress.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type ActionFunctionArgs, json } from "@remix-run/server-runtime";
2-
import { ProgressDeploymentRequestBody } from "@trigger.dev/core/v3";
2+
import { ProgressDeploymentRequestBody, tryCatch } from "@trigger.dev/core/v3";
33
import { z } from "zod";
44
import { authenticateRequest } from "~/services/apiAuth.server";
55
import { logger } from "~/services/logger.server";
@@ -34,8 +34,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
3434
const { environment: authenticatedEnv } = authenticationResult.result;
3535
const { deploymentId } = parsedParams.data;
3636

37-
const rawBody = await request.json();
38-
const body = ProgressDeploymentRequestBody.safeParse(rawBody);
37+
const [, rawBody] = await tryCatch(request.json());
38+
const body = ProgressDeploymentRequestBody.safeParse(rawBody ?? {});
3939

4040
if (!body.success) {
4141
return json({ error: "Invalid request body", issues: body.error.issues }, { status: 400 });

apps/webapp/app/routes/resources.$projectId.deployments.$deploymentShortCode.cancel.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { logger } from "~/services/logger.server";
88
import { requireUserId } from "~/services/session.server";
99
import { DeploymentService } from "~/v3/services/deployment.server";
1010

11-
export const promoteSchema = z.object({
11+
export const cancelSchema = z.object({
1212
redirectUrl: z.string(),
1313
});
1414

@@ -22,7 +22,7 @@ export const action: ActionFunction = async ({ request, params }) => {
2222
const { projectId, deploymentShortCode } = ParamSchema.parse(params);
2323

2424
const formData = await request.formData();
25-
const submission = parse(formData, { schema: promoteSchema });
25+
const submission = parse(formData, { schema: cancelSchema });
2626

2727
if (!submission.value) {
2828
return json(submission);
@@ -84,7 +84,7 @@ export const action: ActionFunction = async ({ request, params }) => {
8484

8585
if (result.isErr()) {
8686
logger.error(
87-
`Failed to promote deployment: ${result.error.type}`,
87+
`Failed to cancel deployment: ${result.error.type}`,
8888
result.error.type === "other"
8989
? {
9090
cause: result.error.cause,
@@ -105,7 +105,14 @@ export const action: ActionFunction = async ({ request, params }) => {
105105
return redirectWithErrorMessage(
106106
submission.value.redirectUrl,
107107
request,
108-
"Deployment not found"
108+
"Deployment is already in a final state and cannot be canceled"
109+
);
110+
case "failed_to_delete_deployment_timeout":
111+
// not a critical error, ignore
112+
return redirectWithSuccessMessage(
113+
submission.value.redirectUrl,
114+
request,
115+
`Canceled deployment ${deploymentShortCode}.`
109116
);
110117
case "other":
111118
default:

apps/webapp/app/v3/services/deployment.server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,23 @@ export class DeploymentService extends BaseService {
212212
type: "other" as const,
213213
cause: error,
214214
})
215-
);
215+
).andThen((result) => {
216+
if (result.count === 0) {
217+
return errAsync({ type: "deployment_cannot_be_cancelled" as const });
218+
}
219+
return okAsync({ id: deployment.id });
220+
});
221+
222+
const deleteTimeout = (deployment: Pick<WorkerDeployment, "id">) =>
223+
fromPromise(TimeoutDeploymentService.dequeue(deployment.id, this._prisma), (error) => ({
224+
type: "failed_to_delete_deployment_timeout" as const,
225+
cause: error,
226+
}));
216227

217228
return getDeployment()
218229
.andThen(validateDeployment)
219230
.andThen(cancelDeployment)
231+
.andThen(deleteTimeout)
220232
.map(() => undefined);
221233
}
222234
}

0 commit comments

Comments
 (0)