scai
Version:
> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.
43 lines (42 loc) • 1.58 kB
JavaScript
import fsSync, { mkdirSync } from 'fs';
import { LOG_PATH, PID_PATH } from '../constants.js';
import { log } from '../utils/log.js';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
export async function startDaemon() {
// If there's a PID file, check if the process is still running
if (fsSync.existsSync(PID_PATH)) {
const pid = parseInt(fsSync.readFileSync(PID_PATH, 'utf8'));
try {
process.kill(pid, 0); // Check if process exists
log(`⚠️ Daemon already running with PID ${pid}.`);
return;
}
catch {
log(`⚠️ Stale PID file found. Removing and restarting daemon...`);
fsSync.unlinkSync(PID_PATH);
}
}
log('🚀 Starting summarizer daemon in background mode...');
log(`📝 Logs will be saved to: ${LOG_PATH}`);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const daemonWorkerPath = path.join(__dirname, '../daemon/daemonWorker.js');
const child = spawn(process.execPath, [daemonWorkerPath], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
env: {
...process.env,
BACKGROUND_MODE: 'true',
}
});
child.unref();
try {
mkdirSync(path.dirname(PID_PATH), { recursive: true });
fsSync.writeFileSync(PID_PATH, String(child.pid));
}
catch (err) {
log(`❌ Failed to write PID file: ${err instanceof Error ? err.message : err}`);
}
}