-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathdev-index-worker.ts
188 lines (164 loc) · 4.84 KB
/
dev-index-worker.ts
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
import {
BuildManifest,
type HandleErrorFunction,
indexerToWorkerMessages,
resourceCatalog,
type TaskManifest,
TriggerConfig,
} from "@trigger.dev/core/v3";
import {
StandardResourceCatalog,
TracingDiagnosticLogLevel,
TracingSDK,
} from "@trigger.dev/core/v3/workers";
import { sendMessageInCatalog, ZodSchemaParsedError } from "@trigger.dev/core/v3/zodMessageHandler";
import { readFile } from "node:fs/promises";
import sourceMapSupport from "source-map-support";
import { registerResources } from "../indexing/registerResources.js";
import { env } from "std-env";
import { normalizeImportPath } from "../utilities/normalizeImportPath.js";
sourceMapSupport.install({
handleUncaughtExceptions: false,
environment: "node",
hookRequire: false,
});
process.on("uncaughtException", function (error, origin) {
if (error instanceof Error) {
process.send &&
process.send({
type: "UNCAUGHT_EXCEPTION",
payload: {
error: { name: error.name, message: error.message, stack: error.stack },
origin,
},
version: "v1",
});
} else {
process.send &&
process.send({
type: "UNCAUGHT_EXCEPTION",
payload: {
error: {
name: "Error",
message: typeof error === "string" ? error : JSON.stringify(error),
},
origin,
},
version: "v1",
});
}
});
resourceCatalog.setGlobalResourceCatalog(new StandardResourceCatalog());
async function importConfig(
configPath: string
): Promise<{ config: TriggerConfig; handleError?: HandleErrorFunction }> {
const configModule = await import(normalizeImportPath(configPath));
const config = configModule?.default ?? configModule?.config;
return {
config,
handleError: configModule?.handleError,
};
}
async function loadBuildManifest() {
const manifestContents = await readFile(env.TRIGGER_BUILD_MANIFEST_PATH!, "utf-8");
const raw = JSON.parse(manifestContents);
return BuildManifest.parse(raw);
}
async function bootstrap() {
const buildManifest = await loadBuildManifest();
const { config } = await importConfig(buildManifest.configPath);
// This needs to run or the PrismaInstrumentation will throw an error
const tracingSDK = new TracingSDK({
url: env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318",
instrumentations: config.instrumentations ?? [],
diagLogLevel: (env.OTEL_LOG_LEVEL as TracingDiagnosticLogLevel) ?? "none",
forceFlushTimeoutMillis: 30_000,
});
const importErrors = await registerResources(buildManifest);
return {
tracingSDK,
config,
buildManifest,
importErrors,
};
}
const { buildManifest, importErrors, config } = await bootstrap();
let tasks = resourceCatalog.listTaskManifests();
// If the config has retry defaults, we need to apply them to all tasks that don't have any retry settings
if (config.retries?.default) {
tasks = tasks.map((task) => {
if (!task.retry) {
return {
...task,
retry: config.retries?.default,
} satisfies TaskManifest;
}
return task;
});
}
// If the config has a maxDuration, we need to apply it to all tasks that don't have a maxDuration
if (typeof config.maxDuration === "number") {
tasks = tasks.map((task) => {
if (typeof task.maxDuration !== "number") {
return {
...task,
maxDuration: config.maxDuration,
} satisfies TaskManifest;
}
return task;
});
}
// If the config has a machine preset, we need to apply it to all tasks that don't have a machine preset
if (typeof config.machine === "string") {
tasks = tasks.map((task) => {
if (typeof task.machine?.preset !== "string") {
return {
...task,
machine: {
preset: config.machine,
},
} satisfies TaskManifest;
}
return task;
});
}
await sendMessageInCatalog(
indexerToWorkerMessages,
"INDEX_COMPLETE",
{
manifest: {
tasks,
queues: resourceCatalog.listQueueManifests(),
configPath: buildManifest.configPath,
runtime: buildManifest.runtime,
workerEntryPoint: buildManifest.runWorkerEntryPoint,
controllerEntryPoint: buildManifest.runControllerEntryPoint,
loaderEntryPoint: buildManifest.loaderEntryPoint,
customConditions: buildManifest.customConditions,
initEntryPoint: buildManifest.initEntryPoint,
},
importErrors,
},
async (msg) => {
process.send?.(msg);
}
).catch((err) => {
if (err instanceof ZodSchemaParsedError) {
return sendMessageInCatalog(
indexerToWorkerMessages,
"TASKS_FAILED_TO_PARSE",
{ zodIssues: err.error.issues, tasks },
async (msg) => {
process.send?.(msg);
}
);
} else {
console.error("Failed to send TASKS_READY message", err);
}
return;
});
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 10);
});