86 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-04-09 15:25:49 +02:00
export class ProcessControl {
2025-04-14 13:56:55 +02:00
constructor(configService, shellService, osService, fsService, codexGlobals) {
this.configService = configService;
2025-04-09 15:25:49 +02:00
this.shell = shellService;
this.os = osService;
this.fs = fsService;
2025-04-14 14:31:22 +02:00
this.codexGlobals = codexGlobals;
2025-04-09 15:25:49 +02:00
}
2025-04-14 13:56:55 +02:00
getCodexProcesses = async () => {
const processes = await this.os.listProcesses();
2025-04-09 15:25:49 +02:00
if (this.os.isWindows()) {
2025-04-14 13:56:55 +02:00
return processes.filter((p) => p.name === "codex.exe");
2025-04-09 15:25:49 +02:00
} else {
return processes.filter(
(p) => p.name === "codex" && !p.cmd.includes("<defunct>"),
);
2025-04-09 15:25:49 +02:00
}
};
2025-04-09 15:25:49 +02:00
2025-04-14 13:56:55 +02:00
getNumberOfCodexProcesses = async () => {
return (await this.getCodexProcesses()).length;
};
2025-04-14 13:56:55 +02:00
stopCodexProcess = async () => {
const processes = await this.getCodexProcesses();
if (processes.length < 1) throw new Error("No codex process found");
2025-04-14 13:56:55 +02:00
const pid = processes[0].pid;
await this.stopProcess(pid);
};
stopProcess = async (pid) => {
2025-04-21 10:41:44 +02:00
this.os.stopProcess(pid);
2025-04-15 14:01:44 +02:00
await this.sleep();
if (await this.isProcessRunning(pid)) {
this.os.terminateProcess(pid);
await this.sleep();
}
};
isProcessRunning = async (pid) => {
const processes = await this.os.listProcesses();
const p = processes.filter((p) => p.pid == pid);
const result = p.length > 0;
return result;
};
2025-04-14 13:56:55 +02:00
startCodexProcess = async () => {
2025-04-14 14:31:22 +02:00
await this.saveCodexConfigFile();
await this.startCodex();
2025-04-15 14:01:44 +02:00
await this.sleep();
};
sleep = async () => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 5000);
});
2025-04-14 13:56:55 +02:00
};
2025-04-09 15:25:49 +02:00
2025-04-14 13:56:55 +02:00
saveCodexConfigFile = async () => {
const publicIp = await this.codexGlobals.getPublicIp();
const bootstrapNodes = await this.codexGlobals.getTestnetSPRs();
this.configService.writeCodexConfigFile(publicIp, bootstrapNodes);
};
2025-04-09 15:25:49 +02:00
2025-04-14 13:56:55 +02:00
startCodex = async () => {
2025-04-21 10:41:44 +02:00
const executable = this.configService.getCodexExe();
const workingDir = this.configService.get().codexRoot;
const args = [
`--config-file=${this.configService.getCodexConfigFilePath()}`,
2025-04-21 15:03:43 +02:00
// Marketplace client parameters cannot be set via config file.
// Open issue: https://github.com/codex-storage/nim-codex/issues/1206
// So we're setting them here.
"persistence",
`--eth-provider=${this.codexGlobals.getEthProvider()}`,
`--eth-private-key=eth.key`, // duplicated in configService.
2025-04-21 10:41:44 +02:00
];
await this.shell.spawnDetachedProcess(executable, workingDir, args);
};
2025-04-09 15:25:49 +02:00
}