Skip to content

Commit 7eef7e3

Browse files
committed
also parse execute errors in deployed worker
1 parent cba095e commit 7eef7e3

File tree

3 files changed

+54
-58
lines changed

3 files changed

+54
-58
lines changed

packages/cli-v3/src/dev/backgroundWorker.ts

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
CreateBackgroundWorkerResponse,
44
ServerBackgroundWorker,
55
TaskRunBuiltInError,
6-
TaskRunErrorCodes,
76
TaskRunExecution,
87
TaskRunExecutionPayload,
98
TaskRunExecutionResult,
@@ -14,13 +13,7 @@ import {
1413
import { Evt } from "evt";
1514

1615
import { join } from "node:path";
17-
import {
18-
CancelledProcessError,
19-
CleanupProcessError,
20-
SigKillTimeoutProcessError,
21-
UnexpectedExitError,
22-
getFriendlyErrorMessage,
23-
} from "@trigger.dev/core/v3/errors";
16+
import { SigKillTimeoutProcessError } from "@trigger.dev/core/v3/errors";
2417
import { TaskRunProcess, TaskRunProcessOptions } from "../executions/taskRunProcess.js";
2518
import { indexWorkerManifest } from "../indexing/indexWorkerManifest.js";
2619
import { prettyError } from "../utilities/cliOutput.js";
@@ -490,7 +483,7 @@ export class BackgroundWorker {
490483
const error = result.error;
491484

492485
if (error.type === "BUILT_IN_ERROR") {
493-
const mappedError = await this.#correctError(error, payload.execution);
486+
const mappedError = await this.#correctError(error);
494487

495488
return {
496489
...result,
@@ -500,61 +493,16 @@ export class BackgroundWorker {
500493

501494
return result;
502495
} catch (e) {
503-
if (e instanceof CancelledProcessError) {
504-
return {
505-
id: payload.execution.run.id,
506-
ok: false,
507-
retry: undefined,
508-
error: {
509-
type: "INTERNAL_ERROR",
510-
code: TaskRunErrorCodes.TASK_RUN_CANCELLED,
511-
},
512-
};
513-
}
514-
515-
if (e instanceof CleanupProcessError) {
516-
return {
517-
id: payload.execution.run.id,
518-
ok: false,
519-
retry: undefined,
520-
error: {
521-
type: "INTERNAL_ERROR",
522-
code: TaskRunErrorCodes.TASK_EXECUTION_ABORTED,
523-
},
524-
};
525-
}
526-
527-
if (e instanceof UnexpectedExitError) {
528-
return {
529-
id: payload.execution.run.id,
530-
ok: false,
531-
retry: undefined,
532-
error: {
533-
type: "INTERNAL_ERROR",
534-
code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE,
535-
message: getFriendlyErrorMessage(e.code, e.signal, e.stderr),
536-
stackTrace: e.stderr,
537-
},
538-
};
539-
}
540-
541496
return {
542497
id: payload.execution.run.id,
543498
ok: false,
544499
retry: undefined,
545-
error: {
546-
type: "INTERNAL_ERROR",
547-
code: TaskRunErrorCodes.TASK_EXECUTION_FAILED,
548-
message: String(e),
549-
},
500+
error: TaskRunProcess.parseExecuteError(e),
550501
};
551502
}
552503
}
553504

554-
async #correctError(
555-
error: TaskRunBuiltInError,
556-
execution: TaskRunExecution
557-
): Promise<TaskRunBuiltInError> {
505+
async #correctError(error: TaskRunBuiltInError): Promise<TaskRunBuiltInError> {
558506
return {
559507
...error,
560508
stackTrace: correctErrorStackTrace(error.stackTrace, this.params.cwd),

packages/cli-v3/src/entryPoints/deploy-run-controller.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,22 @@ class ProdWorker {
784784
error,
785785
});
786786

787-
this.#failRun(message.lazyPayload.runId, error);
787+
try {
788+
await this._taskRunProcess.cancel();
789+
} catch (error) {
790+
logger.error("Failed to cancel task run process", { error });
791+
}
792+
793+
try {
794+
await this.#submitAttemptCompletion(execution, {
795+
id: execution.run.id,
796+
ok: false,
797+
retry: undefined,
798+
error: TaskRunProcess.parseExecuteError(error),
799+
});
800+
} catch (error) {
801+
this.#failRun(message.lazyPayload.runId, error);
802+
}
788803
}
789804
},
790805
REQUEST_ATTEMPT_CANCELLATION: async (message) => {

packages/cli-v3/src/executions/taskRunProcess.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
ExecutorToWorkerMessageCatalog,
33
ServerBackgroundWorker,
4+
TaskRunErrorCodes,
45
TaskRunExecution,
56
TaskRunExecutionPayload,
67
TaskRunExecutionResult,
8+
type TaskRunInternalError,
79
WorkerManifest,
810
WorkerToExecutorMessageCatalog,
911
} from "@trigger.dev/core/v3";
@@ -21,10 +23,10 @@ import { logger } from "../utilities/logger.js";
2123
import {
2224
CancelledProcessError,
2325
CleanupProcessError,
26+
getFriendlyErrorMessage,
2427
GracefulExitTimeoutError,
2528
UnexpectedExitError,
2629
} from "@trigger.dev/core/v3/errors";
27-
import { env } from "std-env";
2830

2931
export type OnWaitForDurationMessage = InferSocketMessageSchema<
3032
typeof ExecutorToWorkerMessageCatalog,
@@ -383,6 +385,37 @@ export class TaskRunProcess {
383385
get pid() {
384386
return this._childPid;
385387
}
388+
389+
static parseExecuteError(error: unknown): TaskRunInternalError {
390+
if (error instanceof CancelledProcessError) {
391+
return {
392+
type: "INTERNAL_ERROR",
393+
code: TaskRunErrorCodes.TASK_RUN_CANCELLED,
394+
};
395+
}
396+
397+
if (error instanceof CleanupProcessError) {
398+
return {
399+
type: "INTERNAL_ERROR",
400+
code: TaskRunErrorCodes.TASK_EXECUTION_ABORTED,
401+
};
402+
}
403+
404+
if (error instanceof UnexpectedExitError) {
405+
return {
406+
type: "INTERNAL_ERROR",
407+
code: TaskRunErrorCodes.TASK_PROCESS_EXITED_WITH_NON_ZERO_CODE,
408+
message: getFriendlyErrorMessage(error.code, error.signal, error.stderr),
409+
stackTrace: error.stderr,
410+
};
411+
}
412+
413+
return {
414+
type: "INTERNAL_ERROR",
415+
code: TaskRunErrorCodes.TASK_EXECUTION_FAILED,
416+
message: String(error),
417+
};
418+
}
386419
}
387420

388421
function executorArgs(workerManifest: WorkerManifest): string[] {

0 commit comments

Comments
 (0)