/** * GitHub Sync Daemon for ElevAIt * Runs alongside Next.js to push visitor logs to GitHub every 5 minutes */ const { pushLogToGitHub } = require('./github-db-sync.js'); const fs = require('fs'); const path = require('path'); const INITIAL_DELAY = 30000; // 30 seconds after startup const LOG_SYNC_INTERVAL = 5 * 60 * 1000; // 5 minutes for visitor logs const LOG_PREFIX = 'elevait-logs'; // Get data directory function getDataDir() { const possiblePaths = ['/data', process.env.DATA_DIR, path.join(process.cwd(), 'data')]; for (const dir of possiblePaths) { if (dir && fs.existsSync(dir)) return dir; } return '/data'; } // Track which files have been synced const syncedFiles = new Map(); // filename -> last synced size // Sync all logs to GitHub async function syncLogsToGitHub() { const dataDir = getDataDir(); const logsDir = path.join(dataDir, 'career-growth-project'); if (!fs.existsSync(logsDir)) { console.log('šŸ“‹ No logs directory found yet'); return; } // Get today's date directory const today = new Date().toISOString().split('T')[0]; const todayDir = path.join(logsDir, today); if (!fs.existsSync(todayDir)) { console.log(`šŸ“‹ No logs for today (${today}) yet`); return; } const logFiles = fs.readdirSync(todayDir).filter(f => f.endsWith('.txt')); if (logFiles.length === 0) { console.log('šŸ“‹ No log files to sync'); return; } console.log(`šŸ“‹ Found ${logFiles.length} log files to check`); for (const logFile of logFiles) { const localPath = path.join(todayDir, logFile); const stats = fs.statSync(localPath); const lastSyncedSize = syncedFiles.get(localPath) || 0; // Only sync if file has grown if (stats.size <= lastSyncedSize) { continue; } const content = fs.readFileSync(localPath, 'utf-8'); if (!content.trim()) continue; // Get new content since last sync const newContent = content.substring(lastSyncedSize); if (!newContent.trim()) continue; const githubPath = `${LOG_PREFIX}/${today}/${logFile}`; const lineCount = newContent.split('\n').filter(l => l.trim()).length; console.log(`šŸ“‹ Syncing ${lineCount} new lines from ${logFile}`); const success = await pushLogToGitHub(githubPath, content); if (success) { syncedFiles.set(localPath, stats.size); } } } async function main() { console.log('šŸ”„ ElevAIt GitHub Sync Daemon starting...'); if (!process.env.GITHUB_TOKEN) { console.log('āš ļø GITHUB_TOKEN not set - daemon will skip syncing'); console.log(' Set GITHUB_TOKEN in Hugging Face Spaces secrets to enable syncing'); } // Initial sync after delay (let app generate some logs first) console.log(`ā° Initial sync scheduled in ${INITIAL_DELAY / 1000} seconds...`); setTimeout(async () => { console.log('\nšŸ“¤ [GitHub Sync] Initial sync starting...'); await syncLogsToGitHub(); }, INITIAL_DELAY); // Log sync every 5 minutes setInterval(async () => { const timestamp = new Date().toISOString(); console.log(`\n[${timestamp}] [GitHub Sync] Syncing logs...`); await syncLogsToGitHub(); }, LOG_SYNC_INTERVAL); console.log('āœ… ElevAIt GitHub Sync Daemon running'); console.log(` └─ Log sync: every ${LOG_SYNC_INTERVAL / 60000} minutes`); console.log(` └─ GitHub repo: mahdinaser/brightmind`); console.log(` └─ Log path: ${LOG_PREFIX}/YYYY-MM-DD/log_HH-MM.txt`); // Handle shutdown gracefully process.on('SIGTERM', async () => { console.log('šŸ›‘ GitHub Sync Daemon received SIGTERM, doing final sync...'); await syncLogsToGitHub(); process.exit(0); }); process.on('SIGINT', async () => { console.log('šŸ›‘ GitHub Sync Daemon received SIGINT, doing final sync...'); await syncLogsToGitHub(); process.exit(0); }); } main().catch(console.error);