@tracemarketplace/cli 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
2
+ import { join } from "path";
3
+ import { getSubmitLogPath, getSubmitWorkerStateDir } from "./config.js";
4
+
5
+ export interface SubmitWorkerState {
6
+ pid: number;
7
+ startedAt: string;
8
+ readyChunkCount: number;
9
+ readySessionCount: number;
10
+ logPath: string;
11
+ }
12
+
13
+ interface SubmitWorkerStateOptions {
14
+ dir?: string;
15
+ }
16
+
17
+ export function isProcessRunning(pid: number): boolean {
18
+ try {
19
+ process.kill(pid, 0);
20
+ return true;
21
+ } catch (err) {
22
+ return (err as NodeJS.ErrnoException).code === "EPERM";
23
+ }
24
+ }
25
+
26
+ export function writeSubmitWorkerState(
27
+ profile: string,
28
+ state: SubmitWorkerState,
29
+ opts: SubmitWorkerStateOptions = {},
30
+ ): void {
31
+ const dir = resolveStateDir(profile, opts.dir);
32
+ mkdirSync(dir, { recursive: true });
33
+ writeFileSync(join(dir, `${state.pid}.json`), JSON.stringify(state, null, 2) + "\n", "utf-8");
34
+ }
35
+
36
+ export function removeSubmitWorkerState(
37
+ profile: string,
38
+ pid: number,
39
+ opts: SubmitWorkerStateOptions = {},
40
+ ): void {
41
+ const dir = resolveStateDir(profile, opts.dir);
42
+ const path = join(dir, `${pid}.json`);
43
+ if (!existsSync(path)) {
44
+ return;
45
+ }
46
+
47
+ try {
48
+ unlinkSync(path);
49
+ } catch {}
50
+ }
51
+
52
+ export function listActiveSubmitWorkers(
53
+ profile: string,
54
+ opts: SubmitWorkerStateOptions = {},
55
+ ): SubmitWorkerState[] {
56
+ const dir = resolveStateDir(profile, opts.dir);
57
+ if (!existsSync(dir)) {
58
+ return [];
59
+ }
60
+
61
+ const workers: SubmitWorkerState[] = [];
62
+ for (const entry of readdirSync(dir)) {
63
+ if (!entry.endsWith(".json")) {
64
+ continue;
65
+ }
66
+
67
+ const path = join(dir, entry);
68
+ const parsed = readSubmitWorkerStateFile(path);
69
+ if (!parsed || !isProcessRunning(parsed.pid)) {
70
+ try {
71
+ unlinkSync(path);
72
+ } catch {}
73
+ continue;
74
+ }
75
+
76
+ workers.push(parsed);
77
+ }
78
+
79
+ return workers.sort((a, b) => a.startedAt.localeCompare(b.startedAt));
80
+ }
81
+
82
+ export function createSubmitWorkerState(
83
+ profile: string,
84
+ pid: number,
85
+ startedAt: string,
86
+ readyChunkCount: number,
87
+ readySessionCount: number,
88
+ ): SubmitWorkerState {
89
+ return {
90
+ pid,
91
+ startedAt,
92
+ readyChunkCount,
93
+ readySessionCount,
94
+ logPath: getSubmitLogPath(profile),
95
+ };
96
+ }
97
+
98
+ function resolveStateDir(profile: string, dir?: string): string {
99
+ return dir ?? getSubmitWorkerStateDir(profile);
100
+ }
101
+
102
+ function readSubmitWorkerStateFile(path: string): SubmitWorkerState | null {
103
+ try {
104
+ const value = JSON.parse(readFileSync(path, "utf-8")) as Partial<SubmitWorkerState>;
105
+ if (
106
+ typeof value.pid !== "number"
107
+ || !Number.isInteger(value.pid)
108
+ || value.pid <= 0
109
+ || typeof value.startedAt !== "string"
110
+ || typeof value.readyChunkCount !== "number"
111
+ || typeof value.readySessionCount !== "number"
112
+ || typeof value.logPath !== "string"
113
+ ) {
114
+ return null;
115
+ }
116
+
117
+ return {
118
+ pid: value.pid,
119
+ startedAt: value.startedAt,
120
+ readyChunkCount: value.readyChunkCount,
121
+ readySessionCount: value.readySessionCount,
122
+ logPath: value.logPath,
123
+ };
124
+ } catch {
125
+ return null;
126
+ }
127
+ }