-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (89 loc) · 3.33 KB
/
Copy pathindex.js
File metadata and controls
108 lines (89 loc) · 3.33 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import express from "express"
import { createServer } from "node:http"
import { handler } from "../build/handler.js"
// * Import connection initialization functions
import { connectDatabase, disconnectDatabase } from "../src/lib/server/database/prisma.js"
import { initRedis, closeRedis } from "../src/lib/server/queue/redis.js"
const port = process.env.PORT || 3000
const app = express()
const server = createServer(app)
// * Store document worker reference for graceful shutdown
let documentWorker = null
/**
* Initialize all connections on startup
*/
async function initializeConnections() {
console.log("🔄 Initializing connections...")
try {
// * Initialize database connection with retry logic
await connectDatabase()
console.log("✅ Database connected")
// * Initialize Redis connections
await initRedis()
console.log("✅ Redis connections established")
console.log("🎉 All connections initialized successfully")
} catch (error) {
console.error("❌ Failed to initialize connections:", error.message)
// * Don't exit immediately, let the app start and retry connections
// * The reconnection strategies will handle ongoing issues
console.log("⚠️ Server starting with connection issues - will retry automatically")
}
// * Start document worker after connections are established
console.log("🔄 Starting document worker...")
try {
const { default: worker } = await import("../src/lib/server/queue/document-worker.js")
documentWorker = worker
console.log("✅ Document worker started")
} catch (error) {
console.error("❌ Failed to start document worker:", error.message)
}
// * Start meeting scheduler worker
console.log("🔄 Starting meeting scheduler worker...")
try {
const { meetingSchedulerWorker } = await import(
"../src/lib/server/queue/meeting-scheduler-worker.js"
)
console.log("✅ Meeting scheduler worker started")
} catch (error) {
console.error("❌ Failed to start meeting scheduler worker:", error.message)
}
}
/**
* Graceful shutdown handler
*/
async function gracefulShutdown(signal) {
console.log(`\n🛑 Received ${signal}, shutting down gracefully...`)
try {
// * Close server to stop accepting new connections
server.close(() => {
console.log("📡 HTTP server closed")
})
// * Close database connection
await disconnectDatabase()
console.log("🔌 Database disconnected")
// * Close Redis connections
await closeRedis()
console.log("🔌 Redis connections closed")
// * Close document worker
if (documentWorker) {
await documentWorker.close()
console.log("🔌 Document worker closed")
}
console.log("👋 Graceful shutdown completed")
process.exit(0)
} catch (error) {
console.error("❌ Error during shutdown:", error.message)
process.exit(1)
}
}
// * Setup graceful shutdown handlers
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"))
process.on("SIGINT", () => gracefulShutdown("SIGINT"))
// * SvelteKit should handle everything else using Express middleware
// * https://github.com/sveltejs/kit/tree/master/packages/adapter-node#custom-server
app.use(handler)
server.listen(port, async () => {
console.log(`🚀 Server running on port ${port}`)
// * Initialize connections after server starts
await initializeConnections()
})