Skip to content
File

Blob: src/worker/dispatch/shared/run-execution-context/shared.ts

typescript45 lines
1import { type ProjectId, type RunId, UnixTimestampMs } from "@/contracts";
2import { type AcceptedRunSnapshot, expectTrusted, PositiveInteger, type RunMetaState } from "@/worker/contracts";
3import { createLogger } from "@/worker/services";
4 
5export const logger = createLogger("dispatch.execution");
6export const HEARTBEAT_INTERVAL_MS = 5_000;
7export const CANCEL_GRACE_MS = 30_000;
8export const PROCESS_WAIT_BUFFER_MS = 35_000;
9 
10export const getProjectStub = (env: Env, projectId: ProjectId) => env.PROJECT_DO.getByName(projectId);
11export const getRunStub = (env: Env, runId: RunId) => env.RUN_DO.getByName(runId);
12 
13export const now = (): UnixTimestampMs => expectTrusted(UnixTimestampMs, Date.now(), "UnixTimestampMs");
14export const toPositiveInteger = (value: number): PositiveInteger =>
15 expectTrusted(PositiveInteger, value, "PositiveInteger");
16export const sleep = async (ms: number): Promise<void> => scheduler.wait(ms);
17 
18export 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 
28export 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};