File
Blob: src/worker/dispatch/shared/active-run-recovery.ts
| 1 | import { type ProjectId, type RunId } from "@/contracts"; |
| 2 | import { isTerminalStatus } from "@/worker/contracts"; |
| 3 | import { kickProjectReconciliation } from "@/worker/dispatch/shared/run-execution-context"; |
| 4 | |
| 5 | export const recoverTerminalActiveRun = async (env: Env, projectId: ProjectId, runId: RunId): Promise<boolean> => { |
| 6 | const runMeta = await env.RUN_DO.getByName(runId).getRunSummary(runId); |
| 7 | if (!runMeta || !isTerminalStatus(runMeta.status) || runMeta.finishedAt === null) { |
| 8 | return false; |
| 9 | } |
| 10 | |
| 11 | // When sandbox work finished but the dispatch retried before ProjectDO accepted terminalization, |
| 12 | // recover from the trusted RunDO terminal state instead of letting the watchdog misclassify the run later. |
| 13 | await env.PROJECT_DO.getByName(projectId).finalizeRunExecution({ |
| 14 | projectId, |
| 15 | runId, |
| 16 | terminalStatus: runMeta.status, |
| 17 | lastError: runMeta.errorMessage, |
| 18 | sandboxDestroyed: false, |
| 19 | }); |
| 20 | await kickProjectReconciliation(env, projectId, runId, "recover_active_run"); |
| 21 | return true; |
| 22 | }; |