Skip to content
File

Blob: src/worker/durable/project-do/reconciliation.ts

typescript108 lines
1import type { ProjectId, RunId } from "@/contracts";
2import {
3 dispatchExecutableRun,
4 reconcileCancelRequestedRunDo,
5 reconcileAcceptedRunD1Sync,
6 reconcileProjectIndexReplicaSync,
7 reconcileRunMetadataD1Sync,
8 reconcileSandboxCleanup,
9 reconcileTerminalRunD1Sync,
10 syncRunMetadataToD1,
11 promotePendingRun,
12 reconcileActiveRunWatchdog,
13} from "./reconciliation/index";
14import type { ProjectDoContext } from "./types";
15export interface AlarmCycleProgress {
16 action:
17 | "project_index_sync"
18 | "watchdog_recovery"
19 | "sandbox_cleanup"
20 | "cancel_sync"
21 | "promote_pending"
22 | "d1_create_sync"
23 | "d1_metadata_sync"
24 | "d1_terminal_sync"
25 | "dispatch_enqueue";
26 runId: RunId | null;
27}
28export {
29 dispatchExecutableRun,
30 reconcileAcceptedRunD1Sync,
31 reconcileActiveRunWatchdog,
32 reconcileCancelRequestedRunDo,
33 reconcileProjectIndexReplicaSync,
34 reconcileRunMetadataD1Sync,
35 reconcileSandboxCleanup,
36 reconcileTerminalRunD1Sync,
37 syncRunMetadataToD1,
38};
39export const runAlarmCycle = async (
40 context: ProjectDoContext,
41 projectId: ProjectId,
42): Promise<AlarmCycleProgress | null> => {
43 const projectIndexSynced = await reconcileProjectIndexReplicaSync(context, projectId);
44 if (projectIndexSynced) {
45 return {
46 action: "project_index_sync",
47 runId: null,
48 };
49 }
50 const watchdogRunId = await reconcileActiveRunWatchdog(context, projectId);
51 if (watchdogRunId) {
52 return {
53 action: "watchdog_recovery",
54 runId: watchdogRunId,
55 };
56 }
57 const sandboxCleanupRunId = await reconcileSandboxCleanup(context, projectId);
58 if (sandboxCleanupRunId) {
59 return {
60 action: "sandbox_cleanup",
61 runId: sandboxCleanupRunId,
62 };
63 }
64 const cancelSyncRunId = await reconcileCancelRequestedRunDo(context, projectId);
65 if (cancelSyncRunId) {
66 return {
67 action: "cancel_sync",
68 runId: cancelSyncRunId,
69 };
70 }
71 const promotedRunId = promotePendingRun(context, projectId);
72 if (promotedRunId) {
73 return {
74 action: "promote_pending",
75 runId: promotedRunId,
76 };
77 }
78 const d1CreateRunId = await reconcileAcceptedRunD1Sync(context, projectId);
79 if (d1CreateRunId) {
80 return {
81 action: "d1_create_sync",
82 runId: d1CreateRunId,
83 };
84 }
85 const d1MetadataRunId = await reconcileRunMetadataD1Sync(context, projectId);
86 if (d1MetadataRunId) {
87 return {
88 action: "d1_metadata_sync",
89 runId: d1MetadataRunId,
90 };
91 }
92 const d1TerminalRunId = await reconcileTerminalRunD1Sync(context, projectId);
93 if (d1TerminalRunId) {
94 return {
95 action: "d1_terminal_sync",
96 runId: d1TerminalRunId,
97 };
98 }
99 const dispatchedRunId = await dispatchExecutableRun(context, projectId);
100 if (dispatchedRunId) {
101 return {
102 action: "dispatch_enqueue",
103 runId: dispatchedRunId,
104 };
105 }
106 return null;
107};