working example of detecting a running codex instance and stopping it

This commit is contained in:
ThatBen 2025-04-14 13:25:51 +02:00
parent 65e38a8bab
commit 7661d829cb
No known key found for this signature in database
GPG Key ID: E020A7DDCD52E1AB
5 changed files with 49 additions and 6 deletions

15
package-lock.json generated
View File

@ -17,7 +17,8 @@
"inquirer": "^9.2.12",
"mime-types": "^2.1.35",
"nanospinner": "^1.1.0",
"open": "^10.1.0"
"open": "^10.1.0",
"ps-list": "^8.1.1"
},
"bin": {
"codexstorage": "index.js"
@ -1947,6 +1948,18 @@
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/ps-list": {
"version": "8.1.1",
"resolved": "https://registry.npmjs.org/ps-list/-/ps-list-8.1.1.tgz",
"integrity": "sha512-OPS9kEJYVmiO48u/B9qneqhkMvgCxT+Tm28VCEJpheTpl8cJ0ffZRRNgS5mrQRTrX5yRTpaJ+hRDeefXYmmorQ==",
"license": "MIT",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/ramda": {
"version": "0.25.0",
"resolved": "https://registry.npmjs.org/ramda/-/ramda-0.25.0.tgz",

View File

@ -27,12 +27,13 @@
"axios": "^1.6.2",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
"fs-extra": "^11.3.0",
"fs-filesystem": "^2.1.2",
"inquirer": "^9.2.12",
"mime-types": "^2.1.35",
"nanospinner": "^1.1.0",
"fs-extra": "^11.3.0",
"fs-filesystem": "^2.1.2",
"open": "^10.1.0"
"open": "^10.1.0",
"ps-list": "^8.1.1"
},
"devDependencies": {
"prettier": "^3.4.2",

View File

@ -1,5 +1,6 @@
import fs from "fs";
import { spawn, exec } from "child_process";
import psList from 'ps-list';
export class ProcessControl {
constructor(configService, shellService, osService, fsService) {
@ -21,6 +22,33 @@ export class ProcessControl {
}
};
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));
}
doThing = async () => {
if (this.config.dataDir.length < 1)
throw new Error("Missing config: dataDir");

View File

@ -139,7 +139,8 @@ export async function main() {
osService,
fsService,
);
await processControl.doThing();
//await processControl.doThing();
await processControl.detectThing();
return;
await mainMenu.show();

View File

@ -92,7 +92,7 @@ export class ConfigService {
`api-port=${this.config.ports.apiPort}${nl}` +
`nat="extip:${publicIp}"${nl}` +
`api-cors-origin="*"${nl}` +
`bootstrap-node=[${bootNodes}]`,
`bootstrap-node=[${bootNodes}]${nl}`,
);
};