(options)
| 571 | // --- Lifecycle --- |
| 572 | |
| 573 | function start(options) { |
| 574 | var delayMs = (options && options.delayMs) || 0; |
| 575 | var pids = getRunningPids(); |
| 576 | var ownedPids = getOwnedLoopPids(pids); |
| 577 | if (ownedPids.length > 0) { |
| 578 | if (shouldRestartForProxy(ownedPids, process.env)) { |
| 579 | console.log('[Lifecycle] Loop running but proxy is unhealthy; restarting.'); |
| 580 | stopPids(ownedPids, { unlinkPidFile: true, unlinkLock: true }); |
| 581 | ownedPids = getOwnedLoopPids(getRunningPids()); |
| 582 | } |
| 583 | } |
| 584 | if (ownedPids.length > 0) { |
| 585 | console.log('[Lifecycle] Already running (PIDs: ' + ownedPids.join(', ') + ').'); |
| 586 | return { status: 'already_running', pids: ownedPids }; |
| 587 | } |
| 588 | if (delayMs > 0) { |
| 589 | sleepMs(delayMs); |
| 590 | } |
| 591 | |
| 592 | var script = getLoopScript(); |
| 593 | console.log('[Lifecycle] Starting: node ' + path.relative(WORKSPACE_ROOT, script) + ' --loop'); |
| 594 | |
| 595 | var out = fs.openSync(LOG_FILE, 'a'); |
| 596 | var err = fs.openSync(LOG_FILE, 'a'); |
| 597 | |
| 598 | var env = prepareStartEnv(process.env); |
| 599 | // .npm-global/bin is a Unix-only convention; skip the PATH injection on Windows |
| 600 | // to avoid polluting the environment with a path that does not exist. |
| 601 | if (process.platform !== 'win32') { |
| 602 | var npmGlobal = path.join(os.homedir(), '.npm-global', 'bin'); |
| 603 | if (env.PATH && !env.PATH.includes(npmGlobal)) { |
| 604 | env.PATH = npmGlobal + ':' + env.PATH; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | var child = spawn(process.execPath, [script, '--loop'], { |
| 609 | detached: true, stdio: ['ignore', out, err], cwd: WORKSPACE_ROOT, env: env, windowsHide: true |
| 610 | }); |
| 611 | child.unref(); |
| 612 | fs.writeFileSync(PID_FILE, String(child.pid)); |
| 613 | console.log('[Lifecycle] Started PID ' + child.pid); |
| 614 | return { status: 'started', pid: child.pid }; |
| 615 | } |
| 616 | |
| 617 | function stop() { |
| 618 | var pids = getRunningPids(); |
no test coverage detected