Spaces:
Paused
Paused
Mahdi Naser Moghadasi
Claude Opus 4.5
Add GitHub sync daemon to push logs to mahdinaser/brightmind repo
96fdde4 Download scripts/github-sync-daemon.js from brightmind-ai/ElevAIt: direct link, hf CLI and curl.
- Browser
- Download file 3.87 kB
-
https://huggingface.co/spaces/brightmind-ai/ElevAIt/resolve/main/scripts/github-sync-daemon.js
- Command line
-
hf download hf://spaces/brightmind-ai/ElevAIt/scripts/github-sync-daemon.js
-
curl -L -o github-sync-daemon.js https://huggingface.co/spaces/brightmind-ai/ElevAIt/resolve/main/scripts/github-sync-daemon.js
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); | |