-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
42 lines (34 loc) · 1.45 KB
/
instrumentation.ts
File metadata and controls
42 lines (34 loc) · 1.45 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
// Track if cleanup listeners are registered to prevent duplicates
let cleanupListenersRegistered = false;
// Simple console logger for instrumentation phase (before pino is available)
function log(level: 'info' | 'warn' | 'error', message: string) {
const timestamp = new Date().toISOString();
console.log(JSON.stringify({ level, time: timestamp, msg: message }));
}
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
// Calibre automatic sync works in both dev and production
const { calibreWatcher } = await import("./lib/calibre-watcher");
const { syncCalibreLibrary } = await import("./lib/sync-service");
const CALIBRE_DB_PATH = process.env.CALIBRE_DB_PATH;
if (CALIBRE_DB_PATH) {
log('info', 'Initializing Calibre automatic sync...');
// Start watching the Calibre database for changes
await calibreWatcher.start(CALIBRE_DB_PATH, syncCalibreLibrary);
} else {
log('warn', "CALIBRE_DB_PATH not configured. Automatic sync is disabled.");
}
// Cleanup on shutdown - only register once to prevent memory leak warnings
if (!cleanupListenersRegistered) {
process.on("SIGTERM", () => {
log('info', "Shutting down Calibre watcher...");
calibreWatcher.stop();
});
process.on("SIGINT", () => {
log('info', "Shutting down Calibre watcher...");
calibreWatcher.stop();
});
cleanupListenersRegistered = true;
}
}
}