request-scope-api 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,5 +28,23 @@ export default requestscopeMiddleware;
28
28
  export declare function dashboard(config?: DashboardConfig, adapter?: StorageAdapter): express.Router;
29
29
  export { setup };
30
30
  export { errorHandler };
31
+ /**
32
+ * Gracefully shuts down all RequestScope resources.
33
+ *
34
+ * This should be called by the host application during shutdown (e.g., on SIGTERM/SIGINT).
35
+ * It will:
36
+ * - Stop the retention scheduler
37
+ * - Drain the queue with a timeout
38
+ * - Destroy the queue (clear timers)
39
+ * - Close all database connections
40
+ *
41
+ * @param options - Optional configuration for shutdown
42
+ * @param options.drainTimeoutMs - Timeout for queue drain in milliseconds (default: 5000)
43
+ * @param options.logHandles - Whether to log active handles after shutdown (default: false)
44
+ */
45
+ export declare function shutdown(options?: {
46
+ drainTimeoutMs?: number;
47
+ logHandles?: boolean;
48
+ }): Promise<void>;
31
49
  export type { RequestScopeConfig, StorageConfig, AuthConfig, RequestRecord, StorageAdapter, QueryFilters, DashboardConfig, };
32
50
  export { requestscope as requestscopeMiddleware } from './middleware.js';
@@ -0,0 +1,49 @@
1
+ /**
2
+ * ShutdownManager — Manages graceful shutdown of all RequestScope resources.
3
+ *
4
+ * This centralizes shutdown logic to prevent multiple shutdown executions,
5
+ * add comprehensive logging, and expose a shutdown() method that the host
6
+ * application can call instead of relying on global signal handlers.
7
+ */
8
+ interface ShutdownResources {
9
+ queue?: {
10
+ drain: (timeoutMs: number) => Promise<void>;
11
+ destroy: () => void;
12
+ };
13
+ retentionScheduler?: {
14
+ stop: () => void;
15
+ };
16
+ }
17
+ declare class ShutdownManager {
18
+ private isShuttingDown;
19
+ private resources;
20
+ /**
21
+ * Registers resources that need to be cleaned up during shutdown.
22
+ */
23
+ registerResources(resources: ShutdownResources): void;
24
+ /**
25
+ * Performs graceful shutdown of all registered resources.
26
+ *
27
+ * Steps:
28
+ * 1. Prevents multiple shutdown executions
29
+ * 2. Stops retention scheduler
30
+ * 3. Drains queue with timeout
31
+ * 4. Destroys queue
32
+ * 5. Closes all database connections
33
+ * 6. Logs remaining active handles for debugging
34
+ */
35
+ shutdown(options?: {
36
+ drainTimeoutMs?: number;
37
+ logHandles?: boolean;
38
+ }): Promise<void>;
39
+ /**
40
+ * Logs active handles to help identify what's preventing process exit.
41
+ */
42
+ private logActiveHandles;
43
+ /**
44
+ * Returns whether shutdown is currently in progress.
45
+ */
46
+ isShutdownInProgress(): boolean;
47
+ }
48
+ export declare const shutdownManager: ShutdownManager;
49
+ export {};
@@ -98,4 +98,6 @@ export interface StorageAdapter {
98
98
  * Returns the number of deleted records.
99
99
  */
100
100
  deleteOlderThan(date: Date): Promise<number>;
101
+ /** Closes the database connection/pool. Called during application shutdown. */
102
+ close(): Promise<void>;
101
103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-scope-api",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "Zero-friction API request observability for Express.js applications",
5
5
  "keywords": [
6
6
  "express",