|
1 | 1 | import { type AuthenticatedEnvironment } from "~/services/apiAuth.server"; |
2 | 2 | import { BaseService } from "./baseService.server"; |
3 | 3 | import { errAsync, fromPromise, okAsync } from "neverthrow"; |
4 | | -import { type WorkerDeploymentStatus, type WorkerDeployment } from "@trigger.dev/database"; |
5 | | -import { type ExternalBuildData, logger, type GitMeta } from "@trigger.dev/core/v3"; |
| 4 | +import { type WorkerDeployment } from "@trigger.dev/database"; |
| 5 | +import { logger, type GitMeta } from "@trigger.dev/core/v3"; |
6 | 6 | import { TimeoutDeploymentService } from "./timeoutDeployment.server"; |
7 | 7 | import { env } from "~/env.server"; |
8 | 8 | import { createRemoteImageBuild } from "../remoteImageBuilder.server"; |
| 9 | +import { FINAL_DEPLOYMENT_STATUSES } from "./failDeployment.server"; |
9 | 10 |
|
10 | 11 | export class DeploymentService extends BaseService { |
11 | 12 | /** |
@@ -143,4 +144,79 @@ export class DeploymentService extends BaseService { |
143 | 144 | .andThen(extendTimeout) |
144 | 145 | .map(() => undefined); |
145 | 146 | } |
| 147 | + |
| 148 | + /** |
| 149 | + * Cancels a deployment that is not yet in a final state. |
| 150 | + * |
| 151 | + * Only acts when the current status is not final. Not idempotent. |
| 152 | + * |
| 153 | + * @param authenticatedEnv The environment which the deployment belongs to. |
| 154 | + * @param friendlyId The friendly deployment ID. |
| 155 | + * @param data Cancelation reason. |
| 156 | + */ |
| 157 | + public cancelDeployment( |
| 158 | + authenticatedEnv: AuthenticatedEnvironment, |
| 159 | + friendlyId: string, |
| 160 | + data: Partial<Pick<WorkerDeployment, "canceledReason">> |
| 161 | + ) { |
| 162 | + const getDeployment = () => |
| 163 | + fromPromise( |
| 164 | + this._prisma.workerDeployment.findFirst({ |
| 165 | + where: { |
| 166 | + friendlyId, |
| 167 | + environmentId: authenticatedEnv.id, |
| 168 | + }, |
| 169 | + select: { |
| 170 | + status: true, |
| 171 | + id: true, |
| 172 | + }, |
| 173 | + }), |
| 174 | + (error) => ({ |
| 175 | + type: "other" as const, |
| 176 | + cause: error, |
| 177 | + }) |
| 178 | + ).andThen((deployment) => { |
| 179 | + if (!deployment) { |
| 180 | + return errAsync({ type: "deployment_not_found" as const }); |
| 181 | + } |
| 182 | + return okAsync(deployment); |
| 183 | + }); |
| 184 | + |
| 185 | + const validateDeployment = (deployment: Pick<WorkerDeployment, "id" | "status">) => { |
| 186 | + if (FINAL_DEPLOYMENT_STATUSES.includes(deployment.status)) { |
| 187 | + logger.warn("Attempted cancelling deployment in a final state", { |
| 188 | + deployment, |
| 189 | + }); |
| 190 | + return errAsync({ type: "deployment_cannot_be_cancelled" as const }); |
| 191 | + } |
| 192 | + |
| 193 | + return okAsync(deployment); |
| 194 | + }; |
| 195 | + |
| 196 | + const cancelDeployment = (deployment: Pick<WorkerDeployment, "id">) => |
| 197 | + fromPromise( |
| 198 | + this._prisma.workerDeployment.updateMany({ |
| 199 | + where: { |
| 200 | + id: deployment.id, |
| 201 | + status: { |
| 202 | + notIn: FINAL_DEPLOYMENT_STATUSES, // status could've changed in the meantime, we're not locking the row |
| 203 | + }, |
| 204 | + }, |
| 205 | + data: { |
| 206 | + status: "CANCELED", |
| 207 | + canceledAt: new Date(), |
| 208 | + canceledReason: data.canceledReason, |
| 209 | + }, |
| 210 | + }), |
| 211 | + (error) => ({ |
| 212 | + type: "other" as const, |
| 213 | + cause: error, |
| 214 | + }) |
| 215 | + ); |
| 216 | + |
| 217 | + return getDeployment() |
| 218 | + .andThen(validateDeployment) |
| 219 | + .andThen(cancelDeployment) |
| 220 | + .map(() => undefined); |
| 221 | + } |
146 | 222 | } |
0 commit comments