2025-04-14 09:50:42 +02:00
|
|
|
import fs from "fs";
|
|
|
|
|
import { spawn, exec } from "child_process";
|
2025-04-14 13:25:51 +02:00
|
|
|
import psList from 'ps-list';
|
2025-04-14 09:50:42 +02:00
|
|
|
|
2025-04-09 15:25:49 +02:00
|
|
|
export class ProcessControl {
|
|
|
|
|
constructor(configService, shellService, osService, fsService) {
|
2025-04-14 11:27:58 +02:00
|
|
|
this.configService = configService;
|
2025-04-09 15:25:49 +02:00
|
|
|
this.config = configService.get();
|
|
|
|
|
this.shell = shellService;
|
|
|
|
|
this.os = osService;
|
|
|
|
|
this.fs = fsService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPublicIp = async () => {
|
|
|
|
|
if (this.os.isWindows()) {
|
|
|
|
|
const result = await this.shell.run(
|
|
|
|
|
"for /f \"delims=\" %a in ('curl -s --ssl-reqd ip.codex.storage') do @echo %a",
|
|
|
|
|
);
|
|
|
|
|
return result.trim();
|
|
|
|
|
} else {
|
|
|
|
|
return await this.shell.run("curl -s https://ip.codex.storage");
|
|
|
|
|
}
|
2025-04-14 11:27:58 +02:00
|
|
|
};
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 13:25:51 +02:00
|
|
|
detectThing = async () => {
|
|
|
|
|
console.log("detecting...");
|
|
|
|
|
|
|
|
|
|
const processes = await psList();
|
|
|
|
|
const codexProcesses = processes.filter((p) => p.name === "codex.exe");
|
|
|
|
|
if (codexProcesses.length > 0) {
|
|
|
|
|
console.log("Codex is already running.");
|
|
|
|
|
codexProcesses.forEach((p) => {
|
|
|
|
|
console.log(`PID: ${JSON.stringify(p)}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log("Stopping codex...");
|
|
|
|
|
await this.stopThing(codexProcesses[0].pid);
|
|
|
|
|
await this.detectThing();
|
|
|
|
|
} else {
|
|
|
|
|
console.log("Codex is not running.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopThing = async (pid) => {
|
|
|
|
|
console.log("stopping process...");
|
|
|
|
|
|
|
|
|
|
process.kill(pid, "SIGINT");
|
|
|
|
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 15:25:49 +02:00
|
|
|
doThing = async () => {
|
2025-04-14 11:27:58 +02:00
|
|
|
if (this.config.dataDir.length < 1)
|
|
|
|
|
throw new Error("Missing config: dataDir");
|
|
|
|
|
if (this.config.logsDir.length < 1)
|
|
|
|
|
throw new Error("Missing config: logsDir");
|
2025-04-09 15:25:49 +02:00
|
|
|
|
|
|
|
|
console.log("start a codex detached");
|
|
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
const executable = this.config.codexExe;
|
2025-04-14 11:42:13 +02:00
|
|
|
const args = [`--config-file=${this.config.codexConfigFilePath}`];
|
|
|
|
|
const bootstrapNodes = [
|
|
|
|
|
"spr:CiUIAhIhAiJvIcA_ZwPZ9ugVKDbmqwhJZaig5zKyLiuaicRcCGqLEgIDARo8CicAJQgCEiECIm8hwD9nA9n26BUoNuarCEllqKDnMrIuK5qJxFwIaosQ3d6esAYaCwoJBJ_f8zKRAnU6KkYwRAIgM0MvWNJL296kJ9gWvfatfmVvT-A7O2s8Mxp8l9c8EW0CIC-h-H-jBVSgFjg3Eny2u33qF7BDnWFzo7fGfZ7_qc9P",
|
2025-04-14 09:50:42 +02:00
|
|
|
];
|
2025-04-14 11:42:13 +02:00
|
|
|
const publicIp = await this.getPublicIp();
|
|
|
|
|
|
|
|
|
|
this.configService.writeCodexConfigFile(publicIp, bootstrapNodes);
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
const command = `"${executable}" ${args.join(" ")}`;
|
|
|
|
|
console.log("command: " + command);
|
|
|
|
|
console.log("\n\n");
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 11:27:58 +02:00
|
|
|
var child = spawn(executable, args, {
|
|
|
|
|
detached: true,
|
2025-04-14 11:42:13 +02:00
|
|
|
//stdio: ["ignore", "ignore", "ignore"],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
child.stdout.on("data", (data) => {
|
|
|
|
|
console.log(`stdout: ${data}`);
|
2025-04-14 11:27:58 +02:00
|
|
|
});
|
2025-04-14 11:42:13 +02:00
|
|
|
|
|
|
|
|
child.stderr.on("data", (data) => {
|
|
|
|
|
console.error(`stderr: ${data}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
child.on("close", (code) => {
|
|
|
|
|
console.log(`child process exited with code ${code}`);
|
|
|
|
|
});
|
|
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
child.unref();
|
2025-04-09 15:25:49 +02:00
|
|
|
|
2025-04-14 09:50:42 +02:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
|
|
|
return;
|
2025-04-14 11:27:58 +02:00
|
|
|
};
|
2025-04-09 15:25:49 +02:00
|
|
|
}
|