-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbootstrap.ts
More file actions
75 lines (60 loc) · 1.76 KB
/
bootstrap.ts
File metadata and controls
75 lines (60 loc) · 1.76 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
import { mkdir, writeFile } from "fs/promises";
import { prisma } from "./db.server";
import { env } from "./env.server";
import { WorkerGroupService } from "./v3/services/worker/workerGroupService.server";
import { dirname } from "path";
import { tryCatch } from "@trigger.dev/core";
export async function bootstrap() {
if (env.TRIGGER_BOOTSTRAP_ENABLED !== "1") {
return;
}
if (env.TRIGGER_BOOTSTRAP_WORKER_GROUP_NAME) {
const [error] = await tryCatch(createWorkerGroup());
if (error) {
console.error("Failed to create worker group", { error });
}
}
}
async function createWorkerGroup() {
const workerGroupName = env.TRIGGER_BOOTSTRAP_WORKER_GROUP_NAME;
const tokenPath = env.TRIGGER_BOOTSTRAP_WORKER_TOKEN_PATH;
const existingWorkerGroup = await prisma.workerInstanceGroup.findFirst({
where: {
name: workerGroupName,
},
});
if (existingWorkerGroup) {
console.warn(`[bootstrap] Worker group ${workerGroupName} already exists`);
return;
}
const service = new WorkerGroupService();
const { token, workerGroup } = await service.createWorkerGroup({
name: workerGroupName,
});
console.log(`
==========================
AirTrigger Bootstrap - Worker Token
WARNING: This will only be shown once. Save it now!
Worker group:
${workerGroup.name}
Token:
${token.plaintext}
If using docker compose, set:
TRIGGER_WORKER_TOKEN=${token.plaintext}
${
tokenPath
? `Or, if using a file:
TRIGGER_WORKER_TOKEN=file://${tokenPath}`
: ""
}
==========================
`);
if (tokenPath) {
const dir = dirname(tokenPath);
await mkdir(dir, { recursive: true });
await writeFile(tokenPath, token.plaintext, {
mode: 0o600,
});
console.log(`[bootstrap] Worker token saved to ${tokenPath}`);
}
}