File
Blob: src/worker/dispatch/shared/run-execution-context/shared.ts
| 1 | import { type ProjectId, type RunId, UnixTimestampMs } from "@/contracts"; |
| 2 | import { type AcceptedRunSnapshot, expectTrusted, PositiveInteger, type RunMetaState } from "@/worker/contracts"; |
| 3 | import { createLogger } from "@/worker/services"; |
| 4 | |
| 5 | export const logger = createLogger("dispatch.execution"); |
| 6 | export const HEARTBEAT_INTERVAL_MS = 5_000; |
| 7 | export const CANCEL_GRACE_MS = 30_000; |
| 8 | export const PROCESS_WAIT_BUFFER_MS = 35_000; |
| 9 | |
| 10 | export const getProjectStub = (env: Env, projectId: ProjectId) => env.PROJECT_DO.getByName(projectId); |
| 11 | export const getRunStub = (env: Env, runId: RunId) => env.RUN_DO.getByName(runId); |
| 12 | |
| 13 | export const now = (): UnixTimestampMs => expectTrusted(UnixTimestampMs, Date.now(), "UnixTimestampMs"); |
| 14 | export const toPositiveInteger = (value: number): PositiveInteger => |
| 15 | expectTrusted(PositiveInteger, value, "PositiveInteger"); |
| 16 | export const sleep = async (ms: number): Promise<void> => scheduler.wait(ms); |
| 17 | |
| 18 | export const ensureRunInitialized = async (env: Env, snapshot: AcceptedRunSnapshot): Promise<void> => { |
| 19 | await getRunStub(env, snapshot.runId).ensureInitialized({ |
| 20 | runId: snapshot.runId, |
| 21 | projectId: snapshot.projectId, |
| 22 | triggerType: snapshot.triggerType, |
| 23 | branch: snapshot.branch, |
| 24 | commitSha: snapshot.commitSha, |
| 25 | }); |
| 26 | }; |
| 27 | |
| 28 | export const kickProjectReconciliation = async ( |
| 29 | env: Env, |
| 30 | projectId: ProjectId, |
| 31 | runId: RunId, |
| 32 | trigger: string, |
| 33 | ): Promise<void> => { |
| 34 | try { |
| 35 | await getProjectStub(env, projectId).kickReconciliation(); |
| 36 | } catch (error) { |
| 37 | logger.warn("project_reconciliation_kick_failed", { |
| 38 | projectId, |
| 39 | runId, |
| 40 | trigger, |
| 41 | error: error instanceof Error ? error.message : String(error), |
| 42 | }); |
| 43 | } |
| 44 | }; |