trigger.dev 3.0.0-beta.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/dist/esm/apiClient.d.ts +190 -0
  2. package/dist/esm/apiClient.js +239 -0
  3. package/dist/esm/apiClient.js.map +1 -0
  4. package/dist/esm/build/buildWorker.d.ts +68 -0
  5. package/dist/esm/build/buildWorker.js +159 -0
  6. package/dist/esm/build/buildWorker.js.map +1 -0
  7. package/dist/esm/build/bundle.d.ts +36 -0
  8. package/dist/esm/build/bundle.js +197 -0
  9. package/dist/esm/build/bundle.js.map +1 -0
  10. package/dist/esm/build/extensions.d.ts +15 -0
  11. package/dist/esm/build/extensions.js +158 -0
  12. package/dist/esm/build/extensions.js.map +1 -0
  13. package/dist/esm/build/externals.d.ts +15 -0
  14. package/dist/esm/build/externals.js +285 -0
  15. package/dist/esm/build/externals.js.map +1 -0
  16. package/dist/esm/build/manifests.d.ts +2 -0
  17. package/dist/esm/build/manifests.js +22 -0
  18. package/dist/esm/build/manifests.js.map +1 -0
  19. package/dist/esm/build/packageModules.d.ts +18 -0
  20. package/dist/esm/build/packageModules.js +84 -0
  21. package/dist/esm/build/packageModules.js.map +1 -0
  22. package/dist/esm/build/plugins.d.ts +6 -0
  23. package/dist/esm/build/plugins.js +67 -0
  24. package/dist/esm/build/plugins.js.map +1 -0
  25. package/dist/esm/build/resolveModule.d.ts +1 -0
  26. package/dist/esm/build/resolveModule.js +9 -0
  27. package/dist/esm/build/resolveModule.js.map +1 -0
  28. package/dist/esm/cli/common.d.ts +30 -0
  29. package/dist/esm/cli/common.js +85 -0
  30. package/dist/esm/cli/common.js.map +1 -0
  31. package/dist/esm/cli/index.d.ts +2 -0
  32. package/dist/esm/cli/index.js +27 -0
  33. package/dist/esm/cli/index.js.map +1 -0
  34. package/dist/esm/commands/deploy.d.ts +5 -0
  35. package/dist/esm/commands/deploy.js +402 -0
  36. package/dist/esm/commands/deploy.js.map +1 -0
  37. package/dist/esm/commands/dev.d.ts +37 -0
  38. package/dist/esm/commands/dev.js +128 -0
  39. package/dist/esm/commands/dev.js.map +1 -0
  40. package/dist/esm/commands/init.d.ts +3 -0
  41. package/dist/esm/commands/init.js +427 -0
  42. package/dist/esm/commands/init.js.map +1 -0
  43. package/dist/esm/commands/list-profiles.d.ts +23 -0
  44. package/dist/esm/commands/list-profiles.js +40 -0
  45. package/dist/esm/commands/list-profiles.js.map +1 -0
  46. package/dist/esm/commands/login.d.ts +28 -0
  47. package/dist/esm/commands/login.js +275 -0
  48. package/dist/esm/commands/login.js.map +1 -0
  49. package/dist/esm/commands/logout.d.ts +23 -0
  50. package/dist/esm/commands/logout.js +28 -0
  51. package/dist/esm/commands/logout.js.map +1 -0
  52. package/dist/esm/commands/update.d.ts +24 -0
  53. package/dist/esm/commands/update.js +221 -0
  54. package/dist/esm/commands/update.js.map +1 -0
  55. package/dist/esm/commands/whoami.d.ts +34 -0
  56. package/dist/esm/commands/whoami.js +70 -0
  57. package/dist/esm/commands/whoami.js.map +1 -0
  58. package/dist/esm/config.d.ts +22 -0
  59. package/dist/esm/config.js +204 -0
  60. package/dist/esm/config.js.map +1 -0
  61. package/dist/esm/consts.d.ts +4 -0
  62. package/dist/esm/consts.js +5 -0
  63. package/dist/esm/consts.js.map +1 -0
  64. package/dist/esm/deploy/buildImage.d.ts +69 -0
  65. package/dist/esm/deploy/buildImage.js +472 -0
  66. package/dist/esm/deploy/buildImage.js.map +1 -0
  67. package/dist/esm/deploy/logs.d.ts +19 -0
  68. package/dist/esm/deploy/logs.js +96 -0
  69. package/dist/esm/deploy/logs.js.map +1 -0
  70. package/dist/esm/dev/backgroundWorker.d.ts +114 -0
  71. package/dist/esm/dev/backgroundWorker.js +412 -0
  72. package/dist/esm/dev/backgroundWorker.js.map +1 -0
  73. package/dist/esm/dev/devOutput.d.ts +9 -0
  74. package/dist/esm/dev/devOutput.js +137 -0
  75. package/dist/esm/dev/devOutput.js.map +1 -0
  76. package/dist/esm/dev/devSession.d.ts +17 -0
  77. package/dist/esm/dev/devSession.js +156 -0
  78. package/dist/esm/dev/devSession.js.map +1 -0
  79. package/dist/esm/dev/workerRuntime.d.ts +16 -0
  80. package/dist/esm/dev/workerRuntime.js +258 -0
  81. package/dist/esm/dev/workerRuntime.js.map +1 -0
  82. package/dist/esm/entryPoints/deploy-index-controller.d.ts +1 -0
  83. package/dist/esm/entryPoints/deploy-index-controller.js +85 -0
  84. package/dist/esm/entryPoints/deploy-index-controller.js.map +1 -0
  85. package/dist/esm/entryPoints/deploy-index-worker.d.ts +1 -0
  86. package/dist/esm/entryPoints/deploy-index-worker.js +104 -0
  87. package/dist/esm/entryPoints/deploy-index-worker.js.map +1 -0
  88. package/dist/esm/entryPoints/deploy-run-controller.d.ts +1 -0
  89. package/dist/esm/entryPoints/deploy-run-controller.js +996 -0
  90. package/dist/esm/entryPoints/deploy-run-controller.js.map +1 -0
  91. package/dist/esm/entryPoints/deploy-run-worker.d.ts +1 -0
  92. package/dist/esm/entryPoints/deploy-run-worker.js +297 -0
  93. package/dist/esm/entryPoints/deploy-run-worker.js.map +1 -0
  94. package/dist/esm/entryPoints/dev-index-worker.d.ts +1 -0
  95. package/dist/esm/entryPoints/dev-index-worker.js +104 -0
  96. package/dist/esm/entryPoints/dev-index-worker.js.map +1 -0
  97. package/dist/esm/entryPoints/dev-run-worker.d.ts +1 -0
  98. package/dist/esm/entryPoints/dev-run-worker.js +254 -0
  99. package/dist/esm/entryPoints/dev-run-worker.js.map +1 -0
  100. package/dist/esm/entryPoints/loader.d.ts +1 -0
  101. package/dist/esm/entryPoints/loader.js +16 -0
  102. package/dist/esm/entryPoints/loader.js.map +1 -0
  103. package/dist/esm/executions/taskRunProcess.d.ts +115 -0
  104. package/dist/esm/executions/taskRunProcess.js +254 -0
  105. package/dist/esm/executions/taskRunProcess.js.map +1 -0
  106. package/dist/esm/imports/magicast.d.ts +2 -0
  107. package/dist/esm/imports/magicast.js +5 -0
  108. package/dist/esm/imports/magicast.js.map +1 -0
  109. package/dist/esm/imports/xdg-app-paths.d.ts +2 -0
  110. package/dist/esm/imports/xdg-app-paths.js +3 -0
  111. package/dist/esm/imports/xdg-app-paths.js.map +1 -0
  112. package/dist/{index.d.ts → esm/index.d.ts} +1 -0
  113. package/dist/esm/index.js +17 -0
  114. package/dist/esm/index.js.map +1 -0
  115. package/dist/esm/indexing/indexWorkerManifest.d.ts +85 -0
  116. package/dist/esm/indexing/indexWorkerManifest.js +76 -0
  117. package/dist/esm/indexing/indexWorkerManifest.js.map +1 -0
  118. package/dist/esm/indexing/registerTasks.d.ts +2 -0
  119. package/dist/esm/indexing/registerTasks.js +62 -0
  120. package/dist/esm/indexing/registerTasks.js.map +1 -0
  121. package/dist/esm/package.json +3 -0
  122. package/dist/esm/runtimes/bun.d.ts +2 -0
  123. package/dist/esm/runtimes/bun.js +11 -0
  124. package/dist/esm/runtimes/bun.js.map +1 -0
  125. package/dist/esm/shims/esm.d.ts +1 -0
  126. package/dist/esm/shims/esm.js +9 -0
  127. package/dist/esm/shims/esm.js.map +1 -0
  128. package/dist/esm/sourceDir.d.ts +1 -0
  129. package/dist/esm/sourceDir.js +4 -0
  130. package/dist/esm/sourceDir.js.map +1 -0
  131. package/dist/esm/telemetry/tracing.d.ts +3 -0
  132. package/dist/esm/telemetry/tracing.js +58 -0
  133. package/dist/esm/telemetry/tracing.js.map +1 -0
  134. package/dist/esm/types.d.ts +6 -0
  135. package/dist/esm/types.js +2 -0
  136. package/dist/esm/types.js.map +1 -0
  137. package/dist/esm/utilities/assertExhaustive.d.ts +1 -0
  138. package/dist/esm/utilities/assertExhaustive.js +4 -0
  139. package/dist/esm/utilities/assertExhaustive.js.map +1 -0
  140. package/dist/esm/utilities/buildManifest.d.ts +2 -0
  141. package/dist/esm/utilities/buildManifest.js +9 -0
  142. package/dist/esm/utilities/buildManifest.js.map +1 -0
  143. package/dist/esm/utilities/cliOutput.d.ts +20 -0
  144. package/dist/esm/utilities/cliOutput.js +88 -0
  145. package/dist/esm/utilities/cliOutput.js.map +1 -0
  146. package/dist/esm/utilities/configFiles.d.ts +29 -0
  147. package/dist/esm/utilities/configFiles.js +65 -0
  148. package/dist/esm/utilities/configFiles.js.map +1 -0
  149. package/dist/esm/utilities/createFileFromTemplate.d.ts +16 -0
  150. package/dist/esm/utilities/createFileFromTemplate.js +50 -0
  151. package/dist/esm/utilities/createFileFromTemplate.js.map +1 -0
  152. package/dist/esm/utilities/deployErrors.d.ts +19 -0
  153. package/dist/esm/utilities/deployErrors.js +117 -0
  154. package/dist/esm/utilities/deployErrors.js.map +1 -0
  155. package/dist/esm/utilities/dotEnv.d.ts +4 -0
  156. package/dist/esm/utilities/dotEnv.js +28 -0
  157. package/dist/esm/utilities/dotEnv.js.map +1 -0
  158. package/dist/esm/utilities/eventBus.d.ts +14 -0
  159. package/dist/esm/utilities/eventBus.js +3 -0
  160. package/dist/esm/utilities/eventBus.js.map +1 -0
  161. package/dist/esm/utilities/fileSystem.d.ts +12 -0
  162. package/dist/esm/utilities/fileSystem.js +81 -0
  163. package/dist/esm/utilities/fileSystem.js.map +1 -0
  164. package/dist/esm/utilities/getApiKeyType.d.ts +13 -0
  165. package/dist/esm/utilities/getApiKeyType.js +44 -0
  166. package/dist/esm/utilities/getApiKeyType.js.map +1 -0
  167. package/dist/esm/utilities/initialBanner.d.ts +4 -0
  168. package/dist/esm/utilities/initialBanner.js +82 -0
  169. package/dist/esm/utilities/initialBanner.js.map +1 -0
  170. package/dist/esm/utilities/keyValueBy.d.ts +8 -0
  171. package/dist/esm/utilities/keyValueBy.js +19 -0
  172. package/dist/esm/utilities/keyValueBy.js.map +1 -0
  173. package/dist/esm/utilities/links.d.ts +9 -0
  174. package/dist/esm/utilities/links.js +10 -0
  175. package/dist/esm/utilities/links.js.map +1 -0
  176. package/dist/esm/utilities/linux.d.ts +1 -0
  177. package/dist/esm/utilities/linux.js +24 -0
  178. package/dist/esm/utilities/linux.js.map +1 -0
  179. package/dist/esm/utilities/logger.d.ts +40 -0
  180. package/dist/esm/utilities/logger.js +109 -0
  181. package/dist/esm/utilities/logger.js.map +1 -0
  182. package/dist/esm/utilities/normalizeImportPath.d.ts +1 -0
  183. package/dist/esm/utilities/normalizeImportPath.js +5 -0
  184. package/dist/esm/utilities/normalizeImportPath.js.map +1 -0
  185. package/dist/esm/utilities/obfuscateApiKey.d.ts +1 -0
  186. package/dist/esm/utilities/obfuscateApiKey.js +5 -0
  187. package/dist/esm/utilities/obfuscateApiKey.js.map +1 -0
  188. package/dist/esm/utilities/parseNameAndPath.d.ts +2 -0
  189. package/dist/esm/utilities/parseNameAndPath.js +10 -0
  190. package/dist/esm/utilities/parseNameAndPath.js.map +1 -0
  191. package/dist/esm/utilities/resolveInternalFilePath.d.ts +1 -0
  192. package/dist/esm/utilities/resolveInternalFilePath.js +8 -0
  193. package/dist/esm/utilities/resolveInternalFilePath.js.map +1 -0
  194. package/dist/esm/utilities/runtimeCheck.d.ts +8 -0
  195. package/dist/esm/utilities/runtimeCheck.js +52 -0
  196. package/dist/esm/utilities/runtimeCheck.js.map +1 -0
  197. package/dist/esm/utilities/safeJsonParse.d.ts +1 -0
  198. package/dist/esm/utilities/safeJsonParse.js +12 -0
  199. package/dist/esm/utilities/safeJsonParse.js.map +1 -0
  200. package/dist/esm/utilities/sanitizeEnvVars.d.ts +3 -0
  201. package/dist/esm/utilities/sanitizeEnvVars.js +4 -0
  202. package/dist/esm/utilities/sanitizeEnvVars.js.map +1 -0
  203. package/dist/esm/utilities/session.d.ts +33 -0
  204. package/dist/esm/utilities/session.js +82 -0
  205. package/dist/esm/utilities/session.js.map +1 -0
  206. package/dist/esm/utilities/sourceFiles.d.ts +12 -0
  207. package/dist/esm/utilities/sourceFiles.js +76 -0
  208. package/dist/esm/utilities/sourceFiles.js.map +1 -0
  209. package/dist/esm/utilities/taskFiles.d.ts +6 -0
  210. package/dist/esm/utilities/taskFiles.js +69 -0
  211. package/dist/esm/utilities/taskFiles.js.map +1 -0
  212. package/dist/esm/utilities/tempDirectories.d.ts +16 -0
  213. package/dist/esm/utilities/tempDirectories.js +27 -0
  214. package/dist/esm/utilities/tempDirectories.js.map +1 -0
  215. package/dist/esm/utilities/windows.d.ts +7 -0
  216. package/dist/esm/utilities/windows.js +21 -0
  217. package/dist/esm/utilities/windows.js.map +1 -0
  218. package/dist/esm/version.d.ts +1 -0
  219. package/dist/esm/version.js +2 -0
  220. package/dist/esm/version.js.map +1 -0
  221. package/package.json +85 -84
  222. package/dist/Containerfile.prod +0 -39
  223. package/dist/index.js +0 -5963
  224. package/dist/index.js.map +0 -1
  225. package/dist/templates/examples/simple.ts.template +0 -14
  226. package/dist/templates/trigger.config.ts.template +0 -16
  227. package/dist/workers/dev/worker-facade.js +0 -187
  228. package/dist/workers/dev/worker-setup.js +0 -40
  229. package/dist/workers/prod/entry-point.js +0 -1146
  230. package/dist/workers/prod/worker-facade.js +0 -193
  231. package/dist/workers/prod/worker-setup.js +0 -35
