@codemafia0000/d0
Version:
Claude Multi-Agent Automated Development AI - Revolutionary development environment where multiple AI agents collaborate to automate software development
99 lines (80 loc) โข 2.66 kB
JavaScript
const fs = require('fs');
const path = require('path');
const MessageDaemon = require('./message-daemon');
// Get project root from command line argument
const projectRoot = process.argv[2] || process.cwd();
// Change working directory to project root
process.chdir(projectRoot);
// Now import constants with correct project root
const CONSTANTS = require('../constants');
class BackgroundDaemon {
constructor() {
this.messageDaemon = new MessageDaemon();
this.isRunning = false;
}
start() {
console.log(`๐ Starting background daemon for project: ${projectRoot}`);
// Write PID file
try {
const pidFile = CONSTANTS.getTmpPath('daemon.pid');
if (!fs.existsSync(CONSTANTS.TMP_DIR)) {
fs.mkdirSync(CONSTANTS.TMP_DIR, { recursive: true });
}
fs.writeFileSync(pidFile, process.pid.toString());
console.log(`๐ PID ${process.pid} written to ${pidFile}`);
} catch (error) {
console.error('Failed to write PID file:', error.message);
}
// Start message daemon
this.messageDaemon.start();
this.isRunning = true;
console.log('โ
Background daemon started successfully');
console.log('๐ค Message auto-processing is now active');
// Keep the process alive
process.on('SIGINT', () => this.shutdown());
process.on('SIGTERM', () => this.shutdown());
process.on('exit', () => this.cleanup());
// Periodic health check
setInterval(() => {
this.healthCheck();
}, 30000); // Every 30 seconds
}
healthCheck() {
// Check if sessions file still exists
if (!fs.existsSync(CONSTANTS.SESSIONS_FILE)) {
console.log('โ ๏ธ Sessions file not found, shutting down daemon');
this.shutdown();
return;
}
// Log status periodically (optional)
const now = new Date().toISOString();
console.log(`๐ Daemon health check at ${now} - Status: Running`);
}
shutdown() {
console.log('๐ Shutting down background daemon...');
if (this.messageDaemon) {
this.messageDaemon.stop();
}
this.cleanup();
process.exit(0);
}
cleanup() {
try {
// Remove PID file
const pidFile = CONSTANTS.getTmpPath('daemon.pid');
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile);
console.log('๐งน Cleaned up PID file');
}
} catch (error) {
console.error('Error during cleanup:', error.message);
}
}
}
// Start the daemon if this file is run directly
if (require.main === module) {
const daemon = new BackgroundDaemon();
daemon.start();
}
module.exports = BackgroundDaemon;