trigger.dev 0.0.0-prerelease-20240219135254 → 0.0.0-v3-canary-20240321115026

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.
@@ -0,0 +1,942 @@
1
+ // src/workers/prod/entry-point.ts
2
+ import {
3
+ CoordinatorToProdWorkerMessages,
4
+ ProdWorkerToCoordinatorMessages,
5
+ ZodSocketConnection as ZodSocketConnection2
6
+ } from "@trigger.dev/core/v3";
7
+
8
+ // ../core-apps/src/http.ts
9
+ var getTextBody = (req) => new Promise((resolve) => {
10
+ let body = "";
11
+ req.on("readable", () => {
12
+ const chunk = req.read();
13
+ if (chunk) {
14
+ body += chunk;
15
+ }
16
+ });
17
+ req.on("end", () => {
18
+ resolve(body);
19
+ });
20
+ });
21
+ var HttpReply = class {
22
+ constructor(response) {
23
+ this.response = response;
24
+ }
25
+ empty(status) {
26
+ return this.response.writeHead(status ?? 200).end();
27
+ }
28
+ text(text, status, contentType) {
29
+ return this.response.writeHead(status ?? 200, { "Content-Type": contentType || "text/plain" }).end(text.endsWith("\n") ? text : `${text}
30
+ `);
31
+ }
32
+ };
33
+ function getRandomInteger(min, max) {
34
+ const intMin = Math.ceil(min);
35
+ const intMax = Math.floor(max);
36
+ return Math.floor(Math.random() * (intMax - intMin + 1)) + intMin;
37
+ }
38
+ function getRandomPortNumber() {
39
+ return getRandomInteger(8e3, 9999);
40
+ }
41
+
42
+ // ../core-apps/src/logger.ts
43
+ var SimpleLogger = class {
44
+ constructor(prefix) {
45
+ this.prefix = prefix;
46
+ }
47
+ #debugEnabled = ["1", "true"].includes(process.env.DEBUG ?? "");
48
+ log(arg0, ...argN) {
49
+ console.log(...this.#getPrefixedArgs(arg0, ...argN));
50
+ return arg0;
51
+ }
52
+ debug(arg0, ...argN) {
53
+ if (!this.#debugEnabled) {
54
+ return arg0;
55
+ }
56
+ console.debug(...this.#getPrefixedArgs("DEBUG", arg0, ...argN));
57
+ return arg0;
58
+ }
59
+ error(arg0, ...argN) {
60
+ console.error(...this.#getPrefixedArgs(arg0, ...argN));
61
+ return arg0;
62
+ }
63
+ #getPrefixedArgs(...args) {
64
+ if (!this.prefix) {
65
+ return args;
66
+ }
67
+ return [this.prefix, ...args];
68
+ }
69
+ };
70
+
71
+ // ../core-apps/src/provider.ts
72
+ import {
73
+ ClientToSharedQueueMessages,
74
+ clientWebsocketMessages,
75
+ PlatformToProviderMessages,
76
+ ProviderToPlatformMessages,
77
+ SharedQueueToClientMessages,
78
+ ZodMessageSender,
79
+ ZodSocketConnection
80
+ } from "@trigger.dev/core/v3";
81
+ var HTTP_SERVER_PORT = Number(process.env.HTTP_SERVER_PORT || getRandomPortNumber());
82
+ var MACHINE_NAME = process.env.MACHINE_NAME || "local";
83
+ var PLATFORM_HOST = process.env.PLATFORM_HOST || "127.0.0.1";
84
+ var PLATFORM_WS_PORT = process.env.PLATFORM_WS_PORT || 3030;
85
+ var PLATFORM_SECRET = process.env.PLATFORM_SECRET || "provider-secret";
86
+ var logger = new SimpleLogger(`[${MACHINE_NAME}]`);
87
+
88
+ // src/workers/prod/entry-point.ts
89
+ import { createServer } from "node:http";
90
+
91
+ // src/workers/prod/backgroundWorker.ts
92
+ import {
93
+ ProdChildToWorkerMessages,
94
+ ProdWorkerToChildMessages,
95
+ SemanticInternalAttributes,
96
+ TaskRunErrorCodes,
97
+ ZodIpcConnection,
98
+ correctErrorStackTrace
99
+ } from "@trigger.dev/core/v3";
100
+ import { Evt } from "evt";
101
+ import { fork } from "node:child_process";
102
+
103
+ // src/workers/common/errors.ts
104
+ var UncaughtExceptionError = class extends Error {
105
+ constructor(originalError, origin) {
106
+ super(`Uncaught exception: ${originalError.message}`);
107
+ this.originalError = originalError;
108
+ this.origin = origin;
109
+ this.name = "UncaughtExceptionError";
110
+ }
111
+ };
112
+
113
+ // src/workers/prod/backgroundWorker.ts
114
+ var UnexpectedExitError = class extends Error {
115
+ constructor(code) {
116
+ super(`Unexpected exit with code ${code}`);
117
+ this.code = code;
118
+ this.name = "UnexpectedExitError";
119
+ }
120
+ };
121
+ var CleanupProcessError = class extends Error {
122
+ constructor() {
123
+ super("Cancelled");
124
+ this.name = "CleanupProcessError";
125
+ }
126
+ };
127
+ var CancelledProcessError = class extends Error {
128
+ constructor() {
129
+ super("Cancelled");
130
+ this.name = "CancelledProcessError";
131
+ }
132
+ };
133
+ var ProdBackgroundWorker = class {
134
+ constructor(path, params) {
135
+ this.path = path;
136
+ this.params = params;
137
+ }
138
+ _initialized = false;
139
+ onTaskHeartbeat = new Evt();
140
+ onWaitForDuration = new Evt();
141
+ onWaitForTask = new Evt();
142
+ onWaitForBatch = new Evt();
143
+ preCheckpointNotification = Evt.create();
144
+ _onClose = new Evt();
145
+ tasks = [];
146
+ _taskRunProcess;
147
+ _closed = false;
148
+ async close() {
149
+ if (this._closed) {
150
+ return;
151
+ }
152
+ this._closed = true;
153
+ this.onTaskHeartbeat.detach();
154
+ await this._taskRunProcess?.cleanup(true);
155
+ }
156
+ async flushTelemetry() {
157
+ await this._taskRunProcess?.cleanup(false);
158
+ }
159
+ async initialize(options) {
160
+ if (this._initialized) {
161
+ throw new Error("Worker already initialized");
162
+ }
163
+ let resolved = false;
164
+ this.tasks = await new Promise((resolve, reject) => {
165
+ const child = fork(this.path, {
166
+ stdio: [
167
+ /*stdin*/
168
+ "ignore",
169
+ /*stdout*/
170
+ "pipe",
171
+ /*stderr*/
172
+ "pipe",
173
+ "ipc"
174
+ ],
175
+ env: {
176
+ ...this.params.env,
177
+ ...options?.env
178
+ }
179
+ });
180
+ const timeout = setTimeout(() => {
181
+ if (resolved) {
182
+ return;
183
+ }
184
+ resolved = true;
185
+ child.kill();
186
+ reject(new Error("Worker timed out"));
187
+ }, 1e4);
188
+ new ZodIpcConnection({
189
+ listenSchema: ProdChildToWorkerMessages,
190
+ emitSchema: ProdWorkerToChildMessages,
191
+ process: child,
192
+ handlers: {
193
+ TASKS_READY: async (message) => {
194
+ if (!resolved) {
195
+ clearTimeout(timeout);
196
+ resolved = true;
197
+ resolve(message.tasks);
198
+ child.kill();
199
+ }
200
+ },
201
+ UNCAUGHT_EXCEPTION: async (message) => {
202
+ if (!resolved) {
203
+ clearTimeout(timeout);
204
+ resolved = true;
205
+ reject(new UncaughtExceptionError(message.error, message.origin));
206
+ child.kill();
207
+ }
208
+ }
209
+ }
210
+ });
211
+ child.stdout?.on("data", (data) => {
212
+ console.log(data.toString());
213
+ });
214
+ child.stderr?.on("data", (data) => {
215
+ console.error(data.toString());
216
+ });
217
+ child.on("exit", (code) => {
218
+ if (!resolved) {
219
+ clearTimeout(timeout);
220
+ resolved = true;
221
+ reject(new Error(`Worker exited with code ${code}`));
222
+ }
223
+ });
224
+ });
225
+ this._initialized = true;
226
+ }
227
+ getMetadata(workerId, version) {
228
+ return {
229
+ contentHash: this.params.contentHash,
230
+ id: workerId,
231
+ version
232
+ };
233
+ }
234
+ // We need to notify all the task run processes that a task run has completed,
235
+ // in case they are waiting for it through triggerAndWait
236
+ async taskRunCompletedNotification(completion, execution) {
237
+ this._taskRunProcess?.taskRunCompletedNotification(completion, execution);
238
+ }
239
+ async waitCompletedNotification() {
240
+ this._taskRunProcess?.waitCompletedNotification();
241
+ }
242
+ async #initializeTaskRunProcess(payload) {
243
+ const metadata = this.getMetadata(
244
+ payload.execution.worker.id,
245
+ payload.execution.worker.version
246
+ );
247
+ if (!this._taskRunProcess) {
248
+ const taskRunProcess = new TaskRunProcess(
249
+ this.path,
250
+ {
251
+ ...this.params.env,
252
+ ...payload.environment ?? {}
253
+ },
254
+ metadata,
255
+ this.params
256
+ );
257
+ taskRunProcess.onExit.attach(() => {
258
+ this._taskRunProcess = void 0;
259
+ });
260
+ taskRunProcess.onTaskHeartbeat.attach((id) => {
261
+ this.onTaskHeartbeat.post(id);
262
+ });
263
+ taskRunProcess.onWaitForBatch.attach((message) => {
264
+ this.onWaitForBatch.post(message);
265
+ });
266
+ taskRunProcess.onWaitForDuration.attach((message) => {
267
+ this.onWaitForDuration.post(message);
268
+ });
269
+ taskRunProcess.onWaitForTask.attach((message) => {
270
+ this.onWaitForTask.post(message);
271
+ });
272
+ this.preCheckpointNotification.attach((message) => {
273
+ taskRunProcess.preCheckpointNotification.post(message);
274
+ });
275
+ await taskRunProcess.initialize();
276
+ this._taskRunProcess = taskRunProcess;
277
+ }
278
+ return this._taskRunProcess;
279
+ }
280
+ // We need to fork the process before we can execute any tasks
281
+ async executeTaskRun(payload) {
282
+ try {
283
+ const taskRunProcess = await this.#initializeTaskRunProcess(payload);
284
+ const result = await taskRunProcess.executeTaskRun(payload);
285
+ await taskRunProcess.cleanup(result.ok || result.retry === void 0);
286
+ if (result.ok) {
287
+ return result;
288
+ }
289
+ const error = result.error;
290
+ if (error.type === "BUILT_IN_ERROR") {
291
+ const mappedError = await this.#correctError(error, payload.execution);
292
+ return {
293
+ ...result,
294
+ error: mappedError
295
+ };
296
+ }
297
+ return result;
298
+ } catch (e) {
299
+ if (e instanceof CancelledProcessError) {
300
+ return {
301
+ id: payload.execution.attempt.id,
302
+ ok: false,
303
+ retry: void 0,
304
+ error: {
305
+ type: "INTERNAL_ERROR",
306
+ code: TaskRunErrorCodes.TASK_RUN_CANCELLED
307
+ }
308
+ };
309
+ }
310
+ if (e instanceof CleanupProcessError) {
311
+ return {
312
+ id: payload.execution.attempt.id,
313
+ ok: false,
314
+ retry: void 0,
315
+ error: {
316
+ type: "INTERNAL_ERROR",
317
+ code: TaskRunErrorCodes.TASK_EXECUTION_ABORTED
318
+ }
319
+ };
320
+ }
321
+ if (e instanceof UnexpectedExitError) {
322
+ return {
323
+ id: payload.execution.attempt.id,
324
+ ok: false,
325
+ retry: void 0,
326
+ error: {
327
+ type: "INTERNAL_ERROR",
328
+ code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE
329
+ }
330
+ };
331
+ }
332
+ return {
333
+ id: payload.execution.attempt.id,
334
+ ok: false,
335
+ retry: void 0,
336
+ error: {
337
+ type: "INTERNAL_ERROR",
338
+ code: TaskRunErrorCodes.TASK_EXECUTION_FAILED
339
+ }
340
+ };
341
+ }
342
+ }
343
+ async cancelAttempt(attemptId) {
344
+ await this._taskRunProcess?.cancel();
345
+ }
346
+ async #correctError(error, execution) {
347
+ return {
348
+ ...error,
349
+ stackTrace: correctErrorStackTrace(error.stackTrace, this.params.projectConfig.projectDir)
350
+ };
351
+ }
352
+ };
353
+ var TaskRunProcess = class {
354
+ constructor(path, env, metadata, worker) {
355
+ this.path = path;
356
+ this.env = env;
357
+ this.metadata = metadata;
358
+ this.worker = worker;
359
+ }
360
+ _ipc;
361
+ _child;
362
+ _attemptPromises = /* @__PURE__ */ new Map();
363
+ _attemptStatuses = /* @__PURE__ */ new Map();
364
+ _currentExecution;
365
+ _isBeingKilled = false;
366
+ _isBeingCancelled = false;
367
+ onTaskHeartbeat = new Evt();
368
+ onExit = new Evt();
369
+ onWaitForBatch = new Evt();
370
+ onWaitForDuration = new Evt();
371
+ onWaitForTask = new Evt();
372
+ preCheckpointNotification = Evt.create();
373
+ async initialize() {
374
+ this._child = fork(this.path, {
375
+ stdio: [
376
+ /*stdin*/
377
+ "ignore",
378
+ /*stdout*/
379
+ "pipe",
380
+ /*stderr*/
381
+ "pipe",
382
+ "ipc"
383
+ ],
384
+ env: {
385
+ ...this.env,
386
+ OTEL_RESOURCE_ATTRIBUTES: JSON.stringify({
387
+ [SemanticInternalAttributes.PROJECT_DIR]: this.worker.projectConfig.projectDir
388
+ }),
389
+ ...this.worker.debugOtel ? { OTEL_LOG_LEVEL: "debug" } : {}
390
+ }
391
+ });
392
+ this._ipc = new ZodIpcConnection({
393
+ listenSchema: ProdChildToWorkerMessages,
394
+ emitSchema: ProdWorkerToChildMessages,
395
+ process: this._child,
396
+ handlers: {
397
+ TASK_RUN_COMPLETED: async (message) => {
398
+ const { result, execution } = message;
399
+ const promiseStatus = this._attemptStatuses.get(execution.attempt.id);
400
+ if (promiseStatus !== "PENDING") {
401
+ return;
402
+ }
403
+ this._attemptStatuses.set(execution.attempt.id, "RESOLVED");
404
+ const attemptPromise = this._attemptPromises.get(execution.attempt.id);
405
+ if (!attemptPromise) {
406
+ return;
407
+ }
408
+ const { resolver } = attemptPromise;
409
+ resolver(result);
410
+ },
411
+ READY_TO_DISPOSE: async (message) => {
412
+ },
413
+ TASK_HEARTBEAT: async (message) => {
414
+ this.onTaskHeartbeat.post(message.id);
415
+ },
416
+ TASKS_READY: async (message) => {
417
+ },
418
+ WAIT_FOR_BATCH: async (message) => {
419
+ this.onWaitForBatch.post(message);
420
+ },
421
+ WAIT_FOR_DURATION: async (message) => {
422
+ this.onWaitForDuration.post(message);
423
+ const { willCheckpointAndRestore } = await this.preCheckpointNotification.waitFor();
424
+ return { willCheckpointAndRestore };
425
+ },
426
+ WAIT_FOR_TASK: async (message) => {
427
+ this.onWaitForTask.post(message);
428
+ }
429
+ }
430
+ });
431
+ this._child.on("exit", this.#handleExit.bind(this));
432
+ this._child.stdout?.on("data", this.#handleLog.bind(this));
433
+ this._child.stderr?.on("data", this.#handleStdErr.bind(this));
434
+ }
435
+ async cancel() {
436
+ this._isBeingCancelled = true;
437
+ await this.cleanup(true);
438
+ }
439
+ async cleanup(kill = false) {
440
+ if (kill && this._isBeingKilled) {
441
+ return;
442
+ }
443
+ this._isBeingKilled = kill;
444
+ await this._ipc?.sendWithAck("CLEANUP", {
445
+ flush: true,
446
+ kill
447
+ });
448
+ }
449
+ async executeTaskRun(payload) {
450
+ let resolver;
451
+ let rejecter;
452
+ const promise = new Promise((resolve, reject) => {
453
+ resolver = resolve;
454
+ rejecter = reject;
455
+ });
456
+ this._attemptStatuses.set(payload.execution.attempt.id, "PENDING");
457
+ this._attemptPromises.set(payload.execution.attempt.id, { resolver, rejecter });
458
+ const { execution, traceContext } = payload;
459
+ this._currentExecution = execution;
460
+ if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
461
+ await this._ipc?.send("EXECUTE_TASK_RUN", {
462
+ execution,
463
+ traceContext,
464
+ metadata: this.metadata
465
+ });
466
+ }
467
+ const result = await promise;
468
+ this._currentExecution = void 0;
469
+ return result;
470
+ }
471
+ taskRunCompletedNotification(completion, execution) {
472
+ if (!completion.ok && typeof completion.retry !== "undefined") {
473
+ return;
474
+ }
475
+ if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
476
+ this._ipc?.send("TASK_RUN_COMPLETED_NOTIFICATION", {
477
+ completion,
478
+ execution
479
+ });
480
+ }
481
+ }
482
+ waitCompletedNotification() {
483
+ if (this._child?.connected && !this._isBeingKilled && !this._child.killed) {
484
+ this._ipc?.send("WAIT_COMPLETED_NOTIFICATION", {});
485
+ }
486
+ }
487
+ async #handleExit(code) {
488
+ for (const [id, status] of this._attemptStatuses.entries()) {
489
+ if (status === "PENDING") {
490
+ this._attemptStatuses.set(id, "REJECTED");
491
+ const attemptPromise = this._attemptPromises.get(id);
492
+ if (!attemptPromise) {
493
+ continue;
494
+ }
495
+ const { rejecter } = attemptPromise;
496
+ if (this._isBeingCancelled) {
497
+ rejecter(new CancelledProcessError());
498
+ } else if (this._isBeingKilled) {
499
+ rejecter(new CleanupProcessError());
500
+ } else {
501
+ rejecter(new UnexpectedExitError(code));
502
+ }
503
+ }
504
+ }
505
+ this.onExit.post(code);
506
+ }
507
+ #handleLog(data) {
508
+ if (!this._currentExecution) {
509
+ return;
510
+ }
511
+ console.log(
512
+ `[${this.metadata.version}][${this._currentExecution.run.id}.${this._currentExecution.attempt.number}] ${data.toString()}`
513
+ );
514
+ }
515
+ #handleStdErr(data) {
516
+ if (this._isBeingKilled) {
517
+ return;
518
+ }
519
+ if (!this._currentExecution) {
520
+ console.error(`[${this.metadata.version}] ${data.toString()}`);
521
+ return;
522
+ }
523
+ console.error(
524
+ `[${this.metadata.version}][${this._currentExecution.run.id}.${this._currentExecution.attempt.number}] ${data.toString()}`
525
+ );
526
+ }
527
+ #kill() {
528
+ if (this._child && !this._child.killed) {
529
+ this._child?.kill();
530
+ }
531
+ }
532
+ };
533
+
534
+ // src/workers/prod/entry-point.ts
535
+ var HTTP_SERVER_PORT2 = Number(process.env.HTTP_SERVER_PORT || getRandomPortNumber());
536
+ var COORDINATOR_HOST = process.env.COORDINATOR_HOST || "127.0.0.1";
537
+ var COORDINATOR_PORT = Number(process.env.COORDINATOR_PORT || 50080);
538
+ var MACHINE_NAME2 = process.env.MACHINE_NAME || "local";
539
+ var POD_NAME = process.env.POD_NAME || "some-pod";
540
+ var SHORT_HASH = process.env.TRIGGER_CONTENT_HASH.slice(0, 9);
541
+ var logger2 = new SimpleLogger(`[${MACHINE_NAME2}][${SHORT_HASH}]`);
542
+ var ProdWorker = class {
543
+ constructor(port, host = "0.0.0.0") {
544
+ this.host = host;
545
+ this.#coordinatorSocket = this.#createCoordinatorSocket();
546
+ this.#backgroundWorker = new ProdBackgroundWorker("worker.js", {
547
+ projectConfig: __PROJECT_CONFIG__,
548
+ env: {
549
+ ...gatherProcessEnv(),
550
+ TRIGGER_API_URL: this.apiUrl,
551
+ TRIGGER_SECRET_KEY: this.apiKey,
552
+ OTEL_EXPORTER_OTLP_ENDPOINT: process.env.OTEL_EXPORTER_OTLP_ENDPOINT ?? "http://0.0.0.0:4318"
553
+ },
554
+ contentHash: this.contentHash
555
+ });
556
+ this.#backgroundWorker.onTaskHeartbeat.attach((attemptFriendlyId) => {
557
+ this.#coordinatorSocket.socket.emit("TASK_HEARTBEAT", { version: "v1", attemptFriendlyId });
558
+ });
559
+ this.#backgroundWorker.onWaitForDuration.attach(async (message) => {
560
+ const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
561
+ "WAIT_FOR_DURATION",
562
+ { version: "v1", ...message }
563
+ );
564
+ logger2.log("WAIT_FOR_DURATION", { willCheckpointAndRestore });
565
+ this.#backgroundWorker.preCheckpointNotification.post({ willCheckpointAndRestore });
566
+ setTimeout(() => {
567
+ if (willCheckpointAndRestore) {
568
+ this.paused = true;
569
+ this.nextResumeAfter = "WAIT_FOR_DURATION";
570
+ }
571
+ this.#coordinatorSocket.close();
572
+ this.#coordinatorSocket.connect();
573
+ }, 3e3);
574
+ });
575
+ this.#backgroundWorker.onWaitForTask.attach(async (message) => {
576
+ const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
577
+ "WAIT_FOR_TASK",
578
+ { version: "v1", ...message }
579
+ );
580
+ logger2.log("WAIT_FOR_TASK", { willCheckpointAndRestore });
581
+ this.#backgroundWorker.preCheckpointNotification.post({ willCheckpointAndRestore });
582
+ setTimeout(() => {
583
+ if (willCheckpointAndRestore) {
584
+ this.paused = true;
585
+ this.nextResumeAfter = "WAIT_FOR_TASK";
586
+ }
587
+ this.#coordinatorSocket.close();
588
+ this.#coordinatorSocket.connect();
589
+ }, 3e3);
590
+ });
591
+ this.#backgroundWorker.onWaitForBatch.attach(async (message) => {
592
+ const { willCheckpointAndRestore } = await this.#coordinatorSocket.socket.emitWithAck(
593
+ "WAIT_FOR_BATCH",
594
+ { version: "v1", ...message }
595
+ );
596
+ logger2.log("WAIT_FOR_BATCH", { willCheckpointAndRestore });
597
+ this.#backgroundWorker.preCheckpointNotification.post({ willCheckpointAndRestore });
598
+ setTimeout(() => {
599
+ if (willCheckpointAndRestore) {
600
+ this.paused = true;
601
+ this.nextResumeAfter = "WAIT_FOR_BATCH";
602
+ }
603
+ this.#coordinatorSocket.close();
604
+ this.#coordinatorSocket.connect();
605
+ }, 3e3);
606
+ });
607
+ this.#httpPort = port;
608
+ this.#httpServer = this.#createHttpServer();
609
+ }
610
+ apiUrl = process.env.TRIGGER_API_URL;
611
+ apiKey = process.env.TRIGGER_SECRET_KEY;
612
+ contentHash = process.env.TRIGGER_CONTENT_HASH;
613
+ projectRef = process.env.TRIGGER_PROJECT_REF;
614
+ envId = process.env.TRIGGER_ENV_ID;
615
+ runId = process.env.TRIGGER_RUN_ID || "index-only";
616
+ attemptId = process.env.TRIGGER_ATTEMPT_ID || "index-only";
617
+ deploymentId = process.env.TRIGGER_DEPLOYMENT_ID;
618
+ executing = false;
619
+ completed = /* @__PURE__ */ new Set();
620
+ paused = false;
621
+ nextResumeAfter;
622
+ #httpPort;
623
+ #backgroundWorker;
624
+ #httpServer;
625
+ #coordinatorSocket;
626
+ #returnValidatedExtraHeaders(headers) {
627
+ for (const [key, value] of Object.entries(headers)) {
628
+ if (value === void 0) {
629
+ throw new Error(`Extra header is undefined: ${key}`);
630
+ }
631
+ }
632
+ return headers;
633
+ }
634
+ #createCoordinatorSocket() {
635
+ const extraHeaders = this.#returnValidatedExtraHeaders({
636
+ "x-machine-name": MACHINE_NAME2,
637
+ "x-pod-name": POD_NAME,
638
+ "x-trigger-content-hash": this.contentHash,
639
+ "x-trigger-project-ref": this.projectRef,
640
+ "x-trigger-attempt-id": this.attemptId,
641
+ "x-trigger-env-id": this.envId,
642
+ "x-trigger-deployment-id": this.deploymentId,
643
+ "x-trigger-run-id": this.runId
644
+ });
645
+ logger2.log("connecting to coordinator", {
646
+ host: COORDINATOR_HOST,
647
+ port: COORDINATOR_PORT,
648
+ extraHeaders
649
+ });
650
+ const coordinatorConnection = new ZodSocketConnection2({
651
+ namespace: "prod-worker",
652
+ host: COORDINATOR_HOST,
653
+ port: COORDINATOR_PORT,
654
+ clientMessages: ProdWorkerToCoordinatorMessages,
655
+ serverMessages: CoordinatorToProdWorkerMessages,
656
+ extraHeaders,
657
+ handlers: {
658
+ RESUME: async (message) => {
659
+ for (let i = 0; i < message.completions.length; i++) {
660
+ const completion = message.completions[i];
661
+ const execution = message.executions[i];
662
+ if (!completion || !execution)
663
+ continue;
664
+ this.#backgroundWorker.taskRunCompletedNotification(completion, execution);
665
+ }
666
+ },
667
+ RESUME_AFTER_DURATION: async (message) => {
668
+ this.#backgroundWorker.waitCompletedNotification();
669
+ },
670
+ EXECUTE_TASK_RUN: async ({ executionPayload }) => {
671
+ if (this.executing) {
672
+ logger2.error("dropping execute request, already executing");
673
+ return;
674
+ }
675
+ if (this.completed.has(executionPayload.execution.attempt.id)) {
676
+ logger2.error("dropping execute request, already completed");
677
+ return;
678
+ }
679
+ this.executing = true;
680
+ const completion = await this.#backgroundWorker.executeTaskRun(executionPayload);
681
+ logger2.log("completed", completion);
682
+ this.completed.add(executionPayload.execution.attempt.id);
683
+ this.executing = false;
684
+ await this.#backgroundWorker.flushTelemetry();
685
+ const { didCheckpoint, shouldExit } = await this.#coordinatorSocket.socket.emitWithAck(
686
+ "TASK_RUN_COMPLETED",
687
+ {
688
+ version: "v1",
689
+ execution: executionPayload.execution,
690
+ completion
691
+ }
692
+ );
693
+ logger2.log("completion acknowledged", { didCheckpoint, shouldExit });
694
+ if (shouldExit) {
695
+ await this.#backgroundWorker.close();
696
+ process.exit(0);
697
+ }
698
+ this.#coordinatorSocket.close();
699
+ this.#coordinatorSocket.connect();
700
+ },
701
+ REQUEST_ATTEMPT_CANCELLATION: async (message) => {
702
+ if (!this.executing) {
703
+ return;
704
+ }
705
+ await this.#backgroundWorker.cancelAttempt(message.attemptId);
706
+ },
707
+ REQUEST_EXIT: async () => {
708
+ process.exit(0);
709
+ }
710
+ },
711
+ onConnection: async (socket, handler, sender, logger3) => {
712
+ if (process.env.INDEX_TASKS === "true") {
713
+ try {
714
+ const taskResources = await this.#initializeWorker();
715
+ const { success } = await socket.emitWithAck("INDEX_TASKS", {
716
+ version: "v1",
717
+ deploymentId: this.deploymentId,
718
+ ...taskResources
719
+ });
720
+ if (success) {
721
+ logger3("indexing done, shutting down..");
722
+ process.exit(0);
723
+ } else {
724
+ logger3("indexing failure, shutting down..");
725
+ process.exit(1);
726
+ }
727
+ } catch (e) {
728
+ if (e instanceof UncaughtExceptionError) {
729
+ logger3("uncaught exception", e.originalError.message);
730
+ socket.emit("INDEXING_FAILED", {
731
+ version: "v1",
732
+ deploymentId: this.deploymentId,
733
+ error: {
734
+ name: e.originalError.name,
735
+ message: e.originalError.message,
736
+ stack: e.originalError.stack
737
+ }
738
+ });
739
+ } else if (e instanceof Error) {
740
+ logger3("error", e.message);
741
+ socket.emit("INDEXING_FAILED", {
742
+ version: "v1",
743
+ deploymentId: this.deploymentId,
744
+ error: {
745
+ name: e.name,
746
+ message: e.message,
747
+ stack: e.stack
748
+ }
749
+ });
750
+ } else if (typeof e === "string") {
751
+ logger3("string error", e);
752
+ socket.emit("INDEXING_FAILED", {
753
+ version: "v1",
754
+ deploymentId: this.deploymentId,
755
+ error: {
756
+ name: "Error",
757
+ message: e
758
+ }
759
+ });
760
+ } else {
761
+ logger3("unknown error", e);
762
+ socket.emit("INDEXING_FAILED", {
763
+ version: "v1",
764
+ deploymentId: this.deploymentId,
765
+ error: {
766
+ name: "Error",
767
+ message: "Unknown error"
768
+ }
769
+ });
770
+ }
771
+ setTimeout(() => {
772
+ process.exit(1);
773
+ }, 200);
774
+ }
775
+ }
776
+ if (this.paused) {
777
+ if (!this.nextResumeAfter) {
778
+ return;
779
+ }
780
+ socket.emit("READY_FOR_RESUME", {
781
+ version: "v1",
782
+ attemptId: this.attemptId,
783
+ type: this.nextResumeAfter
784
+ });
785
+ this.#backgroundWorker.waitCompletedNotification();
786
+ this.paused = false;
787
+ return;
788
+ }
789
+ if (this.executing) {
790
+ return;
791
+ }
792
+ socket.emit("READY_FOR_EXECUTION", {
793
+ version: "v1",
794
+ attemptId: this.attemptId,
795
+ runId: this.runId
796
+ });
797
+ }
798
+ });
799
+ return coordinatorConnection;
800
+ }
801
+ #createHttpServer() {
802
+ const httpServer = createServer(async (req, res) => {
803
+ logger2.log(`[${req.method}]`, req.url);
804
+ const reply = new HttpReply(res);
805
+ switch (req.url) {
806
+ case "/complete":
807
+ setTimeout(() => process.exit(0), 1e3);
808
+ return reply.text("ok");
809
+ case "/date":
810
+ const date = /* @__PURE__ */ new Date();
811
+ return reply.text(date.toString());
812
+ case "/fail":
813
+ setTimeout(() => process.exit(1), 1e3);
814
+ return reply.text("ok");
815
+ case "/health":
816
+ return reply.text("ok");
817
+ case "/whoami":
818
+ return reply.text(this.contentHash);
819
+ case "/wait":
820
+ const { willCheckpointAndRestore } = await this.#coordinatorSocket.sendWithAck(
821
+ "WAIT_FOR_DURATION",
822
+ {
823
+ version: "v1",
824
+ ms: 6e4
825
+ }
826
+ );
827
+ logger2.log("WAIT_FOR_DURATION", { willCheckpointAndRestore });
828
+ this.#coordinatorSocket.close();
829
+ return reply.text("sent WAIT");
830
+ case "/connect":
831
+ this.#coordinatorSocket.connect();
832
+ return reply.empty();
833
+ case "/close":
834
+ this.#coordinatorSocket.sendWithAck("LOG", {
835
+ version: "v1",
836
+ text: "close without delay"
837
+ });
838
+ this.#coordinatorSocket.close();
839
+ return reply.empty();
840
+ case "/close-delay":
841
+ this.#coordinatorSocket.sendWithAck("LOG", {
842
+ version: "v1",
843
+ text: "close with delay"
844
+ });
845
+ setTimeout(() => {
846
+ this.#coordinatorSocket.close();
847
+ }, 200);
848
+ return reply.empty();
849
+ case "/log":
850
+ this.#coordinatorSocket.sendWithAck("LOG", {
851
+ version: "v1",
852
+ text: await getTextBody(req)
853
+ });
854
+ return reply.empty();
855
+ case "/preStop":
856
+ logger2.log("should do preStop stuff, e.g. checkpoint and graceful shutdown");
857
+ return reply.text("got preStop request");
858
+ case "/ready":
859
+ this.#coordinatorSocket.send("READY_FOR_EXECUTION", {
860
+ version: "v1",
861
+ attemptId: this.attemptId,
862
+ runId: this.runId
863
+ });
864
+ return reply.empty();
865
+ default:
866
+ return reply.empty(404);
867
+ }
868
+ });
869
+ httpServer.on("clientError", (err, socket) => {
870
+ socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
871
+ });
872
+ httpServer.on("listening", () => {
873
+ logger2.log("http server listening on port", this.#httpPort);
874
+ });
875
+ httpServer.on("error", (error) => {
876
+ if (error.code != "EADDRINUSE") {
877
+ return;
878
+ }
879
+ logger2.error(`port ${this.#httpPort} already in use, retrying with random port..`);
880
+ this.#httpPort = getRandomPortNumber();
881
+ setTimeout(() => {
882
+ this.start();
883
+ }, 100);
884
+ });
885
+ return httpServer;
886
+ }
887
+ async #initializeWorker() {
888
+ const envVars = await this.#fetchEnvironmentVariables();
889
+ await this.#backgroundWorker.initialize({ env: envVars });
890
+ let packageVersion;
891
+ const taskResources = [];
892
+ if (!this.#backgroundWorker.tasks) {
893
+ throw new Error(`Background Worker started without tasks`);
894
+ }
895
+ for (const task of this.#backgroundWorker.tasks) {
896
+ taskResources.push({
897
+ id: task.id,
898
+ filePath: task.filePath,
899
+ exportName: task.exportName
900
+ });
901
+ packageVersion = task.packageVersion;
902
+ }
903
+ if (!packageVersion) {
904
+ throw new Error(`Background Worker started without package version`);
905
+ }
906
+ return {
907
+ packageVersion,
908
+ tasks: taskResources
909
+ };
910
+ }
911
+ async #fetchEnvironmentVariables() {
912
+ const response = await fetch(`${this.apiUrl}/api/v1/projects/${this.projectRef}/envvars`, {
913
+ method: "GET",
914
+ headers: {
915
+ Authorization: `Bearer ${this.apiKey}`
916
+ }
917
+ });
918
+ if (!response.ok) {
919
+ return {};
920
+ }
921
+ const data = await response.json();
922
+ return data?.variables ?? {};
923
+ }
924
+ start() {
925
+ this.#httpServer.listen(this.#httpPort, this.host);
926
+ }
927
+ };
928
+ var prodWorker = new ProdWorker(HTTP_SERVER_PORT2);
929
+ prodWorker.start();
930
+ function gatherProcessEnv() {
931
+ const env = {
932
+ NODE_ENV: process.env.NODE_ENV ?? "production",
933
+ PATH: process.env.PATH,
934
+ USER: process.env.USER,
935
+ SHELL: process.env.SHELL,
936
+ LANG: process.env.LANG,
937
+ TERM: process.env.TERM,
938
+ NODE_PATH: process.env.NODE_PATH,
939
+ HOME: process.env.HOME
940
+ };
941
+ return Object.fromEntries(Object.entries(env).filter(([key, value]) => value !== void 0));
942
+ }