@@ -1,1146 +0,0 @@
1
- // src/workers/prod/entry-point.ts
2
- import {
3
- CoordinatorToProdWorkerMessages,
4
- PostStartCauses,
5
- PreStopCauses,
6
- ProdWorkerToCoordinatorMessages,
7
- ZodSocketConnection as ZodSocketConnection2
8
- } from "@trigger.dev/core/v3";
9
-
10
- // ../core-apps/src/http.ts
11
- var HttpReply = class {
12
- constructor(response) {
13
- this.response = response;
14
- }
15
- empty(status) {
16
- return this.response.writeHead(status ?? 200).end();
17
- }
18
- text(text, status, contentType) {
19
- return this.response.writeHead(status ?? 200, { "Content-Type": contentType || "text/plain" }).end(text.endsWith("\n") ? text : `${text}
20
- `);
21
- }
22
- json(value, pretty) {
23
- return this.text(
24
- JSON.stringify(value, void 0, pretty ? 2 : void 0),
25
- 200,
26
- "application/json"
27
- );
28
- }
29
- };
30
- function getRandomInteger(min, max) {
31
- const intMin = Math.ceil(min);
32
- const intMax = Math.floor(max);
33
- return Math.floor(Math.random() * (intMax - intMin + 1)) + intMin;
34
- }
35
- function getRandomPortNumber() {
36
- return getRandomInteger(8e3, 9999);
37
- }
38
-
39
- // ../core-apps/src/logger.ts
40
- var SimpleLogger = class {
41
- constructor(prefix) {
42
- this.prefix = prefix;
43
- }
44
- #debugEnabled = ["1", "true"].includes(process.env.DEBUG ?? "");
45
- log(arg0, ...argN) {
46
- console.log(...this.#getPrefixedArgs(arg0, ...argN));
47
- return arg0;
48
- }
49
- debug(arg0, ...argN) {
50
- if (!this.#debugEnabled) {
51
- return arg0;
52
- }
53
- console.debug(...this.#getPrefixedArgs("DEBUG", arg0, ...argN));
54
- return arg0;
55
- }
56
- error(arg0, ...argN) {
57
- console.error(...this.#getPrefixedArgs(arg0, ...argN));
58
- return arg0;
59
- }
60
- #getPrefixedArgs(...args) {
61
- if (!this.prefix) {
62
- return args;
63
- }
64
- return [this.prefix, ...args];
65
- }
66
- };
67
-
68
- // ../core-apps/src/provider.ts
69
- import {
70
- ClientToSharedQueueMessages,
71
- clientWebsocketMessages,
72
- PlatformToProviderMessages,
73
- ProviderToPlatformMessages,
74
- SharedQueueToClientMessages,
75
- ZodMessageSender,
76
- ZodSocketConnection
77
- } from "@trigger.dev/core/v3";
78
- var HTTP_SERVER_PORT = Number(process.env.HTTP_SERVER_PORT || getRandomPortNumber());
79
- var MACHINE_NAME = process.env.MACHINE_NAME || "local";
80
- var PLATFORM_HOST = process.env.PLATFORM_HOST || "127.0.0.1";
81
- var PLATFORM_WS_PORT = process.env.PLATFORM_WS_PORT || 3030;
82
- var PLATFORM_SECRET = process.env.PLATFORM_SECRET || "provider-secret";
83
- var SECURE_CONNECTION = ["1", "true"].includes(process.env.SECURE_CONNECTION ?? "false");
84
- var logger = new SimpleLogger(`[${MACHINE_NAME}]`);
85
-
86
- // src/workers/prod/entry-point.ts
87
- import { readFile } from "node:fs/promises";
88
- import { createServer } from "node:http";
89
-
90
- // src/workers/prod/backgroundWorker.ts
91
- import {
92
- ProdChildToWorkerMessages,
93
- ProdWorkerToChildMessages,
94
- SemanticInternalAttributes,
95
- TaskRunErrorCodes,
96
- ZodIpcConnection,
97
- correctErrorStackTrace
98
- } from "@trigger.dev/core/v3";
99
- import { Evt } from "evt";
100
- import { fork } from "node:child_process";
101
-
102
- // src/workers/common/errors.ts
103
- var UncaughtExceptionError = class extends Error {
104
- constructor(originalError, origin) {
105
- super(`Uncaught exception: ${originalError.message}`);
106
- this.originalError = originalError;
107
- this.origin = origin;
108
- this.name = "UncaughtExceptionError";
109
- }
110
- };
111
- var TaskMetadataParseError = class extends Error {
112
- constructor(zodIssues, tasks) {
113
- super(`Failed to parse task metadata`);
114
- this.zodIssues = zodIssues;
115
- this.tasks = tasks;
116
- this.name = "TaskMetadataParseError";
117
- }
118
- };
119
-
120
- // src/workers/prod/backgroundWorker.ts
121
- var UnexpectedExitError = class extends Error {
122
- constructor(code) {
123
- super(`Unexpected exit with code ${code}`);
124
- this.code = code;
125
- this.name = "UnexpectedExitError";
126
- }
127
- };
128
- var CleanupProcessError = class extends Error {
129
- constructor() {
130
- super("Cancelled");
131
- this.name = "CleanupProcessError";
132
- }
133
- };
134
- var CancelledProcessError = class extends Error {
135
- constructor() {
136
- super("Cancelled");
137
- this.name = "CancelledProcessError";
138
- }
139
- };
140
- var ProdBackgroundWorker = class {
141
- constructor(path, params) {
142
- this.path = path;
143
- this.params = params;
144
- }
145
- _initialized = false;
146
- onTaskHeartbeat = new Evt();
147
- onWaitForBatch = new Evt();
148
- onWaitForDuration = new Evt();
149
- onWaitForTask = new Evt();
150
- preCheckpointNotification = Evt.create();
151
- onReadyForCheckpoint = Evt.create();
152
- onCancelCheckpoint = Evt.create();
153
- _onClose = new Evt();
154
- tasks = [];
155
- _taskRunProcess;
156
- _closed = false;
157
- async close() {
158
- if (this._closed) {
159
- return;
160
- }
161
- this._closed = true;
162
- this.onTaskHeartbeat.detach();
163
- await this._taskRunProcess?.cleanup(true);
164
- }
165
- async flushTelemetry() {
166
- await this._taskRunProcess?.cleanup(false);
167
- }
168
- async initialize(options) {
169
- if (this._initialized) {
170
- throw new Error("Worker already initialized");
171
- }
172
- let resolved = false;
173
- this.tasks = await new Promise((resolve, reject) => {
174
- const child = fork(this.path, {
175
- stdio: [
176
- /*stdin*/
177
- "ignore",
178
- /*stdout*/
179
- "pipe",
180
- /*stderr*/
181
- "pipe",
182
- "ipc"
183
- ],
184
- env: {
185
- ...this.params.env,
186
- ...options?.env
187
- }
188
- });
189
- const timeout = setTimeout(() => {
190
- if (resolved) {
191
- return;
192
- }
193
- resolved = true;
194
- child.kill();
195
- reject(new Error("Worker timed out"));
196
- }, 1e4);
197
- new ZodIpcConnection({
198
- listenSchema: ProdChildToWorkerMessages,
199
- emitSchema: ProdWorkerToChildMessages,
200
- process: child,
201
- handlers: {
202
- TASKS_READY: async (message) => {
203
- if (!resolved) {
204
- clearTimeout(timeout);
205
- resolved = true;
206
- resolve(message.tasks);
207
- child.kill();
208
- }
209
- },
210
- UNCAUGHT_EXCEPTION: async (message) => {
211
- if (!resolved) {
212
- clearTimeout(timeout);
213
- resolved = true;
214
- reject(new UncaughtExceptionError(message.error, message.origin));
215
- child.kill();
216
- }
217
- },
218
- TASKS_FAILED_TO_PARSE: async (message) => {
219
- if (!resolved) {
220
- clearTimeout(timeout);
221
- resolved = true;
222
- reject(new TaskMetadataParseError(message.zodIssues, message.tasks));
223
- child.kill();
224
- }
225
- }
226
- }
227
- });
228
- child.stdout?.on("data", (data) => {
229
- console.log(data.toString());
230
- });
231
- child.stderr?.on("data", (data) => {
232
- console.error(data.toString());
233
- });
234
- child.on("exit", (code) => {
235
- if (!resolved) {
236
- clearTimeout(timeout);
237
- resolved = true;
238
- reject(new Error(`Worker exited with code ${code}`));
239
- }
240
- });
241
- });
242
- this._initialized = true;
243
- }
244
- getMetadata(workerId, version) {
245
- return {
246
- contentHash: this.params.contentHash,
247
- id: workerId,
248
- version
249
- };
250
- }
251
- // We need to notify all the task run processes that a task run has completed,
252
- // in case they are waiting for it through triggerAndWait
253
- async taskRunCompletedNotification(completion, execution) {
254
- this._taskRunProcess?.taskRunCompletedNotification(completion, execution);
255
- }
256
- async waitCompletedNotification() {
257
- this._taskRunProcess?.waitCompletedNotification();
258
- }
259
- async #initializeTaskRunProcess(payload) {
260
- const metadata = this.getMetadata(
261
- payload.execution.worker.id,
262
- payload.execution.worker.version
263
- );
264
- if (!this._taskRunProcess) {
265
- const taskRunProcess = new TaskRunProcess(
266
- payload.execution,
267
- this.path,
268
- {
269
- ...this.params.env,
270
- ...payload.environment ?? {}
271
- },
272
- metadata,
273
- this.params
274
- );
275
- taskRunProcess.onExit.attach(() => {
276
- this._taskRunProcess = void 0;
277
- });
278
- taskRunProcess.onTaskHeartbeat.attach((id) => {
279
- this.onTaskHeartbeat.post(id);
280
- });
281
- taskRunProcess.onWaitForBatch.attach((message) => {
282
- this.onWaitForBatch.post(message);
283
- });
284
- taskRunProcess.onWaitForDuration.attach((message) => {
285
- this.onWaitForDuration.post(message);
286
- });
287
- taskRunProcess.onWaitForTask.attach((message) => {
288
- this.onWaitForTask.post(message);
289
- });
290
- taskRunProcess.onReadyForCheckpoint.attach((message) => {
291
- this.onReadyForCheckpoint.post(message);
292
- });
293
- taskRunProcess.onCancelCheckpoint.attach((message) => {
294
- this.onCancelCheckpoint.post(message);
295
- });
296
- this.preCheckpointNotification.attach((message) => {
297
- taskRunProcess.preCheckpointNotification.post(message);
298
- });
299
- await taskRunProcess.initialize();
300
- this._taskRunProcess = taskRunProcess;
301
- }
302
- return this._taskRunProcess;
303
- }
304
- // We need to fork the process before we can execute any tasks
305
- async executeTaskRun(payload) {
306
- try {
307
- const taskRunProcess = await this.#initializeTaskRunProcess(payload);
308
- const result = await taskRunProcess.executeTaskRun(payload);
309
- if (result.ok) {
310
- return result;
311
- }
312
- const error = result.error;
313
- if (error.type === "BUILT_IN_ERROR") {
314
- const mappedError = await this.#correctError(error, payload.execution);
315
- return {
316
- ...result,
317
- error: mappedError
318
- };
319
- }
320
- return result;
321
- } catch (e) {
322
- if (e instanceof CancelledProcessError) {
323
- return {
324
- id: payload.execution.attempt.id,
325
- ok: false,
326
- retry: void 0,
327
- error: {
328
- type: "INTERNAL_ERROR",
329
- code: TaskRunErrorCodes.TASK_RUN_CANCELLED
330
- }
331
- };
332
- }
333
- if (e instanceof CleanupProcessError) {
334
- return {
335
- id: payload.execution.attempt.id,
336
- ok: false,
337
- retry: void 0,
338
- error: {
339
- type: "INTERNAL_ERROR",
340
- code: TaskRunErrorCodes.TASK_EXECUTION_ABORTED
341
- }
342
- };
343
- }
344
- if (e instanceof UnexpectedExitError) {
345
- return {
346
- id: payload.execution.attempt.id,
347
- ok: false,
348
- retry: void 0,
349
- error: {
350
- type: "INTERNAL_ERROR",
351
- code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE
352
- }
353
- };
354
- }
355
- return {
356
- id: payload.execution.attempt.id,
357
- ok: false,
358
- retry: void 0,
359
- error: {
360
- type: "INTERNAL_ERROR",
361
- code: TaskRunErrorCodes.TASK_EXECUTION_FAILED
362
- }
363
- };
364
- }
365
- }
366
- async cancelAttempt(attemptId) {
367
- await this._taskRunProcess?.cancel();
368
- }
369
- async #correctError(error, execution) {
370
- return {
371
- ...error,
372
- stackTrace: correctErrorStackTrace(error.stackTrace, this.params.projectConfig.projectDir)
373
- };
374
- }
375
- };
376
- var TaskRunProcess = class {
377
- constructor(execution, path, env, metadata, worker) {
378
- this.execution = execution;
379
- this.path = path;
380
- this.env = env;
381
- this.metadata = metadata;
382
- this.worker = worker;
383
- }
384
- _ipc;
385
- _child;
386
- _attemptPromises = /* @__PURE__ */ new Map();
387
- _attemptStatuses = /* @__PURE__ */ new Map();
388
- _currentExecution;
389
- _isBeingKilled = false;
390
- _isBeingCancelled = false;
391
- onTaskHeartbeat = new Evt();
392
- onExit = new Evt();
393
- onWaitForBatch = new Evt();
394
- onWaitForDuration = new Evt();
395
- onWaitForTask = new Evt();
396
- preCheckpointNotification = Evt.create();
397
- onReadyForCheckpoint = Evt.create();
398
- onCancelCheckpoint = Evt.create();
399
- async initialize() {
400
- this._child = fork(this.path, {
401
- stdio: [
402
- /*stdin*/
403
- "ignore",
404
- /*stdout*/
405
- "pipe",
406
- /*stderr*/
407
- "pipe",
408
- "ipc"
409
- ],
410
- env: {
411
- ...this.execution.run.isTest ? { TRIGGER_LOG_LEVEL: "debug" } : {},
412
- ...this.env,
413
- OTEL_RESOURCE_ATTRIBUTES: JSON.stringify({
414
- [SemanticInternalAttributes.PROJECT_DIR]: this.worker.projectConfig.projectDir
415
- }),
416
- ...this.worker.debugOtel ? { OTEL_LOG_LEVEL: "debug" } : {}
417
- }
418
- });
419
- this._ipc = new ZodIpcConnection({
420
- listenSchema: ProdChildToWorkerMessages,
421
- emitSchema: ProdWorkerToChildMessages,
422
- process: this._child,
423
- handlers: {
424
- TASK_RUN_COMPLETED: async (message) => {
425
- const { result, execution } = message;
426
- const promiseStatus = this._attemptStatuses.get(execution.attempt.id);
427
- if (promiseStatus !== "PENDING") {
428
- return;
429
- }
430
- this._attemptStatuses.set(execution.attempt.id, "RESOLVED");
431
- const attemptPromise = this._attemptPromises.get(execution.attempt.id);
432
- if (!attemptPromise) {
433
- return;
434
- }
435
- const { resolver } = attemptPromise;
436
- resolver(result);
437
- },
438
- READY_TO_DISPOSE: async (message) => {
439
- process.exit(0);
440
- },
441
- TASK_HEARTBEAT: async (message) => {
442
- this.onTaskHeartbeat.post(message.id);
443
- },
444
- TASKS_READY: async (message) => {
445
- },
446
- WAIT_FOR_BATCH: async (message) => {
447
- this.onWaitForBatch.post(message);
448
- },
449
- WAIT_FOR_DURATION: async (message) => {
450
- this.onWaitForDuration.post(message);
451
- const { willCheckpointAndRestore } = await this.preCheckpointNotification.waitFor();
452
- return { willCheckpointAndRestore };
453
- },
454
- WAIT_FOR_TASK: async (message) => {
455
- this.onWaitForTask.post(message);
456
- },
457
- READY_FOR_CHECKPOINT: async (message) => {
458
- this.onReadyForCheckpoint.post(message);
459
- },
460
- CANCEL_CHECKPOINT: async (message) => {
461
- this.onCancelCheckpoint.post(message);
462
- }
463
- }
464
- });
465
- this._child.on("exit", this.#handleExit.bind(this));
466
- this._child.stdout?.on("data", this.#handleLog.bind(this));
467
- this._child.stderr?.on("data", this.#handleStdErr.bind(this));
468
- }
469
- async cancel() {
470
- this._isBeingCancelled = true;
471
- await this.cleanup(true);
472
- }
473
- async cleanup(kill = false) {
474
- if (kill && this._isBeingKilled) {
475
- return;
476
- }
477
- this._isBeingKilled = kill;
478
- await this._ipc?.sendWithAck("CLEANUP", {
479
- flush: true,
480
- kill
481
- });
482
- }
483
- async executeTaskRun(payload) {
484
- let resolver;
485
- let rejecter;
486
- const promise = new Promise((resolve, reject) => {
487
- resolver = resolve;
488
- rejecter = reject;
489
- });
490
- this._attemptStatuses.set(payload.execution.attempt.id, "PENDING");
491
- this._attemptPromises.set(payload.execution.attempt.id, { resolver, rejecter });
492
- const { execution, traceContext } = payload;
493
- this._currentExecution = execution;
494
- if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
495
- await this._ipc?.send("EXECUTE_TASK_RUN", {
496
- execution,
497
- traceContext,
498
- metadata: this.metadata
499
- });
500
- }
501
- const result = await promise;
502
- this._currentExecution = void 0;
503
- return result;
504
- }
505
- taskRunCompletedNotification(completion, execution) {
506
- if (!completion.ok && typeof completion.retry !== "undefined") {
507
- return;
508
- }
509
- if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
510
- this._ipc?.send("TASK_RUN_COMPLETED_NOTIFICATION", {
511
- completion,
512
- execution
513
- });
514
- }
515
- }
516
- waitCompletedNotification() {
517
- if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
518
- this._ipc?.send("WAIT_COMPLETED_NOTIFICATION", {});
519
- }
520
- }
521
- async #handleExit(code) {
522
- for (const [id, status] of this._attemptStatuses.entries()) {
523
- if (status === "PENDING") {
524
- this._attemptStatuses.set(id, "REJECTED");
525
- const attemptPromise = this._attemptPromises.get(id);
526
- if (!attemptPromise) {
527
- continue;
528
- }
529
- const { rejecter } = attemptPromise;
530
- if (this._isBeingCancelled) {
531
- rejecter(new CancelledProcessError());
532
- } else if (this._isBeingKilled) {
533
- rejecter(new CleanupProcessError());
534
- } else {
535
- rejecter(new UnexpectedExitError(code));
536
- }
537
- }
538
- }
539
- this.onExit.post(code);
540
- }
541
- #handleLog(data) {
542
- if (!this._currentExecution) {
543
- return;
544
- }
545
- console.log(
546
- `[${this.metadata.version}][${this._currentExecution.run.id}.${this._currentExecution.attempt.number}] ${data.toString()}`
547
- );
548
- }
549
- #handleStdErr(data) {
550
- if (this._isBeingKilled) {
551
- return;
552
- }
553
- if (!this._currentExecution) {
554
- console.error(`[${this.metadata.version}] ${data.toString()}`);
555
- return;
556
- }
557
- console.error(
558
- `[${this.metadata.version}][${this._currentExecution.run.id}.${this._currentExecution.attempt.number}] ${data.toString()}`
559
- );
560
- }
561
- #kill() {
562
- if (this._child && !this._child.killed) {
563
- this._child?.kill();
564
- }
565
- }
566
- };
567
-
568
- // src/workers/prod/entry-point.ts
569
- import { setTimeout as setTimeout2 } from "node:timers/promises";
570
- var HTTP_SERVER_PORT2 = Number(process.env.HTTP_SERVER_PORT || getRandomPortNumber());
571
- var COORDINATOR_HOST = process.env.COORDINATOR_HOST || "127.0.0.1";
572
- var COORDINATOR_PORT = Number(process.env.COORDINATOR_PORT || 50080);
573
- var MACHINE_NAME2 = process.env.MACHINE_NAME || "local";
574
- var POD_NAME = process.env.POD_NAME || "some-pod";
575
- var SHORT_HASH = process.env.TRIGGER_CONTENT_HASH.slice(0, 9);
576
- var logger2 = new SimpleLogger(`[${MACHINE_NAME2}][${SHORT_HASH}]`);
577
- var ProdWorker = class {
578
- constructor(port, host = "0.0.0.0") {
579
- this.host = host;
580
- this.#coordinatorSocket = this.#createCoordinatorSocket(COORDINATOR_HOST);
581
- this.#backgroundWorker = new ProdBackgroundWorker("worker.js", {
582
- projectConfig: __PROJECT_CONFIG__,
583
- env: {
584
- ...gatherProcessEnv(),
585
- TRIGGER_API_URL: this.apiUrl,
586
- TRIGGER_SECRET_KEY: this.apiKey,
587
- OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318"
588
- },
589
- contentHash: this.contentHash
590
- });
591
- this.#backgroundWorker.onTaskHeartbeat.attach((attemptFriendlyId) => {
592
- this.#coordinatorSocket.socket.emit("TASK_HEARTBEAT", { version: "v1", attemptFriendlyId });
593
- });
594
- this.#backgroundWorker.onReadyForCheckpoint.attach(async (message) => {
595
- this.#coordinatorSocket.socket.emit("READY_FOR_CHECKPOINT", { version: "v1" });
596
- });
597
- this.#backgroundWorker.onCancelCheckpoint.attach(async (message) => {
598
- logger2.log("onCancelCheckpoint() clearing paused state, don't wait for post start hook", {
599
- paused: this.paused,
600
- nextResumeAfter: this.nextResumeAfter,
601
- waitForPostStart: this.waitForPostStart
602
- });
603
- this.paused = false;
604
- this.nextResumeAfter = void 0;
605
- this.waitForPostStart = false;
606
- this.#coordinatorSocket.socket.emit("CANCEL_CHECKPOINT", { version: "v1" });
607
- });
608
- this.#backgroundWorker.onWaitForDuration.attach(async (message) => {
609
- if (!this.attemptFriendlyId) {
610
- logger2.error("Failed to send wait message, attempt friendly ID not set", { message });
611
- return;
612
- }
613
- const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
614
- "WAIT_FOR_DURATION",
615
- {
616
- ...message,
617
- attemptFriendlyId: this.attemptFriendlyId
618
- }
619
- );
620
- this.#prepareForWait("WAIT_FOR_DURATION", willCheckpointAndRestore);
621
- });
622
- this.#backgroundWorker.onWaitForTask.attach(async (message) => {
623
- if (!this.attemptFriendlyId) {
624
- logger2.error("Failed to send wait message, attempt friendly ID not set", { message });
625
- return;
626
- }
627
- const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
628
- "WAIT_FOR_TASK",
629
- {
630
- ...message,
631
- attemptFriendlyId: this.attemptFriendlyId
632
- }
633
- );
634
- this.#prepareForWait("WAIT_FOR_TASK", willCheckpointAndRestore);
635
- });
636
- this.#backgroundWorker.onWaitForBatch.attach(async (message) => {
637
- if (!this.attemptFriendlyId) {
638
- logger2.error("Failed to send wait message, attempt friendly ID not set", { message });
639
- return;
640
- }
641
- const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
642
- "WAIT_FOR_BATCH",
643
- {
644
- ...message,
645
- attemptFriendlyId: this.attemptFriendlyId
646
- }
647
- );
648
- this.#prepareForWait("WAIT_FOR_BATCH", willCheckpointAndRestore);
649
- });
650
- this.#httpPort = port;
651
- this.#httpServer = this.#createHttpServer();
652
- }
653
- apiUrl = process.env.TRIGGER_API_URL;
654
- apiKey = process.env.TRIGGER_SECRET_KEY;
655
- contentHash = process.env.TRIGGER_CONTENT_HASH;
656
- projectRef = process.env.TRIGGER_PROJECT_REF;
657
- envId = process.env.TRIGGER_ENV_ID;
658
- runId = process.env.TRIGGER_RUN_ID || "index-only";
659
- deploymentId = process.env.TRIGGER_DEPLOYMENT_ID;
660
- deploymentVersion = process.env.TRIGGER_DEPLOYMENT_VERSION;
661
- runningInKubernetes = !!process.env.KUBERNETES_PORT;
662
- executing = false;
663
- completed = /* @__PURE__ */ new Set();
664
- paused = false;
665
- attemptFriendlyId;
666
- nextResumeAfter;
667
- waitForPostStart = false;
668
- #httpPort;
669
- #backgroundWorker;
670
- #httpServer;
671
- #coordinatorSocket;
672
- async #reconnect(isPostStart = false, reconnectImmediately = false) {
673
- if (isPostStart) {
674
- this.waitForPostStart = false;
675
- }
676
- this.#coordinatorSocket.close();
677
- if (!reconnectImmediately) {
678
- await setTimeout2(1e3);
679
- }
680
- let coordinatorHost = COORDINATOR_HOST;
681
- try {
682
- if (this.runningInKubernetes) {
683
- coordinatorHost = (await readFile("/etc/taskinfo/coordinator-host", "utf-8")).replace(
684
- "\n",
685
- ""
686
- );
687
- logger2.log("reconnecting", {
688
- coordinatorHost: {
689
- fromEnv: COORDINATOR_HOST,
690
- fromVolume: coordinatorHost,
691
- current: this.#coordinatorSocket.socket.io.opts.hostname
692
- }
693
- });
694
- }
695
- } catch (error) {
696
- logger2.error("taskinfo read error during reconnect", { error });
697
- } finally {
698
- this.#coordinatorSocket = this.#createCoordinatorSocket(coordinatorHost);
699
- }
700
- }
701
- #prepareForWait(reason, willCheckpointAndRestore) {
702
- logger2.log(`prepare for ${reason}`, { willCheckpointAndRestore });
703
- this.#backgroundWorker.preCheckpointNotification.post({ willCheckpointAndRestore });
704
- if (willCheckpointAndRestore) {
705
- this.paused = true;
706
- this.nextResumeAfter = reason;
707
- this.waitForPostStart = true;
708
- }
709
- }
710
- async #prepareForRetry(willCheckpointAndRestore, shouldExit) {
711
- logger2.log("prepare for retry", { willCheckpointAndRestore, shouldExit });
712
- if (shouldExit) {
713
- if (willCheckpointAndRestore) {
714
- logger2.log("WARNING: Will checkpoint but also requested exit. This won't end well.");
715
- }
716
- await this.#backgroundWorker.close();
717
- process.exit(0);
718
- }
719
- this.executing = false;
720
- this.attemptFriendlyId = void 0;
721
- if (willCheckpointAndRestore) {
722
- this.waitForPostStart = true;
723
- this.#coordinatorSocket.socket.emit("READY_FOR_CHECKPOINT", { version: "v1" });
724
- return;
725
- }
726
- }
727
- #resumeAfterDuration() {
728
- this.paused = false;
729
- this.nextResumeAfter = void 0;
730
- this.#backgroundWorker.waitCompletedNotification();
731
- }
732
- #returnValidatedExtraHeaders(headers) {
733
- for (const [key, value] of Object.entries(headers)) {
734
- if (value === void 0) {
735
- throw new Error(`Extra header is undefined: ${key}`);
736
- }
737
- }
738
- return headers;
739
- }
740
- #createCoordinatorSocket(host) {
741
- const extraHeaders = this.#returnValidatedExtraHeaders({
742
- "x-machine-name": MACHINE_NAME2,
743
- "x-pod-name": POD_NAME,
744
- "x-trigger-content-hash": this.contentHash,
745
- "x-trigger-project-ref": this.projectRef,
746
- "x-trigger-env-id": this.envId,
747
- "x-trigger-deployment-id": this.deploymentId,
748
- "x-trigger-run-id": this.runId,
749
- "x-trigger-deployment-version": this.deploymentVersion
750
- });
751
- if (this.attemptFriendlyId) {
752
- extraHeaders["x-trigger-attempt-friendly-id"] = this.attemptFriendlyId;
753
- }
754
- logger2.log("connecting to coordinator", {
755
- host,
756
- port: COORDINATOR_PORT,
757
- extraHeaders
758
- });
759
- const coordinatorConnection = new ZodSocketConnection2({
760
- namespace: "prod-worker",
761
- host,
762
- port: COORDINATOR_PORT,
763
- clientMessages: ProdWorkerToCoordinatorMessages,
764
- serverMessages: CoordinatorToProdWorkerMessages,
765
- extraHeaders,
766
- handlers: {
767
- RESUME_AFTER_DEPENDENCY: async (message) => {
768
- if (!this.paused) {
769
- logger2.error("worker not paused", {
770
- completions: message.completions,
771
- executions: message.executions
772
- });
773
- return;
774
- }
775
- if (message.completions.length !== message.executions.length) {
776
- logger2.error("did not receive the same number of completions and executions", {
777
- completions: message.completions,
778
- executions: message.executions
779
- });
780
- return;
781
- }
782
- if (message.completions.length === 0 || message.executions.length === 0) {
783
- logger2.error("no completions or executions", {
784
- completions: message.completions,
785
- executions: message.executions
786
- });
787
- return;
788
- }
789
- if (this.nextResumeAfter !== "WAIT_FOR_TASK" && this.nextResumeAfter !== "WAIT_FOR_BATCH") {
790
- logger2.error("not waiting to resume after dependency", {
791
- nextResumeAfter: this.nextResumeAfter
792
- });
793
- return;
794
- }
795
- if (this.nextResumeAfter === "WAIT_FOR_TASK" && message.completions.length > 1) {
796
- logger2.error("waiting for single task but got multiple completions", {
797
- completions: message.completions,
798
- executions: message.executions
799
- });
800
- return;
801
- }
802
- this.paused = false;
803
- this.nextResumeAfter = void 0;
804
- for (let i = 0; i < message.completions.length; i++) {
805
- const completion = message.completions[i];
806
- const execution = message.executions[i];
807
- if (!completion || !execution)
808
- continue;
809
- this.#backgroundWorker.taskRunCompletedNotification(completion, execution);
810
- }
811
- },
812
- RESUME_AFTER_DURATION: async (message) => {
813
- if (!this.paused) {
814
- logger2.error("worker not paused", {
815
- attemptId: message.attemptId
816
- });
817
- return;
818
- }
819
- if (this.nextResumeAfter !== "WAIT_FOR_DURATION") {
820
- logger2.error("not waiting to resume after duration", {
821
- nextResumeAfter: this.nextResumeAfter
822
- });
823
- return;
824
- }
825
- this.#resumeAfterDuration();
826
- },
827
- EXECUTE_TASK_RUN: async ({ executionPayload }) => {
828
- if (this.executing) {
829
- logger2.error("dropping execute request, already executing");
830
- return;
831
- }
832
- if (this.completed.has(executionPayload.execution.attempt.id)) {
833
- logger2.error("dropping execute request, already completed");
834
- return;
835
- }
836
- this.executing = true;
837
- this.attemptFriendlyId = executionPayload.execution.attempt.id;
838
- const completion = await this.#backgroundWorker.executeTaskRun(executionPayload);
839
- logger2.log("completed", completion);
840
- this.completed.add(executionPayload.execution.attempt.id);
841
- const { willCheckpointAndRestore, shouldExit } = await this.#coordinatorSocket.socket.emitWithAck("TASK_RUN_COMPLETED", {
842
- version: "v1",
843
- execution: executionPayload.execution,
844
- completion
845
- });
846
- logger2.log("completion acknowledged", { willCheckpointAndRestore, shouldExit });
847
- this.#prepareForRetry(willCheckpointAndRestore, shouldExit);
848
- },
849
- REQUEST_ATTEMPT_CANCELLATION: async (message) => {
850
- if (!this.executing) {
851
- return;
852
- }
853
- await this.#backgroundWorker.cancelAttempt(message.attemptId);
854
- },
855
- REQUEST_EXIT: async () => {
856
- this.#coordinatorSocket.close();
857
- process.exit(0);
858
- },
859
- READY_FOR_RETRY: async (message) => {
860
- if (this.completed.size < 1) {
861
- return;
862
- }
863
- this.#coordinatorSocket.socket.emit("READY_FOR_EXECUTION", {
864
- version: "v1",
865
- runId: this.runId,
866
- totalCompletions: this.completed.size
867
- });
868
- }
869
- },
870
- onConnection: async (socket, handler, sender, logger3) => {
871
- if (this.waitForPostStart) {
872
- logger3.log("skip connection handler, waiting for post start hook");
873
- return;
874
- }
875
- if (process.env.INDEX_TASKS === "true") {
876
- try {
877
- const taskResources = await this.#initializeWorker();
878
- const { success } = await socket.emitWithAck("INDEX_TASKS", {
879
- version: "v1",
880
- deploymentId: this.deploymentId,
881
- ...taskResources
882
- });
883
- if (success) {
884
- logger3.info("indexing done, shutting down..");
885
- process.exit(0);
886
- } else {
887
- logger3.info("indexing failure, shutting down..");
888
- process.exit(1);
889
- }
890
- } catch (e) {
891
- if (e instanceof TaskMetadataParseError) {
892
- logger3.error("tasks metadata parse error", { message: e.zodIssues, tasks: e.tasks });
893
- socket.emit("INDEXING_FAILED", {
894
- version: "v1",
895
- deploymentId: this.deploymentId,
896
- error: {
897
- name: "TaskMetadataParseError",
898
- message: "There was an error parsing the task metadata",
899
- stack: JSON.stringify({ zodIssues: e.zodIssues, tasks: e.tasks })
900
- }
901
- });
902
- } else if (e instanceof UncaughtExceptionError) {
903
- logger3.error("uncaught exception", { message: e.originalError.message });
904
- socket.emit("INDEXING_FAILED", {
905
- version: "v1",
906
- deploymentId: this.deploymentId,
907
- error: {
908
- name: e.originalError.name,
909
- message: e.originalError.message,
910
- stack: e.originalError.stack
911
- }
912
- });
913
- } else if (e instanceof Error) {
914
- logger3.error("error", { message: e.message });
915
- socket.emit("INDEXING_FAILED", {
916
- version: "v1",
917
- deploymentId: this.deploymentId,
918
- error: {
919
- name: e.name,
920
- message: e.message,
921
- stack: e.stack
922
- }
923
- });
924
- } else if (typeof e === "string") {
925
- logger3.error("string error", { message: e });
926
- socket.emit("INDEXING_FAILED", {
927
- version: "v1",
928
- deploymentId: this.deploymentId,
929
- error: {
930
- name: "Error",
931
- message: e
932
- }
933
- });
934
- } else {
935
- logger3.error("unknown error", { error: e });
936
- socket.emit("INDEXING_FAILED", {
937
- version: "v1",
938
- deploymentId: this.deploymentId,
939
- error: {
940
- name: "Error",
941
- message: "Unknown error"
942
- }
943
- });
944
- }
945
- await setTimeout2(200);
946
- process.exit(1);
947
- }
948
- }
949
- if (this.paused) {
950
- if (!this.nextResumeAfter) {
951
- return;
952
- }
953
- if (!this.attemptFriendlyId) {
954
- logger3.error("Missing friendly ID");
955
- return;
956
- }
957
- if (this.nextResumeAfter === "WAIT_FOR_DURATION") {
958
- this.#resumeAfterDuration();
959
- return;
960
- }
961
- socket.emit("READY_FOR_RESUME", {
962
- version: "v1",
963
- attemptFriendlyId: this.attemptFriendlyId,
964
- type: this.nextResumeAfter
965
- });
966
- return;
967
- }
968
- if (this.executing) {
969
- return;
970
- }
971
- socket.emit("READY_FOR_EXECUTION", {
972
- version: "v1",
973
- runId: this.runId,
974
- totalCompletions: this.completed.size
975
- });
976
- },
977
- onError: async (socket, err, logger3) => {
978
- logger3.error("onError", {
979
- error: {
980
- name: err.name,
981
- message: err.message
982
- }
983
- });
984
- await this.#reconnect();
985
- },
986
- onDisconnect: async (socket, reason, description, logger3) => {
987
- }
988
- });
989
- return coordinatorConnection;
990
- }
991
- #createHttpServer() {
992
- const httpServer = createServer(async (req, res) => {
993
- logger2.log(`[${req.method}]`, req.url);
994
- const reply = new HttpReply(res);
995
- try {
996
- const url = new URL(req.url ?? "", `http://${req.headers.host}`);
997
- switch (url.pathname) {
998
- case "/health": {
999
- return reply.text("ok");
1000
- }
1001
- case "/status": {
1002
- return reply.json({
1003
- executing: this.executing,
1004
- pause: this.paused,
1005
- nextResumeAfter: this.nextResumeAfter
1006
- });
1007
- }
1008
- case "/connect": {
1009
- this.#coordinatorSocket.connect();
1010
- return reply.text("Connected to coordinator");
1011
- }
1012
- case "/close": {
1013
- await this.#coordinatorSocket.sendWithAck("LOG", {
1014
- version: "v1",
1015
- text: `[${req.method}] ${req.url}`
1016
- });
1017
- this.#coordinatorSocket.close();
1018
- return reply.text("Disconnected from coordinator");
1019
- }
1020
- case "/test": {
1021
- await this.#coordinatorSocket.sendWithAck("LOG", {
1022
- version: "v1",
1023
- text: `[${req.method}] ${req.url}`
1024
- });
1025
- return reply.text("Received ACK from coordinator");
1026
- }
1027
- case "/preStop": {
1028
- const cause = PreStopCauses.safeParse(url.searchParams.get("cause"));
1029
- if (!cause.success) {
1030
- logger2.error("Failed to parse cause", { cause });
1031
- return reply.text("Failed to parse cause", 400);
1032
- }
1033
- switch (cause.data) {
1034
- case "terminate": {
1035
- break;
1036
- }
1037
- default: {
1038
- logger2.error("Unhandled cause", { cause: cause.data });
1039
- break;
1040
- }
1041
- }
1042
- logger2.log("preStop", { url: req.url });
1043
- return reply.text("preStop ok");
1044
- }
1045
- case "/postStart": {
1046
- const cause = PostStartCauses.safeParse(url.searchParams.get("cause"));
1047
- if (!cause.success) {
1048
- logger2.error("Failed to parse cause", { cause });
1049
- return reply.text("Failed to parse cause", 400);
1050
- }
1051
- switch (cause.data) {
1052
- case "index": {
1053
- break;
1054
- }
1055
- case "create": {
1056
- break;
1057
- }
1058
- case "restore": {
1059
- await this.#reconnect(true, true);
1060
- break;
1061
- }
1062
- default: {
1063
- logger2.error("Unhandled cause", { cause: cause.data });
1064
- break;
1065
- }
1066
- }
1067
- return reply.text("postStart ok");
1068
- }
1069
- default: {
1070
- return reply.empty(404);
1071
- }
1072
- }
1073
- } catch (error) {
1074
- logger2.error("HTTP server error", { error });
1075
- reply.empty(500);
1076
- }
1077
- });
1078
- httpServer.on("clientError", (err, socket) => {
1079
- socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
1080
- });
1081
- httpServer.on("listening", () => {
1082
- logger2.log("http server listening on port", this.#httpPort);
1083
- });
1084
- httpServer.on("error", async (error) => {
1085
- if (error.code != "EADDRINUSE") {
1086
- return;
1087
- }
1088
- logger2.error(`port ${this.#httpPort} already in use, retrying with random port..`);
1089
- this.#httpPort = getRandomPortNumber();
1090
- await setTimeout2(100);
1091
- this.start();
1092
- });
1093
- return httpServer;
1094
- }
1095
- async #initializeWorker() {
1096
- const envVars = await this.#fetchEnvironmentVariables();
1097
- await this.#backgroundWorker.initialize({ env: envVars });
1098
- let packageVersion;
1099
- const taskResources = [];
1100
- if (!this.#backgroundWorker.tasks) {
1101
- throw new Error(`Background Worker started without tasks`);
1102
- }
1103
- for (const task of this.#backgroundWorker.tasks) {
1104
- taskResources.push(task);
1105
- packageVersion = task.packageVersion;
1106
- }
1107
- if (!packageVersion) {
1108
- throw new Error(`Background Worker started without package version`);
1109
- }
1110
- return {
1111
- packageVersion,
1112
- tasks: taskResources
1113
- };
1114
- }
1115
- async #fetchEnvironmentVariables() {
1116
- const response = await fetch(`${this.apiUrl}/api/v1/projects/${this.projectRef}/envvars`, {
1117
- method: "GET",
1118
- headers: {
1119
- Authorization: `Bearer ${this.apiKey}`
1120
- }
1121
- });
1122
- if (!response.ok) {
1123
- return {};
1124
- }
1125
- const data = await response.json();
1126
- return data?.variables ?? {};
1127
- }
1128
- start() {
1129
- this.#httpServer.listen(this.#httpPort, this.host);
1130
- }
1131
- };
1132
- var prodWorker = new ProdWorker(HTTP_SERVER_PORT2);
1133
- prodWorker.start();
1134
- function gatherProcessEnv() {
1135
- const env = {
1136
- NODE_ENV: process.env.NODE_ENV ?? "production",
1137
- PATH: process.env.PATH,
1138
- USER: process.env.USER,
1139
- SHELL: process.env.SHELL,
1140
- LANG: process.env.LANG,
1141
- TERM: process.env.TERM,
1142
- NODE_PATH: process.env.NODE_PATH,
1143
- HOME: process.env.HOME
1144
- };
1145
- return Object.fromEntries(Object.entries(env).filter(([key, value]) => value !== void 0));
1146
- }