-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmanaged-index-controller.ts
More file actions
157 lines (131 loc) · 4.44 KB
/
managed-index-controller.ts
File metadata and controls
157 lines (131 loc) · 4.44 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
import {
BuildManifest,
CreateBackgroundWorkerRequestBody,
serializeIndexingError,
} from "@trigger.dev/core/v3";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { env } from "std-env";
import { CliApiClient } from "../apiClient.js";
import { indexWorkerManifest } from "../indexing/indexWorkerManifest.js";
import { resolveSourceFiles } from "../utilities/sourceFiles.js";
import { execOptionsForRuntime } from "@trigger.dev/core/v3/build";
import { writeJSONFile } from "../utilities/fileSystem.js";
async function loadBuildManifest() {
const manifestContents = await readFile("./build.json", "utf-8");
const raw = JSON.parse(manifestContents);
return BuildManifest.parse(raw);
}
async function bootstrap() {
const buildManifest = await loadBuildManifest();
if (typeof env.TRIGGER_API_URL !== "string") {
console.error("TRIGGER_API_URL is not set");
process.exit(1);
}
const cliApiClient = new CliApiClient(
env.TRIGGER_API_URL,
env.TRIGGER_SECRET_KEY,
env.TRIGGER_PREVIEW_BRANCH
);
if (!env.TRIGGER_PROJECT_REF) {
console.error("TRIGGER_PROJECT_REF is not set");
process.exit(1);
}
if (!env.TRIGGER_DEPLOYMENT_ID) {
console.error("TRIGGER_DEPLOYMENT_ID is not set");
process.exit(1);
}
return {
buildManifest,
cliApiClient,
projectRef: env.TRIGGER_PROJECT_REF,
deploymentId: env.TRIGGER_DEPLOYMENT_ID,
};
}
type BootstrapResult = Awaited<ReturnType<typeof bootstrap>>;
async function indexDeployment({
cliApiClient,
projectRef,
deploymentId,
buildManifest,
}: BootstrapResult) {
const stdout: string[] = [];
const stderr: string[] = [];
try {
const $env = await cliApiClient.getEnvironmentVariables(projectRef);
if (!$env.success) {
throw new Error(`Failed to fetch environment variables: ${$env.error}`);
}
const workerManifest = await indexWorkerManifest({
runtime: buildManifest.runtime,
indexWorkerPath: buildManifest.indexWorkerEntryPoint,
buildManifestPath: "./build.json",
nodeOptions: execOptionsForRuntime(buildManifest.runtime, buildManifest),
env: $env.data.variables,
otelHookExclude: buildManifest.otelImportHook?.exclude,
otelHookInclude: buildManifest.otelImportHook?.include,
handleStdout(data) {
stdout.push(data);
},
handleStderr(data) {
if (!data.includes("DeprecationWarning")) {
stderr.push(data);
}
},
});
console.log("Writing index.json", process.cwd());
const { timings, ...manifestWithoutTimings } = workerManifest;
await writeJSONFile(join(process.cwd(), "index.json"), manifestWithoutTimings, true);
const sourceFiles = resolveSourceFiles(buildManifest.sources, workerManifest.tasks);
const buildPlatform = process.env.BUILDPLATFORM;
const targetPlatform = process.env.TARGETPLATFORM;
const backgroundWorkerBody: CreateBackgroundWorkerRequestBody = {
localOnly: true,
metadata: {
contentHash: buildManifest.contentHash,
packageVersion: buildManifest.packageVersion,
cliPackageVersion: buildManifest.cliPackageVersion,
tasks: workerManifest.tasks,
queues: workerManifest.queues,
sourceFiles,
runtime: workerManifest.runtime,
runtimeVersion: workerManifest.runtimeVersion,
},
engine: "V2",
supportsLazyAttempts: true,
buildPlatform,
targetPlatform,
};
const createResponse = await cliApiClient.createDeploymentBackgroundWorker(
deploymentId,
backgroundWorkerBody
);
if (!createResponse.success) {
console.error(
JSON.stringify({
message: "Failed to create background worker",
buildPlatform,
targetPlatform,
error: createResponse.error,
})
);
// Do NOT fail the deployment, this may be a multi-platform deployment
return;
}
console.log(
JSON.stringify({
message: "Background worker created",
buildPlatform,
targetPlatform,
createResponse: createResponse.data,
})
);
} catch (error) {
const serialiedIndexError = serializeIndexingError(error, stderr.join("\n"));
console.error("Failed to index deployment", serialiedIndexError);
await cliApiClient.failDeployment(deploymentId, { error: serialiedIndexError });
process.exit(1);
}
}
const results = await bootstrap();
await indexDeployment(results);