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) {
|
2025-04-14 11:27:58 +02:00
|
|
|
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 {
|
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-14 11:27:58 +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:25:51 +02:00
|
|
|
|
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:25:51 +02:00
|
|
|
|
2025-04-14 13:56:55 +02:00
|
|
|
const pid = processes[0].pid;
|
2025-04-21 10:41:44 +02:00
|
|
|
this.os.stopProcess(pid);
|
2025-04-15 14:01:44 +02:00
|
|
|
await this.sleep();
|
2025-04-14 13:56:55 +02:00
|
|
|
};
|
2025-04-14 13:25:51 +02:00
|
|
|
|
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()}`,
|
|
|
|
|
];
|
|
|
|
|
await this.shell.spawnDetachedProcess(executable, workingDir, args);
|
2025-04-14 11:27:58 +02:00
|
|
|
};
|
2025-04-09 15:25:49 +02:00
|
|
|
}
|