-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcreateDeploymentBackgroundWorker.server.ts
More file actions
165 lines (138 loc) · 4.78 KB
/
createDeploymentBackgroundWorker.server.ts
File metadata and controls
165 lines (138 loc) · 4.78 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { CreateBackgroundWorkerRequestBody, logger, tryCatch } from "@trigger.dev/core/v3";
import { BackgroundWorkerId } from "@trigger.dev/core/v3/isomorphic";
import type { BackgroundWorker, WorkerDeployment } from "@trigger.dev/database";
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
import { BaseService, ServiceValidationError } from "./baseService.server";
import {
createBackgroundFiles,
createWorkerResources,
syncDeclarativeSchedules,
} from "./createBackgroundWorker.server";
import { TimeoutDeploymentService } from "./timeoutDeployment.server";
export class CreateDeploymentBackgroundWorkerService extends BaseService {
public async call(
environment: AuthenticatedEnvironment,
deploymentId: string,
body: CreateBackgroundWorkerRequestBody
): Promise<BackgroundWorker | undefined> {
return this.traceWithEnv("call", environment, async (span) => {
span.setAttribute("deploymentId", deploymentId);
const deployment = await this._prisma.workerDeployment.findFirst({
where: {
friendlyId: deploymentId,
},
});
if (!deployment) {
return;
}
if (deployment.status !== "BUILDING") {
return;
}
const backgroundWorker = await this._prisma.backgroundWorker.create({
data: {
...BackgroundWorkerId.generate(),
version: deployment.version,
runtimeEnvironmentId: environment.id,
projectId: environment.projectId,
metadata: body.metadata,
contentHash: body.metadata.contentHash,
cliVersion: body.metadata.cliPackageVersion,
sdkVersion: body.metadata.packageVersion,
supportsLazyAttempts: body.supportsLazyAttempts,
engine: body.engine,
},
});
//upgrade the project to engine "V2" if it's not already
if (environment.project.engine === "V1" && body.engine === "V2") {
await this._prisma.project.update({
where: {
id: environment.project.id,
},
data: {
engine: "V2",
},
});
}
const [filesError, tasksToBackgroundFiles] = await tryCatch(
createBackgroundFiles(
body.metadata.sourceFiles,
backgroundWorker,
environment,
this._prisma
)
);
if (filesError) {
logger.error("Error creating background worker files", {
error: filesError,
});
const serviceError = new ServiceValidationError("Error creating background worker files");
await this.#failBackgroundWorkerDeployment(deployment, serviceError);
throw serviceError;
}
const [resourcesError] = await tryCatch(
createWorkerResources(
body.metadata,
backgroundWorker,
environment,
this._prisma,
tasksToBackgroundFiles
)
);
if (resourcesError) {
logger.error("Error creating background worker resources", {
error: resourcesError,
});
const serviceError = new ServiceValidationError(
"Error creating background worker resources"
);
await this.#failBackgroundWorkerDeployment(deployment, serviceError);
throw serviceError;
}
const [schedulesError] = await tryCatch(
syncDeclarativeSchedules(body.metadata.tasks, backgroundWorker, environment, this._prisma)
);
if (schedulesError) {
logger.error("Error syncing declarative schedules", {
error: schedulesError,
});
const serviceError =
schedulesError instanceof ServiceValidationError
? schedulesError
: new ServiceValidationError("Error syncing declarative schedules");
await this.#failBackgroundWorkerDeployment(deployment, serviceError);
throw serviceError;
}
// Link the deployment with the background worker
await this._prisma.workerDeployment.update({
where: {
id: deployment.id,
},
data: {
status: "DEPLOYING",
workerId: backgroundWorker.id,
builtAt: new Date(),
type: backgroundWorker.engine === "V2" ? "MANAGED" : "V1",
},
});
await TimeoutDeploymentService.dequeue(deployment.id, this._prisma);
return backgroundWorker;
});
}
async #failBackgroundWorkerDeployment(deployment: WorkerDeployment, error: Error) {
await this._prisma.workerDeployment.update({
where: {
id: deployment.id,
},
data: {
status: "FAILED",
failedAt: new Date(),
errorData: {
name: error.name,
message: error.message,
},
},
});
await TimeoutDeploymentService.dequeue(deployment.id, this._prisma);
throw error;
}
}