| 9 | const SHUTDOWN_TIMEOUT_MS = 5000 |
| 10 | |
| 11 | export class ProcessManager { |
| 12 | private instances = new Map<string, AcpInstance>() |
| 13 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 14 | private processes = new Map<string, any>() |
| 15 | |
| 16 | create(group: string, command: string): AcpInstance { |
| 17 | const id = crypto.randomUUID() |
| 18 | const instance: AcpInstance = { |
| 19 | id, |
| 20 | group, |
| 21 | command, |
| 22 | status: 'running', |
| 23 | pid: undefined, |
| 24 | startTime: Date.now(), |
| 25 | exitCode: null, |
| 26 | logs: [], |
| 27 | subscribers: new Set(), |
| 28 | } |
| 29 | |
| 30 | const args = this.parseCommand(command) |
| 31 | const fullArgs = ['--group', group, ...args] |
| 32 | |
| 33 | const proc = Bun.spawn(['acp-link', ...fullArgs], { |
| 34 | stdout: 'pipe', |
| 35 | stderr: 'pipe', |
| 36 | env: { ...Bun.env, ACP_CHILD: '1' }, |
| 37 | }) |
| 38 | |
| 39 | instance.pid = proc.pid |
| 40 | this.instances.set(id, instance) |
| 41 | this.processes.set(id, proc) |
| 42 | log( |
| 43 | 'manager', |
| 44 | `created instance ${id.slice(0, 8)} group=${group} pid=${proc.pid} cmd="acp-link ${fullArgs.join(' ')}"`, |
| 45 | ) |
| 46 | |
| 47 | this.pipeStream(proc.stdout, id, 'stdout') |
| 48 | this.pipeStream(proc.stderr, id, 'stderr') |
| 49 | |
| 50 | proc.exited.then(code => { |
| 51 | instance.status = code === 0 ? 'stopped' : 'failed' |
| 52 | instance.exitCode = code |
| 53 | instance.pid = undefined |
| 54 | this.processes.delete(id) |
| 55 | log( |
| 56 | 'manager', |
| 57 | `instance ${id.slice(0, 8)} ${instance.status} exit=${code}`, |
| 58 | ) |
| 59 | this.notifyStatus(instance) |
| 60 | }) |
| 61 | |
| 62 | return instance |
| 63 | } |
| 64 | |
| 65 | stop(id: string): boolean { |
| 66 | const proc = this.processes.get(id) |
| 67 | if (!proc) return false |
| 68 | const inst = this.instances.get(id) |
nothing calls this directly
no outgoing calls
no test coverage detected