mindcraft/src/process/agent-process.js

46 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-12-08 16:18:20 -06:00
import { spawn } from 'child_process';
2023-12-10 20:18:20 -06:00
export class AgentProcess {
static runningCount = 0;
start(profile, load_memory=false, init_message=null, count_id=0) {
2023-12-21 15:11:38 -07:00
let args = ['src/process/init-agent.js', this.name];
args.push('-p', profile);
args.push('-c', count_id);
if (load_memory)
args.push('-l', load_memory);
2023-12-21 20:42:01 -07:00
if (init_message)
args.push('-m', init_message);
2023-12-09 21:40:53 -06:00
2023-12-08 16:18:20 -06:00
const agentProcess = spawn('node', args, {
stdio: 'inherit',
stderr: 'inherit',
});
AgentProcess.runningCount++;
2023-12-09 21:40:53 -06:00
let last_restart = Date.now();
2023-12-08 16:18:20 -06:00
agentProcess.on('exit', (code, signal) => {
console.log(`Agent process exited with code ${code} and signal ${signal}`);
if (code !== 0) {
2023-12-20 20:41:15 -08:00
// agent must run for at least 10 seconds before restarting
if (Date.now() - last_restart < 10000) {
console.error(`Agent process ${profile} exited too quickly and will not be restarted.`);
AgentProcess.runningCount--;
if (AgentProcess.runningCount <= 0) {
console.error('All agent processes have ended. Exiting.');
process.exit(0);
}
return;
2023-12-09 21:40:53 -06:00
}
2023-12-08 16:18:20 -06:00
console.log('Restarting agent...');
this.start(profile, true, 'Agent process restarted.', count_id);
2023-12-09 21:40:53 -06:00
last_restart = Date.now();
2023-12-08 16:18:20 -06:00
}
});
agentProcess.on('error', (err) => {
console.error('Failed to start agent process:', err);
});
}
2023-12-09 21:40:53 -06:00
}