-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
61 lines (50 loc) · 1.91 KB
/
instrumentation.ts
File metadata and controls
61 lines (50 loc) · 1.91 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
/**
* Next.js Instrumentation
*
* This file is used to initialize server-side instrumentation like Sentry.
* It runs before any server-side code in Next.js.
*
* The background job worker runs as a separate process (see scripts/worker.ts).
*
* https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation
*/
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
// Import the server Sentry config
await import("./sentry.server.config");
// Initialize plugins (happens at module import time)
await import("./src/server/plugins");
const { logger } = await import("./src/lib/logger");
// Start internal metrics server for Prometheus scraping on port 9091
// Worker uses 9092, Discord bot uses 9093 (not exposed via Fly.io http_service)
const { startMetricsServer, stopMetricsServer } = await import("./src/server/metrics/server");
startMetricsServer(9091);
// Handle graceful shutdown to close DB/Redis connections
let shuttingDown = false;
const shutdown = async () => {
if (shuttingDown) return; // Prevent duplicate shutdown attempts
shuttingDown = true;
logger.info("Shutting down Next.js server...");
try {
// Stop metrics server
await stopMetricsServer();
// Close Redis connection
const { redis } = await import("./src/server/redis");
await redis.quit();
// Close database pool
const { pool } = await import("./src/server/db");
await pool.end();
logger.info("Graceful shutdown complete");
} catch (error) {
logger.error("Error during shutdown", { error });
}
process.exit(0);
};
process.on("SIGTERM", () => void shutdown());
process.on("SIGINT", () => void shutdown());
}
if (process.env.NEXT_RUNTIME === "edge") {
// Import the edge Sentry config
await import("./sentry.edge.config");
}
}