- Add in-process scheduler service triggered by ENABLE_SCHEDULER - Record runs in new matching_runs table; throttle per-event and log stats - Add admin endpoints: POST /api/admin/events/:slug/run-now and GET /api/admin/events/:slug/matching-runs - Wire scheduler start/stop in server and add ENV flags + compose defaults - Prisma schema: add MatchingRun model and relation - Update env examples for scheduler configuration
62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
require('dotenv').config();
|
|
const http = require('http');
|
|
const app = require('./app');
|
|
const { testConnection, disconnect } = require('./utils/db');
|
|
const { initializeSocket } = require('./socket');
|
|
const scheduler = require('./services/scheduler');
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
async function startServer() {
|
|
// Test database connection
|
|
await testConnection();
|
|
|
|
// Create HTTP server
|
|
const server = http.createServer(app);
|
|
|
|
// Initialize Socket.IO
|
|
initializeSocket(server);
|
|
|
|
server.listen(PORT, '0.0.0.0', () => {
|
|
console.log('=================================');
|
|
console.log('🚀 spotlight.cam Backend Started');
|
|
console.log('=================================');
|
|
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
|
|
console.log(`Server running on port: ${PORT}`);
|
|
console.log(`Health check: http://localhost:${PORT}/api/health`);
|
|
console.log('=================================');
|
|
if (process.env.ENABLE_SCHEDULER === 'true') {
|
|
try {
|
|
scheduler.start();
|
|
console.log('[*] Scheduler started');
|
|
} catch (e) {
|
|
console.error('Failed to start scheduler:', e?.message || e);
|
|
}
|
|
} else {
|
|
console.log('[*] Scheduler disabled (ENABLE_SCHEDULER != "true")');
|
|
}
|
|
});
|
|
|
|
return server;
|
|
}
|
|
|
|
startServer().catch((err) => {
|
|
console.error('Failed to start server:', err);
|
|
process.exit(1);
|
|
});
|
|
|
|
// Graceful shutdown
|
|
process.on('SIGTERM', async () => {
|
|
console.log('SIGTERM received, shutting down gracefully...');
|
|
try { scheduler.stop(); } catch (_) {}
|
|
await disconnect();
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
console.log('SIGINT received, shutting down gracefully...');
|
|
try { scheduler.stop(); } catch (_) {}
|
|
await disconnect();
|
|
process.exit(0);
|
|
});
|