Enhance worker management and logging in summary-hook.js and worker-utils.ts

- Refactored logging functionality in summary-hook.js to improve clarity and consistency.
- Added checks in worker-utils.ts to prevent unnecessary restarts of the worker service, ensuring it only starts if not already running.
This commit is contained in:
Alex Newman
2025-11-04 14:40:34 -05:00
parent c03457d2d4
commit c8a206b682
5 changed files with 84 additions and 65 deletions
+23 -4
View File
@@ -6,17 +6,36 @@ const FIXED_PORT = parseInt(process.env.CLAUDE_MEM_WORKER_PORT || '37777', 10);
/**
* Ensure worker service is running
* Just starts PM2 - no health checks, no retries, no delays
* PM2 handles the rest (already running = no-op, failures = exit code)
* Checks if worker is already running before attempting to start
* This prevents unnecessary restarts that could interrupt mid-action processing
*/
export function ensureWorkerRunning(): void {
const packageRoot = getPackageRoot();
const pm2Path = path.join(packageRoot, 'node_modules', '.bin', 'pm2');
const ecosystemPath = path.join(packageRoot, 'ecosystem.config.cjs');
spawn(pm2Path, ['start', ecosystemPath], {
// Check if worker is already running
const checkProcess = spawn(pm2Path, ['list', '--no-color'], {
cwd: packageRoot,
stdio: 'inherit'
stdio: ['ignore', 'pipe', 'ignore']
});
let output = '';
checkProcess.stdout?.on('data', (data) => {
output += data.toString();
});
checkProcess.on('close', (code) => {
// Check if 'claude-mem-worker' is in the PM2 list output and is 'online'
const isRunning = output.includes('claude-mem-worker') && output.includes('online');
if (!isRunning) {
// Only start if not already running
spawn(pm2Path, ['start', ecosystemPath], {
cwd: packageRoot,
stdio: 'ignore'
});
}
});
}