@sharedots/cli 0.1.2 → 0.1.3
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.
- package/bin/.gitkeep +1 -0
- package/bin/sharedots-daemon.exe +0 -0
- package/dist/index.js +43567 -430
- package/dist/node-worker.js +8457 -0
- package/package.json +13 -6
- package/dist/daemon-runner.d.ts +0 -32
- package/dist/daemon-runner.d.ts.map +0 -1
- package/dist/daemon-runner.js +0 -132
- package/dist/daemon-runner.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/job-runner.d.ts +0 -81
- package/dist/job-runner.d.ts.map +0 -1
- package/dist/job-runner.js +0 -544
- package/dist/job-runner.js.map +0 -1
- package/dist/parser.d.ts +0 -5
- package/dist/parser.d.ts.map +0 -1
- package/dist/parser.js +0 -81
- package/dist/parser.js.map +0 -1
- package/dist/up-wizard.d.ts +0 -21
- package/dist/up-wizard.d.ts.map +0 -1
- package/dist/up-wizard.js +0 -298
- package/dist/up-wizard.js.map +0 -1
- package/src/cli.test.ts +0 -69
- package/src/daemon-runner.ts +0 -165
- package/src/index.ts +0 -526
- package/src/job-runner.test.ts +0 -287
- package/src/job-runner.ts +0 -645
- package/src/parser.ts +0 -77
- package/src/up-wizard.ts +0 -339
- package/tsconfig.json +0 -12
package/src/index.ts
DELETED
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { spawnSync } from "node:child_process";
|
|
3
|
-
import { existsSync, readFileSync, realpathSync, rmSync } from "node:fs";
|
|
4
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
import { decodeInviteCode, encodeInviteCode } from "@sharedots/protocol";
|
|
7
|
-
import { Sharedots } from "sharedots";
|
|
8
|
-
import {
|
|
9
|
-
configPath,
|
|
10
|
-
getHomeDir,
|
|
11
|
-
isProcessRunning,
|
|
12
|
-
logPath,
|
|
13
|
-
pidPath,
|
|
14
|
-
readConfig,
|
|
15
|
-
startDaemonProcess,
|
|
16
|
-
stopDaemonProcess,
|
|
17
|
-
writeConfig,
|
|
18
|
-
} from "./daemon-runner.js";
|
|
19
|
-
import {
|
|
20
|
-
constructDashboardUrl,
|
|
21
|
-
openDashboardBridge,
|
|
22
|
-
runInteractiveJobRunner,
|
|
23
|
-
runScript,
|
|
24
|
-
} from "./job-runner.js";
|
|
25
|
-
import { runUpWizard } from "./up-wizard.js";
|
|
26
|
-
|
|
27
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
28
|
-
|
|
29
|
-
export { parseFlags } from "./parser.js";
|
|
30
|
-
import { parseFlags } from "./parser.js";
|
|
31
|
-
|
|
32
|
-
function printHelp(): void {
|
|
33
|
-
console.log(`Sharedots CLI v0.1.0 — Universal Distributed Compute Network
|
|
34
|
-
|
|
35
|
-
Usage:
|
|
36
|
-
sharedots up [--join CODE] [--cluster] [--contributor] [--yes]
|
|
37
|
-
sharedots join <INVITE_CODE_OR_URL> [--foreground]
|
|
38
|
-
sharedots run [script.py] [--partitions N] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
39
|
-
sharedots dashboard [--coordinator URL] [--port N] [--hosted]
|
|
40
|
-
sharedots tui [--coordinator URL]
|
|
41
|
-
sharedots room create [--name NAME] [--coordinator URL]
|
|
42
|
-
sharedots worker join <ROOM_ID> --token <SECRET> [--coordinator URL]
|
|
43
|
-
sharedots worker start [--cpu-limit N] [--memory-limit MB] [--gpu] [--foreground]
|
|
44
|
-
sharedots worker status
|
|
45
|
-
sharedots worker stop
|
|
46
|
-
sharedots worker leave
|
|
47
|
-
sharedots worker logs [--lines N]
|
|
48
|
-
sharedots job [submit <job.json> | run [script.py]] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
49
|
-
sharedots bench [--size N] [--partitions P] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
50
|
-
`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function main(): Promise<void> {
|
|
54
|
-
const { positional, flags } = parseFlags(process.argv.slice(2));
|
|
55
|
-
const [group, sub, ...rest] = positional;
|
|
56
|
-
|
|
57
|
-
if (!group || flags.help || group === "help") {
|
|
58
|
-
printHelp();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const defaultCoord =
|
|
63
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
64
|
-
process.env.SHAREDOTS_COORDINATOR ??
|
|
65
|
-
"https://sharedots.onrender.com";
|
|
66
|
-
|
|
67
|
-
if (group === "up") {
|
|
68
|
-
await runUpWizard({
|
|
69
|
-
cluster: Boolean(flags.cluster),
|
|
70
|
-
contributor: Boolean(flags.contributor),
|
|
71
|
-
joinCode: typeof flags.join === "string" ? flags.join : sub?.startsWith("sd_join_") ? sub : undefined,
|
|
72
|
-
coordinator: typeof flags.coordinator === "string" ? flags.coordinator : undefined,
|
|
73
|
-
yes: Boolean(flags.yes || flags.y),
|
|
74
|
-
foreground: Boolean(flags.foreground),
|
|
75
|
-
});
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (group === "join") {
|
|
80
|
-
const rawInput = sub ?? rest[0] ?? (typeof flags.code === "string" ? flags.code : undefined);
|
|
81
|
-
if (!rawInput) {
|
|
82
|
-
console.error("Usage: sharedots join <INVITE_CODE_OR_URL> [--foreground]");
|
|
83
|
-
process.exit(1);
|
|
84
|
-
}
|
|
85
|
-
const decoded = decodeInviteCode(rawInput);
|
|
86
|
-
if (!decoded) {
|
|
87
|
-
console.error("✖ Invalid invite code format. Expected 'sd_join_...' or URL containing it.");
|
|
88
|
-
process.exit(1);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
console.log(`Verifying connection to coordinator at ${decoded.coordinatorUrl}...`);
|
|
92
|
-
try {
|
|
93
|
-
const res = await fetch(`${decoded.coordinatorUrl}/api/overview`, { signal: AbortSignal.timeout(3000) });
|
|
94
|
-
if (!res.ok && res.status !== 401 && res.status !== 403) {
|
|
95
|
-
console.warn(`Warning: Coordinator returned status ${res.status}`);
|
|
96
|
-
}
|
|
97
|
-
} catch (err) {
|
|
98
|
-
console.warn(`Notice: Could not ping ${decoded.coordinatorUrl} (${err instanceof Error ? err.message : String(err)}). Continuing with configuration.`);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
writeConfig({
|
|
102
|
-
coordinator: decoded.coordinatorUrl,
|
|
103
|
-
roomId: decoded.roomId,
|
|
104
|
-
token: decoded.token,
|
|
105
|
-
joinedAt: new Date().toISOString(),
|
|
106
|
-
});
|
|
107
|
-
console.log(`Saved worker configuration for room ${decoded.roomId} (${decoded.coordinatorUrl})`);
|
|
108
|
-
|
|
109
|
-
const started = startDaemonProcess({
|
|
110
|
-
coordinator: decoded.coordinatorUrl,
|
|
111
|
-
roomId: decoded.roomId,
|
|
112
|
-
token: decoded.token,
|
|
113
|
-
cpuLimit: typeof flags["cpu-limit"] === "string" ? flags["cpu-limit"] : undefined,
|
|
114
|
-
memoryLimit: typeof flags["memory-limit"] === "string" ? flags["memory-limit"] : undefined,
|
|
115
|
-
gpu: flags.gpu === true,
|
|
116
|
-
foreground: flags.foreground === true,
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
if (started.pid) {
|
|
120
|
-
console.log(`Started sharedots-daemon (PID ${started.pid}) for room ${decoded.roomId}`);
|
|
121
|
-
console.log(`Logs: ${logPath()}`);
|
|
122
|
-
console.log(`Run 'sharedots tui' or 'sharedots worker status' to monitor.`);
|
|
123
|
-
}
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (group === "tui") {
|
|
128
|
-
try {
|
|
129
|
-
const tui = await import("@sharedots/tui");
|
|
130
|
-
if (typeof tui.runTui === "function") {
|
|
131
|
-
await tui.runTui({
|
|
132
|
-
initialCoordinator: defaultCoord,
|
|
133
|
-
});
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
} catch {
|
|
137
|
-
// Fallback to relative path lookup
|
|
138
|
-
const repoRoot = resolve(__dirname, "../../..");
|
|
139
|
-
const candidates = [
|
|
140
|
-
join(repoRoot, "apps", "tui", "dist", "index.js"),
|
|
141
|
-
join(process.cwd(), "apps", "tui", "dist", "index.js"),
|
|
142
|
-
];
|
|
143
|
-
for (const cand of candidates) {
|
|
144
|
-
if (existsSync(cand)) {
|
|
145
|
-
const mod = await import(`file://${cand.replace(/\\/g, "/")}`);
|
|
146
|
-
if (typeof mod.runTui === "function") {
|
|
147
|
-
await mod.runTui({ initialCoordinator: defaultCoord });
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
console.error("Failed to load @sharedots/tui. Please build apps/tui first.");
|
|
153
|
-
process.exit(1);
|
|
154
|
-
}
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (group === "room" && sub === "create") {
|
|
159
|
-
const client = new Sharedots({ coordinator: defaultCoord });
|
|
160
|
-
const name = typeof flags.name === "string" ? flags.name : undefined;
|
|
161
|
-
const room = await client.createRoom(name);
|
|
162
|
-
const inviteCode = encodeInviteCode(defaultCoord, room.roomId, room.secret);
|
|
163
|
-
|
|
164
|
-
if (flags.json) {
|
|
165
|
-
console.log(JSON.stringify({ ...room, inviteCode }, null, 2));
|
|
166
|
-
} else {
|
|
167
|
-
console.log(`Room created successfully`);
|
|
168
|
-
console.log(` ROOM_ID: ${room.roomId}`);
|
|
169
|
-
console.log(` ROOM_SECRET: ${room.secret}`);
|
|
170
|
-
console.log(` COORDINATOR: ${defaultCoord}`);
|
|
171
|
-
console.log(` INVITE_CODE: ${inviteCode}`);
|
|
172
|
-
console.log(`\nJoin a worker with 1-command:`);
|
|
173
|
-
console.log(` sharedots join ${inviteCode}`);
|
|
174
|
-
console.log(`Or classic style:`);
|
|
175
|
-
console.log(` sharedots worker join ${room.roomId} --coordinator ${defaultCoord} --token ${room.secret}`);
|
|
176
|
-
}
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (group === "worker") {
|
|
181
|
-
if (sub === "join") {
|
|
182
|
-
const firstArg = rest[0];
|
|
183
|
-
// If first argument is an invite code, decode it directly
|
|
184
|
-
if (firstArg && (firstArg.startsWith("sd_join_") || firstArg.includes("sd_join_"))) {
|
|
185
|
-
const decoded = decodeInviteCode(firstArg);
|
|
186
|
-
if (decoded) {
|
|
187
|
-
writeConfig({
|
|
188
|
-
coordinator: decoded.coordinatorUrl,
|
|
189
|
-
roomId: decoded.roomId,
|
|
190
|
-
token: decoded.token,
|
|
191
|
-
joinedAt: new Date().toISOString(),
|
|
192
|
-
});
|
|
193
|
-
console.log(`Saved worker configuration for room ${decoded.roomId} (${decoded.coordinatorUrl})`);
|
|
194
|
-
console.log(`Start the background daemon with: sharedots worker start`);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const roomId = firstArg;
|
|
200
|
-
const token =
|
|
201
|
-
(typeof flags.token === "string" ? flags.token : undefined) ?? process.env.SHAREDOTS_TOKEN;
|
|
202
|
-
if (!roomId || !token) {
|
|
203
|
-
console.error("Usage: sharedots worker join <ROOM_ID> --token <SECRET> [--coordinator URL] OR sharedots join <CODE>");
|
|
204
|
-
process.exit(1);
|
|
205
|
-
}
|
|
206
|
-
writeConfig({
|
|
207
|
-
coordinator: defaultCoord,
|
|
208
|
-
roomId,
|
|
209
|
-
token,
|
|
210
|
-
joinedAt: new Date().toISOString(),
|
|
211
|
-
});
|
|
212
|
-
console.log(`Saved worker configuration for room ${roomId} (${defaultCoord})`);
|
|
213
|
-
console.log(`Start the background daemon with: sharedots worker start`);
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (sub === "start") {
|
|
218
|
-
const cfg = readConfig();
|
|
219
|
-
const roomId = (typeof flags.room === "string" ? flags.room : undefined) ?? cfg?.roomId;
|
|
220
|
-
const token = (typeof flags.token === "string" ? flags.token : undefined) ?? cfg?.token;
|
|
221
|
-
const coordinator =
|
|
222
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
223
|
-
cfg?.coordinator ??
|
|
224
|
-
defaultCoord;
|
|
225
|
-
|
|
226
|
-
if (!roomId || !token) {
|
|
227
|
-
console.error("Worker is not joined to a room. Run `sharedots join <INVITE_CODE>` or `sharedots worker join <ROOM_ID> --token <SECRET>` first.");
|
|
228
|
-
process.exit(1);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const started = startDaemonProcess({
|
|
232
|
-
coordinator,
|
|
233
|
-
roomId,
|
|
234
|
-
token,
|
|
235
|
-
cpuLimit: typeof flags["cpu-limit"] === "string" ? flags["cpu-limit"] : undefined,
|
|
236
|
-
memoryLimit: typeof flags["memory-limit"] === "string" ? flags["memory-limit"] : undefined,
|
|
237
|
-
gpu: flags.gpu === true,
|
|
238
|
-
foreground: flags.foreground === true,
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
if (started.pid) {
|
|
242
|
-
console.log(`Started sharedots-daemon (PID ${started.pid}) for room ${roomId}`);
|
|
243
|
-
console.log(`Logs: ${logPath()}`);
|
|
244
|
-
}
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
if (sub === "status") {
|
|
249
|
-
const cfg = readConfig();
|
|
250
|
-
const pidFile = pidPath();
|
|
251
|
-
const pid = existsSync(pidFile) ? Number.parseInt(readFileSync(pidFile, "utf8").trim(), 10) : null;
|
|
252
|
-
const running = pid !== null && Number.isFinite(pid) && isProcessRunning(pid);
|
|
253
|
-
const widFile = join(getHomeDir(), "state", "worker_id");
|
|
254
|
-
const workerId = existsSync(widFile) ? readFileSync(widFile, "utf8").trim() : null;
|
|
255
|
-
|
|
256
|
-
console.log(`Worker Status: ${running ? "running" : "stopped"}`);
|
|
257
|
-
if (pid) console.log(` PID: ${pid}`);
|
|
258
|
-
if (workerId) console.log(` Worker ID: ${workerId}`);
|
|
259
|
-
if (cfg) {
|
|
260
|
-
console.log(` Room ID: ${cfg.roomId}`);
|
|
261
|
-
console.log(` Coordinator: ${cfg.coordinator}`);
|
|
262
|
-
}
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (sub === "stop") {
|
|
267
|
-
const res = stopDaemonProcess();
|
|
268
|
-
if (res.stopped) {
|
|
269
|
-
console.log(`Stopped worker daemon (PID ${res.pid})`);
|
|
270
|
-
} else {
|
|
271
|
-
console.log("Worker daemon was already stopped.");
|
|
272
|
-
}
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (sub === "leave") {
|
|
277
|
-
stopDaemonProcess();
|
|
278
|
-
rmSync(configPath(), { force: true });
|
|
279
|
-
console.log("Worker left room and cleared local room configuration.");
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
if (sub === "logs") {
|
|
284
|
-
const p = logPath();
|
|
285
|
-
if (!existsSync(p)) {
|
|
286
|
-
console.log("No worker logs found yet.");
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
const lines = readFileSync(p, "utf8").split(/\r?\n/);
|
|
290
|
-
const count = typeof flags.lines === "string" ? Number.parseInt(flags.lines, 10) : 50;
|
|
291
|
-
console.log(lines.slice(-count).join("\n"));
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (group === "run") {
|
|
297
|
-
const cfg = readConfig();
|
|
298
|
-
const targetScript = sub ?? (typeof flags.file === "string" ? flags.file : undefined);
|
|
299
|
-
const partitions = typeof flags.partitions === "number"
|
|
300
|
-
? flags.partitions
|
|
301
|
-
: typeof flags.partitions === "string"
|
|
302
|
-
? parseInt(flags.partitions, 10)
|
|
303
|
-
: typeof flags.p === "string" || typeof flags.p === "number"
|
|
304
|
-
? Number(flags.p)
|
|
305
|
-
: undefined;
|
|
306
|
-
|
|
307
|
-
const roomId =
|
|
308
|
-
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
309
|
-
process.env.SHAREDOTS_ROOM_ID ??
|
|
310
|
-
cfg?.roomId;
|
|
311
|
-
const token =
|
|
312
|
-
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
313
|
-
process.env.SHAREDOTS_TOKEN ??
|
|
314
|
-
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
315
|
-
const coordinator =
|
|
316
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
317
|
-
process.env.SHAREDOTS_COORDINATOR ??
|
|
318
|
-
cfg?.coordinator ??
|
|
319
|
-
defaultCoord;
|
|
320
|
-
|
|
321
|
-
if (targetScript) {
|
|
322
|
-
const scriptArgs: string[] = [];
|
|
323
|
-
for (const r of rest) {
|
|
324
|
-
scriptArgs.push(r);
|
|
325
|
-
}
|
|
326
|
-
const reserved = new Set(["coordinator", "room", "token", "partitions", "p", "cpu", "memory", "gpu", "timeout", "json", "help", "h"]);
|
|
327
|
-
for (const [k, v] of Object.entries(flags)) {
|
|
328
|
-
if (reserved.has(k)) continue;
|
|
329
|
-
if (typeof v === "boolean" && v) {
|
|
330
|
-
scriptArgs.push(`--${k}`);
|
|
331
|
-
} else if (typeof v === "string" || typeof v === "number") {
|
|
332
|
-
scriptArgs.push(`--${k}`, String(v));
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
await runScript({
|
|
337
|
-
scriptPath: targetScript,
|
|
338
|
-
coordinator,
|
|
339
|
-
roomId,
|
|
340
|
-
token,
|
|
341
|
-
partitions,
|
|
342
|
-
args: scriptArgs,
|
|
343
|
-
cpu: typeof flags.cpu === "string" ? parseInt(flags.cpu, 10) : typeof flags.cpu === "number" ? flags.cpu : undefined,
|
|
344
|
-
memory: typeof flags.memory === "string" ? flags.memory : undefined,
|
|
345
|
-
gpu: flags.gpu === true,
|
|
346
|
-
timeout: typeof flags.timeout === "string" ? parseInt(flags.timeout, 10) : typeof flags.timeout === "number" ? flags.timeout : undefined,
|
|
347
|
-
});
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// Interactive job runner if no script file was passed
|
|
352
|
-
await runInteractiveJobRunner({
|
|
353
|
-
coordinator,
|
|
354
|
-
roomId,
|
|
355
|
-
token,
|
|
356
|
-
preset: typeof flags.preset === "string" ? flags.preset : undefined,
|
|
357
|
-
partitions,
|
|
358
|
-
});
|
|
359
|
-
return;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (group === "dashboard") {
|
|
363
|
-
const cfg = readConfig();
|
|
364
|
-
const coordinator =
|
|
365
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
366
|
-
process.env.SHAREDOTS_COORDINATOR ??
|
|
367
|
-
cfg?.coordinator ??
|
|
368
|
-
defaultCoord;
|
|
369
|
-
const roomId =
|
|
370
|
-
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
371
|
-
process.env.SHAREDOTS_ROOM_ID ??
|
|
372
|
-
cfg?.roomId;
|
|
373
|
-
const token =
|
|
374
|
-
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
375
|
-
process.env.SHAREDOTS_TOKEN ??
|
|
376
|
-
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
377
|
-
const port = typeof flags.port === "string" ? flags.port : typeof flags.p === "string" ? flags.p : "3000";
|
|
378
|
-
|
|
379
|
-
await openDashboardBridge({
|
|
380
|
-
coordinator,
|
|
381
|
-
roomId,
|
|
382
|
-
token,
|
|
383
|
-
port,
|
|
384
|
-
hosted: flags.hosted === true,
|
|
385
|
-
openBrowser: flags["no-open"] !== true,
|
|
386
|
-
});
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
if (group === "job") {
|
|
391
|
-
if (sub === "submit") {
|
|
392
|
-
const jobFile = rest[0];
|
|
393
|
-
if (!jobFile || !existsSync(resolve(jobFile))) {
|
|
394
|
-
console.error("Usage: sharedots job submit <job.json> [--coordinator URL] [--room ROOM_ID] [--token SECRET]");
|
|
395
|
-
process.exit(1);
|
|
396
|
-
}
|
|
397
|
-
const cfg = readConfig();
|
|
398
|
-
const coordinator =
|
|
399
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
400
|
-
cfg?.coordinator ??
|
|
401
|
-
defaultCoord;
|
|
402
|
-
const roomId =
|
|
403
|
-
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
404
|
-
process.env.SHAREDOTS_ROOM_ID ??
|
|
405
|
-
cfg?.roomId;
|
|
406
|
-
const token =
|
|
407
|
-
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
408
|
-
process.env.SHAREDOTS_TOKEN ??
|
|
409
|
-
cfg?.token;
|
|
410
|
-
|
|
411
|
-
if (!roomId || !token) {
|
|
412
|
-
console.error("Missing --room and --token (or join a room first).");
|
|
413
|
-
process.exit(1);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
const spec = JSON.parse(readFileSync(resolve(jobFile), "utf8"));
|
|
417
|
-
const client = new Sharedots({ coordinator, token });
|
|
418
|
-
const room = await client.connect(roomId);
|
|
419
|
-
const job = await room.submit(spec);
|
|
420
|
-
console.log(`Submitted job ${job.jobId} to room ${roomId}`);
|
|
421
|
-
job.on("progress", (p) => {
|
|
422
|
-
console.log(` progress: ${p.percent}% (${p.completedTasks ?? 0}/${p.totalTasks ?? 0})`);
|
|
423
|
-
});
|
|
424
|
-
const result = await job.result();
|
|
425
|
-
console.log(JSON.stringify(result, null, 2));
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (sub === "run" || !sub) {
|
|
430
|
-
const cfg = readConfig();
|
|
431
|
-
const targetScript = rest[0] ?? (typeof flags.file === "string" ? flags.file : undefined);
|
|
432
|
-
const partitions = typeof flags.partitions === "number"
|
|
433
|
-
? flags.partitions
|
|
434
|
-
: typeof flags.partitions === "string"
|
|
435
|
-
? parseInt(flags.partitions, 10)
|
|
436
|
-
: typeof flags.p === "string" || typeof flags.p === "number"
|
|
437
|
-
? Number(flags.p)
|
|
438
|
-
: undefined;
|
|
439
|
-
|
|
440
|
-
const roomId =
|
|
441
|
-
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
442
|
-
process.env.SHAREDOTS_ROOM_ID ??
|
|
443
|
-
cfg?.roomId;
|
|
444
|
-
const token =
|
|
445
|
-
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
446
|
-
process.env.SHAREDOTS_TOKEN ??
|
|
447
|
-
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
448
|
-
const coordinator =
|
|
449
|
-
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
450
|
-
process.env.SHAREDOTS_COORDINATOR ??
|
|
451
|
-
cfg?.coordinator ??
|
|
452
|
-
defaultCoord;
|
|
453
|
-
|
|
454
|
-
if (targetScript) {
|
|
455
|
-
const scriptArgs: string[] = [];
|
|
456
|
-
const reserved = new Set(["coordinator", "room", "token", "partitions", "p", "cpu", "memory", "gpu", "timeout", "json", "help", "h"]);
|
|
457
|
-
for (const [k, v] of Object.entries(flags)) {
|
|
458
|
-
if (reserved.has(k)) continue;
|
|
459
|
-
if (typeof v === "boolean" && v) {
|
|
460
|
-
scriptArgs.push(`--${k}`);
|
|
461
|
-
} else if (typeof v === "string" || typeof v === "number") {
|
|
462
|
-
scriptArgs.push(`--${k}`, String(v));
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
await runScript({
|
|
467
|
-
scriptPath: targetScript,
|
|
468
|
-
coordinator,
|
|
469
|
-
roomId,
|
|
470
|
-
token,
|
|
471
|
-
partitions,
|
|
472
|
-
args: scriptArgs,
|
|
473
|
-
cpu: typeof flags.cpu === "string" ? parseInt(flags.cpu, 10) : typeof flags.cpu === "number" ? flags.cpu : undefined,
|
|
474
|
-
memory: typeof flags.memory === "string" ? flags.memory : undefined,
|
|
475
|
-
gpu: flags.gpu === true,
|
|
476
|
-
timeout: typeof flags.timeout === "string" ? parseInt(flags.timeout, 10) : typeof flags.timeout === "number" ? flags.timeout : undefined,
|
|
477
|
-
});
|
|
478
|
-
} else {
|
|
479
|
-
await runInteractiveJobRunner({
|
|
480
|
-
coordinator,
|
|
481
|
-
roomId,
|
|
482
|
-
token,
|
|
483
|
-
preset: typeof flags.preset === "string" ? flags.preset : undefined,
|
|
484
|
-
partitions,
|
|
485
|
-
});
|
|
486
|
-
}
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
if (group === "bench") {
|
|
492
|
-
const repoRoot = resolve(__dirname, "../../..");
|
|
493
|
-
const benchScript = join(repoRoot, "scripts", "bench", "run-matrix-bench.mjs");
|
|
494
|
-
if (!existsSync(benchScript)) {
|
|
495
|
-
console.error(`Benchmark script not found at ${benchScript}`);
|
|
496
|
-
process.exit(1);
|
|
497
|
-
}
|
|
498
|
-
const res = spawnSync(process.execPath, [benchScript, ...process.argv.slice(3)], {
|
|
499
|
-
stdio: "inherit",
|
|
500
|
-
});
|
|
501
|
-
process.exit(res.status ?? 0);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
console.error(`Unknown command: ${positional.join(" ")}`);
|
|
505
|
-
printHelp();
|
|
506
|
-
process.exit(1);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const checkIsMain = () => {
|
|
510
|
-
if (!process.argv[1]) return false;
|
|
511
|
-
const target = resolve(fileURLToPath(import.meta.url)).toLowerCase();
|
|
512
|
-
if (resolve(process.argv[1]).toLowerCase() === target) return true;
|
|
513
|
-
try {
|
|
514
|
-
if (realpathSync(process.argv[1]).toLowerCase() === target) return true;
|
|
515
|
-
} catch {}
|
|
516
|
-
return false;
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
const isMain = checkIsMain();
|
|
520
|
-
|
|
521
|
-
if (isMain) {
|
|
522
|
-
main().catch((err) => {
|
|
523
|
-
console.error(err instanceof Error ? err.message : String(err));
|
|
524
|
-
process.exit(1);
|
|
525
|
-
});
|
|
526
|
-
}
|