-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
308 lines (258 loc) · 8.57 KB
/
server.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// server.ts
// Revised to handle clearing of stall detectors (heartbeat monitor) after crawl completes
import dotenv from 'dotenv';
import { Logger } from './services/logging';
import { CrawlerManager } from './services/crawling/crawlerManager';
import { VectorSearchService } from './services/search/vectorSearchService';
import { pgVectorClient } from './db/pgvectorClient';
import { configureMiddleware } from './services/server/config';
import { healthRoutes } from './services/server/health';
import { crawlRoutes } from './services/server/crawl';
import { searchRoutes } from './services/server/search';
import { cronRoutes } from './services/server/cron';
import { CronManager } from './services/cron/cronManager';
import crypto from 'crypto';
import { handleGlobalError } from './services/errorHandling/globalErrorHandler';
import { marketCapRoutes } from './services/server/marketcap';
import { newsRoutes } from './services/server/news';
import { handleRSSFeed } from './services/rss/rssFeed';
import { commonTopicsRoutes } from './services/server/commonTopicsRoutes';
import { CommonTopicsCron } from './services/cron/commonTopicsCron';
import { chatRoutes } from './services/server/chatRoutes';
import { llmRateLimiter } from './services/middleware/rateLimiter';
import { llmRoutes } from './services/server/llmRoutes';
// HeartbeatMonitor class definition
class HeartbeatMonitor {
private lastHeartbeat: Map<string, Date> = new Map();
private timeoutMs: number;
constructor(timeoutMs: number = 15 * 60 * 1000) {
// Default 15 minutes
this.timeoutMs = timeoutMs;
}
updateHeartbeat(forumName: string) {
this.lastHeartbeat.set(forumName, new Date());
}
isStalled(forumName: string): boolean {
const lastBeat = this.lastHeartbeat.get(forumName);
return lastBeat ? Date.now() - lastBeat.getTime() > this.timeoutMs : false;
}
clear(forumName: string) {
this.lastHeartbeat.delete(forumName);
}
getAllStalled(): string[] {
return Array.from(this.lastHeartbeat.entries())
.filter(([_, lastBeat]) => Date.now() - lastBeat.getTime() > this.timeoutMs)
.map(([forumName]) => forumName);
}
}
// Server state interface
interface ServerState {
isShuttingDown: boolean;
activeConnections: Set<any>;
heartbeatMonitor: HeartbeatMonitor;
isHealthy: boolean;
}
// Initialize server state
const state: ServerState = {
isShuttingDown: false,
activeConnections: new Set(),
heartbeatMonitor: new HeartbeatMonitor(),
isHealthy: true,
};
dotenv.config();
const logger = new Logger({
logFile: 'logs/server.log',
level: 'info',
});
export const crawlerManager = new CrawlerManager(logger, state.heartbeatMonitor);
const searchService = new VectorSearchService();
const cronManager = new CronManager(crawlerManager, logger);
import { Hono } from 'hono';
const app = new Hono();
// Configure middleware
configureMiddleware(app);
// Configure routes
healthRoutes(app, crawlerManager);
crawlRoutes(app, crawlerManager, logger);
searchRoutes(app, searchService, logger);
cronRoutes(app, cronManager, logger);
marketCapRoutes(app, logger);
newsRoutes(app, logger);
app.get('/rss', async c => {
const response = await handleRSSFeed(c.req.raw);
return response;
});
// Add common topics routes
app.route('', commonTopicsRoutes);
// Add chat routes
app.route('', chatRoutes);
// Add LLM routes with rate limiting
app.use('/api/generateSimile', llmRateLimiter);
app.use('/api/generateFollowUp', llmRateLimiter);
app.route('', llmRoutes);
// Initialize cron job for common topics
const commonTopicsCron = new CommonTopicsCron();
commonTopicsCron.start();
// Add search logging middleware to search routes
// app.post('/api/search', searchLogger, searchHandler);
// app.post('/api/search/:type', searchLogger, searchByTypeHandler);
// Error handling
app.onError((err, c) => {
const errorId = crypto.randomUUID();
const timestamp = new Date().toISOString();
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
handleGlobalError(err, `Global Error Handler - Path: ${c.req.path}`);
// Rate limiting errors
if (err instanceof Error && err.message.includes('Rate limit exceeded')) {
return c.json(
{
errorId,
error: 'Too many requests',
retryAfter: 60,
timestamp,
},
429
);
}
// Database errors
if (err instanceof Error && err.message.includes('connection')) {
return c.json(
{
errorId,
error: 'Service temporarily unavailable',
timestamp,
},
503
);
}
return c.json(
{
errorId,
error: 'Internal server error',
message:
process.env.NODE_ENV === 'development' ? errorMessage : 'An unexpected error occurred',
timestamp,
},
500
);
});
// Periodic connection and stall checks
const checkConnections = async () => {
try {
await pgVectorClient.query('SELECT 1');
state.isHealthy = true;
// Check for stalled crawls
const stalledForums = state.heartbeatMonitor.getAllStalled();
for (const forum of stalledForums) {
logger.warn(`Detected stalled crawl for ${forum}`);
try {
await crawlerManager.stopCrawl(forum);
} catch (error: any) {
logger.error(`Error stopping stalled crawl for ${forum}:`, error);
}
}
} catch (error: any) {
state.isHealthy = false;
logger.error('Database connection error:', error);
}
};
setInterval(checkConnections, 30000);
// Add to the graceful shutdown function:
const gracefulShutdown = async (server: any, signal: string) => {
if (state.isShuttingDown) return;
state.isShuttingDown = true;
logger.info(`${signal} received. Starting graceful shutdown...`);
try {
await Promise.race([
(async () => {
// Stop accepting new connections
server.stop();
// Stop all cron jobs
// marketCapCronJob.stop();
// newsCronJob.stop();
// Stop all active crawlers and clear their heartbeats
const activeStatuses = crawlerManager
.getAllStatuses()
.filter(status => status.status === 'running')
.map(status => status.forumName);
for (const forum of activeStatuses) {
state.heartbeatMonitor.clear(forum);
await crawlerManager.stopCrawl(forum);
}
// Stop cron jobs
cronManager.stopScheduledCrawls();
// Close database connections
await pgVectorClient.end();
logger.info('Graceful shutdown completed');
})(),
new Promise((_, reject) => setTimeout(() => reject(new Error('Shutdown timeout')), 30000)),
]);
process.exit(0);
} catch (error: any) {
logger.error('Error during shutdown:', error);
process.exit(1);
}
};
// Port utility
const isPortInUse = async (port: number): Promise<boolean> => {
try {
const server = Bun.serve({
port,
fetch: () => new Response('test'),
});
server.stop();
// Add a short delay to ensure the port is fully released
await new Promise(resolve => setTimeout(resolve, 50));
return false;
} catch {
return true;
}
};
const findAvailablePort = async (startPort: number): Promise<number> => {
let port = startPort;
while (await isPortInUse(port)) {
port++;
}
return port;
};
// Start server
const startServer = async () => {
try {
console.log(`Selected environment: ${process.env.NODE_ENV}`);
// Initialize database connection
await pgVectorClient.connect();
logger.info('pgVectorClient connected successfully');
const preferredPort = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const port = await findAvailablePort(preferredPort);
const server = Bun.serve({
port,
fetch: app.fetch,
development: process.env.NODE_ENV === 'development',
});
logger.info(`Server listening on http://localhost:${port}`);
const shutdownHandler = (signal: string) => gracefulShutdown(server, signal);
process.on('SIGTERM', () => shutdownHandler('SIGTERM'));
process.on('SIGINT', () => shutdownHandler('SIGINT'));
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection', {
reason,
promise,
timestamp: new Date().toISOString(),
});
});
// Apply rate limiting to LLM endpoints
app.use('/api/chat*', llmRateLimiter);
app.use('/api/common-topics/*/chat', llmRateLimiter);
return server;
} catch (error: any) {
logger.error('Failed to start server:', error);
process.exit(1);
}
};
if (import.meta.main) {
startServer().catch(error => {
logger.error('Startup error:', error);
process.exit(1);
});
}
export { startServer, app };