50 lines
1.5 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.config = configService.get();
this.shell = shellService;
this.os = osService;
this.fs = fsService;
2025-04-14 13:56:55 +02:00
this.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 {
2025-04-14 13:56:55 +02:00
return processes.filter((p) => p.name === "codex");
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;
process.kill(pid, "SIGINT");
await new Promise((resolve) => setTimeout(resolve, 2000));
2025-04-14 13:56:55 +02:00
};
2025-04-14 13:56:55 +02:00
startCodexProcess = async () => {
this.saveCodexConfigFile();
this.startCodex();
};
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 () => {
const executable = this.config.codexExe;
2025-04-14 11:42:13 +02:00
const args = [`--config-file=${this.config.codexConfigFilePath}`];
2025-04-14 13:56:55 +02:00
await this.shell.spawnDetachedProcess(executable, args);
};
2025-04-09 15:25:49 +02:00
}