-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathworkerGroupService.server.ts
More file actions
316 lines (274 loc) · 8.05 KB
/
workerGroupService.server.ts
File metadata and controls
316 lines (274 loc) · 8.05 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { WorkerInstanceGroup, WorkerInstanceGroupType } from "@trigger.dev/database";
import { WithRunEngine } from "../baseService.server";
import { WorkerGroupTokenService } from "./workerGroupTokenService.server";
import { logger } from "~/services/logger.server";
import { FEATURE_FLAG } from "~/v3/featureFlags";
import { makeFlag, makeSetFlag } from "~/v3/featureFlags.server";
import { isComputeRegionAccessible, resolveComputeAccess } from "~/v3/regionAccess.server";
export class WorkerGroupService extends WithRunEngine {
private readonly defaultNamePrefix = "worker_group";
async createWorkerGroup({
projectId,
organizationId,
name,
description,
}: {
projectId?: string;
organizationId?: string;
name?: string;
description?: string;
}) {
if (!name) {
name = await this.generateWorkerName({ projectId });
}
const tokenService = new WorkerGroupTokenService({
prisma: this._prisma,
engine: this._engine,
});
const token = await tokenService.createToken();
const workerGroup = await this._prisma.workerInstanceGroup.create({
data: {
projectId,
organizationId,
type: projectId ? WorkerInstanceGroupType.UNMANAGED : WorkerInstanceGroupType.MANAGED,
masterQueue: this.generateMasterQueueName({ projectId, name }),
tokenId: token.id,
description,
name,
},
});
if (workerGroup.type === WorkerInstanceGroupType.MANAGED) {
const managedCount = await this._prisma.workerInstanceGroup.count({
where: {
type: WorkerInstanceGroupType.MANAGED,
},
});
const getFlag = makeFlag(this._prisma);
const defaultWorkerInstanceGroupId = await getFlag({
key: FEATURE_FLAG.defaultWorkerInstanceGroupId,
});
// If there's no global default yet we should set it to the new worker group
if (!defaultWorkerInstanceGroupId) {
const setFlag = makeSetFlag(this._prisma);
await setFlag({
key: FEATURE_FLAG.defaultWorkerInstanceGroupId,
value: workerGroup.id,
});
}
}
return {
workerGroup,
token,
};
}
/**
This updates a single worker group.
The name should never be updated. This would mean changing the masterQueue name which can have unexpected consequences.
*/
async updateWorkerGroup({
projectId,
workerGroupId,
description,
}: {
projectId: string;
workerGroupId: string;
description?: string;
}) {
const workerGroup = await this._prisma.workerInstanceGroup.findUnique({
where: {
id: workerGroupId,
projectId,
},
});
if (!workerGroup) {
logger.error("[WorkerGroupService] No worker group found for update", {
workerGroupId,
description,
});
return;
}
await this._prisma.workerInstanceGroup.update({
where: {
id: workerGroup.id,
},
data: {
description,
},
});
}
/**
This lists worker groups.
Without a project ID, only shared worker groups will be returned.
With a project ID, in addition to all shared worker groups, ones associated with the project will also be returned.
*/
async listWorkerGroups({ projectId, listHidden }: { projectId?: string; listHidden?: boolean }) {
const workerGroups = await this._prisma.workerInstanceGroup.findMany({
where: {
OR: [
{
type: WorkerInstanceGroupType.MANAGED,
},
{
projectId,
},
],
AND: listHidden ? [] : [{ hidden: false }],
},
});
return workerGroups;
}
async deleteWorkerGroup({
projectId,
workerGroupId,
}: {
projectId: string;
workerGroupId: string;
}) {
const workerGroup = await this._prisma.workerInstanceGroup.findUnique({
where: {
id: workerGroupId,
},
});
if (!workerGroup) {
logger.error("[WorkerGroupService] WorkerGroup not found for deletion", {
workerGroupId,
projectId,
});
return;
}
if (workerGroup.projectId !== projectId) {
logger.error("[WorkerGroupService] WorkerGroup does not belong to project", {
workerGroupId,
projectId,
});
return;
}
await this._prisma.workerInstanceGroup.delete({
where: {
id: workerGroupId,
},
});
}
async getGlobalDefaultWorkerGroup() {
const flags = makeFlag(this._prisma);
const defaultWorkerInstanceGroupId = await flags({
key: FEATURE_FLAG.defaultWorkerInstanceGroupId,
});
if (!defaultWorkerInstanceGroupId) {
logger.error("[WorkerGroupService] Default worker group not found in feature flags");
return;
}
const workerGroup = await this._prisma.workerInstanceGroup.findUnique({
where: {
id: defaultWorkerInstanceGroupId,
},
});
if (!workerGroup) {
logger.error("[WorkerGroupService] Default worker group not found", {
defaultWorkerInstanceGroupId,
});
return;
}
return workerGroup;
}
async getDefaultWorkerGroupForProject({
projectId,
regionOverride,
}: {
projectId: string;
regionOverride?: string;
}): Promise<WorkerInstanceGroup | undefined> {
const project = await this._prisma.project.findFirst({
where: {
id: projectId,
},
include: {
defaultWorkerGroup: true,
organization: { select: { featureFlags: true } },
},
});
if (!project) {
throw new Error("Project not found.");
}
// If they've specified a region, we need to check they have access to it
if (regionOverride) {
const workerGroup = await this._prisma.workerInstanceGroup.findFirst({
where: {
masterQueue: regionOverride,
},
});
if (!workerGroup) {
throw new Error(`The region you specified doesn't exist ("${regionOverride}").`);
}
// If they're restricted, check they have access
if (project.allowedWorkerQueues.length > 0) {
if (project.allowedWorkerQueues.includes(workerGroup.masterQueue)) {
return workerGroup;
}
throw new Error(
`You don't have access to this region ("${regionOverride}"). You can use the following regions: ${project.allowedWorkerQueues.join(
", "
)}.`
);
}
if (workerGroup.hidden) {
throw new Error(`The region you specified isn't available to you ("${regionOverride}").`);
}
if (workerGroup.workloadType === "MICROVM") {
const hasComputeAccess = await resolveComputeAccess(
this._prisma,
project.organization.featureFlags
);
if (!isComputeRegionAccessible(workerGroup, hasComputeAccess)) {
throw new Error(`The region you specified isn't available to you ("${regionOverride}").`);
}
}
return workerGroup;
}
if (project.defaultWorkerGroup) {
return project.defaultWorkerGroup;
}
return await this.getGlobalDefaultWorkerGroup();
}
async setDefaultWorkerGroupForProject({
projectId,
workerGroupId,
}: {
projectId: string;
workerGroupId: string;
}) {
const workerGroup = await this._prisma.workerInstanceGroup.findUnique({
where: {
id: workerGroupId,
},
});
if (!workerGroup) {
logger.error("[WorkerGroupService] WorkerGroup not found", {
workerGroupId,
});
return;
}
await this._prisma.project.update({
where: {
id: projectId,
},
data: {
defaultWorkerGroupId: workerGroupId,
},
});
}
private async generateWorkerName({ projectId }: { projectId?: string }) {
const workerGroups = await this._prisma.workerInstanceGroup.count({
where: {
projectId: projectId ?? null,
},
});
return `${this.defaultNamePrefix}_${workerGroups + 1}`;
}
private generateMasterQueueName({ projectId, name }: { projectId?: string; name: string }) {
if (!projectId) {
return name;
}
return `${projectId}-${name}`;
}
}