File size: 4,026 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { RunRecord, RunState, TerminationReason } from "./types.js";

function nowMs() {
  return Date.now();
}

const DEFAULT_MAX_EXITED_RECORDS = 2_000;

function resolveMaxExitedRecords(value?: number): number {
  if (typeof value !== "number" || !Number.isFinite(value) || value < 1) {
    return DEFAULT_MAX_EXITED_RECORDS;
  }
  return Math.max(1, Math.floor(value));
}

export type RunRegistry = {
  add: (record: RunRecord) => void;
  get: (runId: string) => RunRecord | undefined;
  list: () => RunRecord[];
  listByScope: (scopeKey: string) => RunRecord[];
  updateState: (
    runId: string,
    state: RunState,
    patch?: Partial<Pick<RunRecord, "pid" | "terminationReason" | "exitCode" | "exitSignal">>,
  ) => RunRecord | undefined;
  touchOutput: (runId: string) => void;
  finalize: (
    runId: string,
    exit: {
      reason: TerminationReason;
      exitCode: number | null;
      exitSignal: NodeJS.Signals | number | null;
    },
  ) => { record: RunRecord; firstFinalize: boolean } | null;
  delete: (runId: string) => void;
};

export function createRunRegistry(options?: { maxExitedRecords?: number }): RunRegistry {
  const records = new Map<string, RunRecord>();
  const maxExitedRecords = resolveMaxExitedRecords(options?.maxExitedRecords);

  const pruneExitedRecords = () => {
    if (!records.size) {
      return;
    }
    let exited = 0;
    for (const record of records.values()) {
      if (record.state === "exited") {
        exited += 1;
      }
    }
    if (exited <= maxExitedRecords) {
      return;
    }
    let remove = exited - maxExitedRecords;
    for (const [runId, record] of records.entries()) {
      if (remove <= 0) {
        break;
      }
      if (record.state !== "exited") {
        continue;
      }
      records.delete(runId);
      remove -= 1;
    }
  };

  const add: RunRegistry["add"] = (record) => {
    records.set(record.runId, { ...record });
  };

  const get: RunRegistry["get"] = (runId) => {
    const record = records.get(runId);
    return record ? { ...record } : undefined;
  };

  const list: RunRegistry["list"] = () => {
    return Array.from(records.values()).map((record) => ({ ...record }));
  };

  const listByScope: RunRegistry["listByScope"] = (scopeKey) => {
    if (!scopeKey.trim()) {
      return [];
    }
    return Array.from(records.values())
      .filter((record) => record.scopeKey === scopeKey)
      .map((record) => ({ ...record }));
  };

  const updateState: RunRegistry["updateState"] = (runId, state, patch) => {
    const current = records.get(runId);
    if (!current) {
      return undefined;
    }
    const updatedAtMs = nowMs();
    const next: RunRecord = {
      ...current,
      ...patch,
      state,
      updatedAtMs,
      lastOutputAtMs: current.lastOutputAtMs,
    };
    records.set(runId, next);
    return { ...next };
  };

  const touchOutput: RunRegistry["touchOutput"] = (runId) => {
    const current = records.get(runId);
    if (!current) {
      return;
    }
    const ts = nowMs();
    records.set(runId, {
      ...current,
      lastOutputAtMs: ts,
      updatedAtMs: ts,
    });
  };

  const finalize: RunRegistry["finalize"] = (runId, exit) => {
    const current = records.get(runId);
    if (!current) {
      return null;
    }
    const firstFinalize = current.state !== "exited";
    const ts = nowMs();
    const next: RunRecord = {
      ...current,
      state: "exited",
      terminationReason: current.terminationReason ?? exit.reason,
      exitCode: current.exitCode !== undefined ? current.exitCode : exit.exitCode,
      exitSignal: current.exitSignal !== undefined ? current.exitSignal : exit.exitSignal,
      updatedAtMs: ts,
    };
    records.set(runId, next);
    pruneExitedRecords();
    return { record: { ...next }, firstFinalize };
  };

  const del: RunRegistry["delete"] = (runId) => {
    records.delete(runId);
  };

  return {
    add,
    get,
    list,
    listByScope,
    updateState,
    touchOutput,
    finalize,
    delete: del,
  };
}