-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbackgroundWorker.ts
More file actions
81 lines (68 loc) · 2.47 KB
/
backgroundWorker.ts
File metadata and controls
81 lines (68 loc) · 2.47 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
import { BuildManifest, ServerBackgroundWorker, WorkerManifest } from "@trigger.dev/core/v3";
import { execOptionsForRuntime } from "@trigger.dev/core/v3/build";
import { join } from "node:path";
import { indexWorkerManifest } from "../indexing/indexWorkerManifest.js";
import { prettyError } from "../utilities/cliOutput.js";
import { writeJSONFile } from "../utilities/fileSystem.js";
import { logger } from "../utilities/logger.js";
import type { Metafile } from "esbuild";
export type BackgroundWorkerOptions = {
env: Record<string, string>;
cwd: string;
stop: () => void;
};
export class BackgroundWorker {
public deprecated: boolean = false;
public manifest: WorkerManifest | undefined;
public serverWorker: ServerBackgroundWorker | undefined;
constructor(
public build: BuildManifest,
public metafile: Metafile,
public params: BackgroundWorkerOptions
) {}
deprecate() {
this.deprecated = true;
}
get workerManifestPath(): string {
return join(this.build.outputPath, "index.json");
}
get buildManifestPath(): string {
return join(this.build.outputPath, "build.json");
}
stop() {
logger.debug("[BackgroundWorker] Stopping worker", {
version: this.serverWorker?.version,
outputPath: this.build.outputPath,
});
this.params.stop();
}
async initialize() {
if (this.manifest) {
throw new Error("Worker already initialized");
}
// Write the build manifest to this.build.outputPath/build.json
await writeJSONFile(this.buildManifestPath, this.build, true);
logger.debug("indexing worker manifest", { build: this.build, params: this.params });
this.manifest = await indexWorkerManifest({
runtime: this.build.runtime,
indexWorkerPath: this.build.indexWorkerEntryPoint,
buildManifestPath: this.buildManifestPath,
nodeOptions: execOptionsForRuntime(this.build.runtime, this.build),
env: this.params.env,
cwd: this.params.cwd,
otelHookInclude: this.build.otelImportHook?.include,
otelHookExclude: this.build.otelImportHook?.exclude,
handleStdout(data) {
logger.debug(data);
},
handleStderr(data) {
if (!data.includes("Debugger attached")) {
prettyError(data.toString());
}
},
});
// Write the build manifest to this.build.outputPath/worker.json
await writeJSONFile(this.workerManifestPath, this.manifest, true);
logger.debug("worker manifest indexed", { path: this.build.outputPath });
}
}