Skip to content
File

Blob: src/worker/dispatch/workflows/steps/finalize.ts

typescript29 lines
1import { type UnixTimestampMs as UnixTimestampMsType } from "@/contracts";
2import { type AcceptedRunSnapshot, type PositiveInteger } from "@/worker/contracts";
3import { createRunExecutionContext, type RunExecutionOutcome } from "@/worker/dispatch/shared/run-execution-context";
4import { finalizeExecution, RunLease } from "@/worker/dispatch/shared";
5import { toExecutionMaterial, toWorkflowClaim } from "../execution";
6 
7export const finalizeWorkflowRun = async (
8 env: Env,
9 snapshot: AcceptedRunSnapshot,
10 startedAt: UnixTimestampMsType,
11 outcome: RunExecutionOutcome,
12 position: PositiveInteger | null = null,
13): Promise<void> => {
14 const context = createRunExecutionContext(env, toExecutionMaterial(snapshot), toWorkflowClaim(snapshot), {
15 startedAt,
16 });
17 const lease = new RunLease(context);
18 
19 if (position !== null) {
20 context.state.currentStepPosition = position;
21 }
22 
23 try {
24 await finalizeExecution(context, lease, outcome);
25 } finally {
26 context.runtime.dispose();
27 }
28};