-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
60 lines (48 loc) · 2.15 KB
/
server.js
File metadata and controls
60 lines (48 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const env = require("./src/config/env.config")
const dns = require("dns")
// Force IPv4 resolution to prevent connection timeouts with cloud databases in Node 17+
dns.setDefaultResultOrder('ipv4first')
// Prevent ioredis 'Connection is closed' from crashing the server when Redis
// is unavailable. After the retry limit is reached, ioredis flushes its command
// queue with a thrown error. We catch it here so the rest of the app keeps running.
process.on('uncaughtException', (err) => {
if (err.message && err.message.includes('Connection is closed')) {
console.warn('[Redis] Connection closed after max retries. Redis-dependent features (queues, sockets) will be unavailable until Redis comes back online.');
return; // Suppress — do not crash
}
// Re-throw anything else (real unhandled errors should still crash)
console.error('[Server] Uncaught Exception:', err);
process.exit(1);
});
process.on('unhandledRejection', (reason) => {
if (reason && reason.message && reason.message.includes('Connection is closed')) {
console.warn('[Redis] Unhandled rejection: Redis connection closed.');
return;
}
console.error('[Server] Unhandled Rejection:', reason);
});
const { verifyConnection } = require("./src/db")
const app = require("./src/app")
const PORT = env.PORT || 3000
async function startServer() {
try {
await verifyConnection()
// Start background scheduler
const SchedulerService = require("./src/services/scheduler.service");
SchedulerService.start();
// Start Queue workers
const QueueService = require("./src/services/queue.service");
QueueService.startWorkers();
const SocketService = require("./src/services/socket.service");
const http = require("http");
const server = http.createServer(app);
SocketService.init(server);
server.listen(PORT, "0.0.0.0", () => {
console.log(`Server started. Running on http://0.0.0.0:${PORT}`)
})
} catch (error) {
console.error(`Server failed to start:`, error.message)
process.exit(1)
}
}
startServer()