ElevAIt / scripts /github-sync-daemon.js
Mahdi Naser Moghadasi
Claude Opus 4.5
Add GitHub sync daemon to push logs to mahdinaser/brightmind repo
96fdde4
Raw History Blame Contribute Delete
3.87 kB
/**
* 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);