-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdeployment.server.ts
More file actions
146 lines (137 loc) · 4.99 KB
/
deployment.server.ts
File metadata and controls
146 lines (137 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { type AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { BaseService } from "./baseService.server";
import { errAsync, fromPromise, okAsync } from "neverthrow";
import { type WorkerDeploymentStatus, type WorkerDeployment } from "@trigger.dev/database";
import { type ExternalBuildData, logger, type GitMeta } from "@trigger.dev/core/v3";
import { TimeoutDeploymentService } from "./timeoutDeployment.server";
import { env } from "~/env.server";
import { createRemoteImageBuild } from "../remoteImageBuilder.server";
export class DeploymentService extends BaseService {
/**
* Progresses a deployment from PENDING to INSTALLING and then to BUILDING.
* Also extends the deployment timeout.
*
* When progressing to BUILDING, the remote Depot build is also created.
*
* Only acts when the current status allows. Not idempotent.
*
* @param authenticatedEnv The environment which the deployment belongs to.
* @param friendlyId The friendly deployment ID.
* @param updates Optional deployment details to persist.
*/
public progressDeployment(
authenticatedEnv: AuthenticatedEnvironment,
friendlyId: string,
updates: Partial<Pick<WorkerDeployment, "contentHash" | "runtime"> & { git: GitMeta }>
) {
const getDeployment = () =>
fromPromise(
this._prisma.workerDeployment.findFirst({
where: {
friendlyId,
environmentId: authenticatedEnv.id,
},
select: {
status: true,
id: true,
},
}),
(error) => ({
type: "other" as const,
cause: error,
})
).andThen((deployment) => {
if (!deployment) {
return errAsync({ type: "deployment_not_found" as const });
}
return okAsync(deployment);
});
const validateDeployment = (deployment: Pick<WorkerDeployment, "id" | "status">) => {
if (deployment.status !== "PENDING" && deployment.status !== "INSTALLING") {
logger.warn(
"Attempted progressing deployment that is not in PENDING or INSTALLING status",
{
deployment,
}
);
return errAsync({ type: "deployment_cannot_be_progressed" as const });
}
return okAsync(deployment);
};
const progressToInstalling = (deployment: Pick<WorkerDeployment, "id">) =>
fromPromise(
this._prisma.workerDeployment.updateMany({
where: { id: deployment.id, status: "PENDING" }, // status could've changed in the meantime, we're not locking the row
data: {
...updates,
status: "INSTALLING",
startedAt: new Date(),
},
}),
(error) => ({
type: "other" as const,
cause: error,
})
).andThen((result) => {
if (result.count === 0) {
return errAsync({ type: "deployment_cannot_be_progressed" as const });
}
return okAsync({ id: deployment.id, status: "INSTALLING" as const });
});
const createRemoteBuild = (deployment: Pick<WorkerDeployment, "id">) =>
fromPromise(createRemoteImageBuild(authenticatedEnv.project), (error) => ({
type: "failed_to_create_remote_build" as const,
cause: error,
}));
const progressToBuilding = (deployment: Pick<WorkerDeployment, "id">) =>
createRemoteBuild(deployment)
.andThen((externalBuildData) =>
fromPromise(
this._prisma.workerDeployment.updateMany({
where: { id: deployment.id, status: "INSTALLING" }, // status could've changed in the meantime, we're not locking the row
data: {
...updates,
externalBuildData,
status: "BUILDING",
installedAt: new Date(),
},
}),
(error) => ({
type: "other" as const,
cause: error,
})
)
)
.andThen((result) => {
if (result.count === 0) {
return errAsync({ type: "deployment_cannot_be_progressed" as const });
}
return okAsync({ id: deployment.id, status: "BUILDING" as const });
});
const extendTimeout = (deployment: Pick<WorkerDeployment, "id" | "status">) =>
fromPromise(
TimeoutDeploymentService.enqueue(
deployment.id,
deployment.status,
deployment.status === "INSTALLING"
? "Installing dependencies timed out"
: "Building timed out",
new Date(Date.now() + env.DEPLOY_TIMEOUT_MS)
),
(error) => ({
type: "failed_to_extend_deployment_timeout" as const,
cause: error,
})
);
return getDeployment()
.andThen(validateDeployment)
.andThen((deployment) => {
if (deployment.status === "PENDING") {
return progressToInstalling(deployment);
}
return progressToBuilding(deployment);
})
.andThen(extendTimeout)
.map(() => undefined);
}
}