-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
186 lines (158 loc) · 7.04 KB
/
server.js
File metadata and controls
186 lines (158 loc) · 7.04 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-present Scalytics, Inc. (https://www.scalytics.io)
// ──────────────────────────────────────────────────────────────────────────────
// Shim for all `node:`-prefixed core modules (events, buffer, etc.)
// ──────────────────────────────────────────────────────────────────────────────
const Module = require('module');
const _origLoad = Module._load;
Module._load = function(request, parent, isMain) {
// if it's something like 'node:events' or 'node:buffer', strip the prefix
if (request.startsWith('node:')) {
request = request.slice(5);
}
return _origLoad(request, parent, isMain);
};
// ──────────────────────────────────────────────────────────────────────────────
const express = require('express');
const path = require('path');
const fs = require('fs');
const http = require('http');
const dotenv = require('dotenv');
const dbPath = path.join(__dirname, 'src', 'models', 'db.js');
if (!fs.existsSync(dbPath)) {
console.error('ERROR: Database module not found at %s. Deployment issue?', dbPath);
process.exit(1);
}
const { ensureSecureKeys } = require('./src/utils/securityUtils');
const { checkHuggingFaceDependencies } = require('./scripts/check_hf_dependencies');
const { loadSystemSettings } = require('./src/config/systemConfig');
// Configure llama.cpp shared library path (if exists)
const llamaBinDir = path.join(__dirname, 'bin', 'llama');
if (fs.existsSync(llamaBinDir)) {
process.env.LD_LIBRARY_PATH = process.env.LD_LIBRARY_PATH
? `${llamaBinDir}:${process.env.LD_LIBRARY_PATH}`
: llamaBinDir;
}
// Setup process-wide uncaught exception handlers
process.on('uncaughtException', (err) => {
console.error('\n=====================================');
console.error('UNCAUGHT EXCEPTION - SERVER CRASHING:', err);
fs.appendFileSync('server-crash.log', `\n[${new Date().toISOString()}] UNCAUGHT EXCEPTION:\n${err.stack}\n`);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('\n=====================================');
console.error('UNHANDLED PROMISE REJECTION:', reason);
fs.appendFileSync('server-crash.log', `\n[${new Date().toISOString()}] UNHANDLED REJECTION:\n${reason}\n`);
});
// Load .env file but DO NOT override existing process.env variables (like those set by PM2)
dotenv.config({ override: false });
try {
ensureSecureKeys();
const app = express();
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
// Set long timeouts to prevent premature socket closure for long-running SSE streams
server.timeout = 600000; // 10 minutes
server.headersTimeout = 600000; // 10 minutes
const { initializeSocket } = require('./src/config/socket');
const wsServer = initializeSocket(server);
const { setupEventBusBridge } = require('./src/config/eventBusBridge');
setupEventBusBridge(wsServer);
app.set('wsServer', wsServer);
const { setupMiddleware, getFileUploadMiddleware } = require('./src/config/middleware');
const { protect } = require('./src/middleware/authMiddleware');
setupMiddleware(app);
const { setupRoutes } = require('./src/config/routes');
setupRoutes(app, {
fileUpload: getFileUploadMiddleware(),
protect: protect
});
app.use((err, req, res, next) => {
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
console.error('Malformed JSON received:', err.message);
return res.status(400).json({ error: 'Malformed JSON payload.' });
}
if (err.type === 'entity.too.large' || (err.name === 'PayloadTooLargeError' || err.status === 413)) {
console.warn("PayloadTooLargeError encountered for %s %s: %s", String(req.method).replace(/\n|\r/g, ''), String(req.path).replace(/\n|\r/g, ''), err.message);
const limit = err.limit ? `${(err.limit / 1024 / 1024).toFixed(2)} MB` : 'the configured limit';
return res.status(413).json({
error: 'Request payload is too large.',
message: `The request body exceeds ${limit}.`
});
}
console.error('Unhandled Express error:', err);
if (!res.headersSent) {
res.status(err.status || 500).json({ error: 'An unexpected server error occurred.' });
}
});
async function startServer() {
try {
const { initializeServer } = require('./src/config/database');
await initializeServer();
await loadSystemSettings();
if (process.env.SKIP_HF_CHECK !== 'true') {
try {
await checkHuggingFaceDependencies();
} catch (hfError) {
console.warn('Warning: Hugging Face dependency check failed:', hfError.message);
}
}
if (process.env.SKIP_VLLM_SERVICE !== 'true') {
try {
const vllmService = require('./src/services/vllmService');
if (typeof vllmService.initialize === 'function') {
await vllmService.initialize();
}
} catch (vllmError) {
console.warn('Warning: vLLM Service initialization failed:', vllmError.message);
}
}
server.listen(PORT, () => {
});
} catch (error) {
console.error('Failed to start server:', error);
fs.appendFileSync('server-crash.log', `\n[${new Date().toISOString()}] STARTUP ERROR:\n${error.stack}\n`);
process.exit(1);
}
}
startServer();
const shutdown = (signal) => {
server.close(async () => {
try {
if (process.env.SKIP_VLLM_SERVICE !== 'true') {
const vllmService = require('./src/services/vllmService');
if (vllmService && typeof vllmService.shutdown === 'function') {
await vllmService.shutdown();
}
}
const { db } = require('./src/models/db');
if (db && typeof db.close === 'function') {
await new Promise((resolve, reject) => {
db.close((err) => {
if (err) {
console.error('Error closing database:', err.message);
reject(err);
} else {
resolve();
}
});
});
}
} catch (dbCloseError) {
console.error('Error accessing/closing database during shutdown:', dbCloseError);
}
process.exit(0);
});
setTimeout(() => {
console.error('Graceful shutdown timed out. Forcing exit.');
process.exit(1);
}, 10000);
};
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));
} catch (initError) {
console.error('Error during server initialization:', initError);
fs.appendFileSync('server-crash.log', `\n[${new Date().toISOString()}] INITIALIZATION ERROR:\n${initError.stack}\n`);
process.exit(1);
}