kaelum 1.5.0 → 1.6.0
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.
- package/core/setConfig.js +20 -0
- package/core/shutdown.js +167 -0
- package/core/start.js +13 -0
- package/createApp.js +14 -0
- package/index.d.ts +15 -0
- package/package.json +2 -2
package/core/setConfig.js
CHANGED
|
@@ -257,6 +257,26 @@ function setConfig(app, options = {}) {
|
|
|
257
257
|
}
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
+
// --- Graceful Shutdown ---
|
|
261
|
+
if (options.hasOwnProperty("gracefulShutdown")) {
|
|
262
|
+
const server = app.locals && app.locals._kaelum_server;
|
|
263
|
+
if (server && server.listening) {
|
|
264
|
+
const {
|
|
265
|
+
enableGracefulShutdown,
|
|
266
|
+
removeSignalHandlers,
|
|
267
|
+
} = require("./shutdown");
|
|
268
|
+
if (options.gracefulShutdown === false) {
|
|
269
|
+
removeSignalHandlers(app);
|
|
270
|
+
} else {
|
|
271
|
+
const shutdownOpts =
|
|
272
|
+
typeof options.gracefulShutdown === "object"
|
|
273
|
+
? options.gracefulShutdown
|
|
274
|
+
: {};
|
|
275
|
+
enableGracefulShutdown(app, shutdownOpts);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
260
280
|
// Return the full merged config for convenience
|
|
261
281
|
return app.locals.kaelumConfig;
|
|
262
282
|
}
|
package/core/shutdown.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// core/shutdown.js
|
|
2
|
+
// Kaelum graceful shutdown module.
|
|
3
|
+
// Handles process signal interception, connection draining, and cleanup hook execution.
|
|
4
|
+
|
|
5
|
+
const DEFAULT_TIMEOUT = 10000;
|
|
6
|
+
const DEFAULT_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Register a cleanup function to run during shutdown.
|
|
10
|
+
*/
|
|
11
|
+
function onShutdown(app, fn) {
|
|
12
|
+
if (!app) throw new Error("onShutdown requires an app instance");
|
|
13
|
+
if (typeof fn !== "function") {
|
|
14
|
+
throw new Error("onShutdown: expected a function, got " + typeof fn);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!Array.isArray(app.locals._kaelum_shutdown_hooks)) {
|
|
18
|
+
app.locals._kaelum_shutdown_hooks = [];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
app.locals._kaelum_shutdown_hooks.push(fn);
|
|
22
|
+
return app;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Run all registered cleanup hooks in order.
|
|
27
|
+
* Errors in individual hooks are caught and logged but do not abort the sequence.
|
|
28
|
+
*/
|
|
29
|
+
async function runCleanupHooks(app) {
|
|
30
|
+
const hooks = (app.locals && app.locals._kaelum_shutdown_hooks) || [];
|
|
31
|
+
for (const fn of hooks) {
|
|
32
|
+
try {
|
|
33
|
+
await Promise.resolve(fn());
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.error(
|
|
36
|
+
"Kaelum shutdown: cleanup hook error:",
|
|
37
|
+
err && err.message ? err.message : err
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Gracefully close the server and run cleanup hooks.
|
|
45
|
+
*
|
|
46
|
+
* If callback is provided: calls cb(err) and returns app.
|
|
47
|
+
* If no callback: returns a Promise<void>.
|
|
48
|
+
*/
|
|
49
|
+
function close(app, cb) {
|
|
50
|
+
if (!app) throw new Error("close requires an app instance");
|
|
51
|
+
|
|
52
|
+
// Prevent double-shutdown
|
|
53
|
+
if (app.locals._kaelum_shutdown_in_progress) {
|
|
54
|
+
if (typeof cb === "function") {
|
|
55
|
+
process.nextTick(() => cb());
|
|
56
|
+
return app;
|
|
57
|
+
}
|
|
58
|
+
return Promise.resolve();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
app.locals._kaelum_shutdown_in_progress = true;
|
|
62
|
+
|
|
63
|
+
const timeout = app.locals._kaelum_shutdown_timeout || DEFAULT_TIMEOUT;
|
|
64
|
+
const server = app.locals._kaelum_server;
|
|
65
|
+
|
|
66
|
+
// Remove signal handlers to prevent re-entry
|
|
67
|
+
removeSignalHandlers(app);
|
|
68
|
+
|
|
69
|
+
const doShutdown = async () => {
|
|
70
|
+
try {
|
|
71
|
+
// Phase 1: close the HTTP server (drain existing connections)
|
|
72
|
+
if (server && typeof server.close === "function") {
|
|
73
|
+
await new Promise((resolve) => {
|
|
74
|
+
const timer = setTimeout(() => {
|
|
75
|
+
console.error(
|
|
76
|
+
"Kaelum shutdown: server close timed out after " + timeout + "ms"
|
|
77
|
+
);
|
|
78
|
+
resolve();
|
|
79
|
+
}, timeout);
|
|
80
|
+
if (timer.unref) timer.unref();
|
|
81
|
+
|
|
82
|
+
server.close((err) => {
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
if (err) {
|
|
85
|
+
console.error(
|
|
86
|
+
"Kaelum shutdown: server close error:",
|
|
87
|
+
err.message || err
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
resolve();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Phase 2: run cleanup hooks (always runs, even after timeout)
|
|
96
|
+
await runCleanupHooks(app);
|
|
97
|
+
} finally {
|
|
98
|
+
app.locals._kaelum_shutdown_in_progress = false;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (typeof cb === "function") {
|
|
103
|
+
doShutdown()
|
|
104
|
+
.then(() => cb(null))
|
|
105
|
+
.catch((err) => cb(err));
|
|
106
|
+
return app;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return doShutdown();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Register process signal handlers for automatic graceful shutdown.
|
|
114
|
+
* Called internally from start.js after server creation.
|
|
115
|
+
*/
|
|
116
|
+
function enableGracefulShutdown(app, options) {
|
|
117
|
+
if (!app) return;
|
|
118
|
+
|
|
119
|
+
const cfg = typeof options === "object" && options !== null ? options : {};
|
|
120
|
+
const timeout = cfg.timeout || DEFAULT_TIMEOUT;
|
|
121
|
+
const signals = Array.isArray(cfg.signals) ? cfg.signals : DEFAULT_SIGNALS;
|
|
122
|
+
|
|
123
|
+
app.locals._kaelum_shutdown_timeout = timeout;
|
|
124
|
+
|
|
125
|
+
// Remove existing handlers before registering new ones
|
|
126
|
+
removeSignalHandlers(app);
|
|
127
|
+
|
|
128
|
+
const handlers = {};
|
|
129
|
+
|
|
130
|
+
for (const signal of signals) {
|
|
131
|
+
const handler = () => {
|
|
132
|
+
console.log(
|
|
133
|
+
"\nKaelum: received " + signal + ", starting graceful shutdown..."
|
|
134
|
+
);
|
|
135
|
+
close(app)
|
|
136
|
+
.then(() => process.exit(0))
|
|
137
|
+
.catch((err) => {
|
|
138
|
+
console.error("Kaelum shutdown error:", err.message || err);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
handlers[signal] = handler;
|
|
143
|
+
process.on(signal, handler);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
app.locals._kaelum_shutdown_handlers = handlers;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Remove signal handlers previously installed by enableGracefulShutdown.
|
|
151
|
+
*/
|
|
152
|
+
function removeSignalHandlers(app) {
|
|
153
|
+
const handlers = app.locals && app.locals._kaelum_shutdown_handlers;
|
|
154
|
+
if (handlers && typeof handlers === "object") {
|
|
155
|
+
for (const signal of Object.keys(handlers)) {
|
|
156
|
+
process.removeListener(signal, handlers[signal]);
|
|
157
|
+
}
|
|
158
|
+
app.locals._kaelum_shutdown_handlers = null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
module.exports = {
|
|
163
|
+
onShutdown,
|
|
164
|
+
close,
|
|
165
|
+
enableGracefulShutdown,
|
|
166
|
+
removeSignalHandlers,
|
|
167
|
+
};
|
package/core/start.js
CHANGED
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
// - If a server is already running, returns the existing server (no double-listen)
|
|
6
6
|
// - Stores the server instance in app.locals._kaelum_server
|
|
7
7
|
// - Attaches basic error logging for startup errors
|
|
8
|
+
// - Enables graceful shutdown by default (unless config disables it)
|
|
9
|
+
|
|
10
|
+
const { enableGracefulShutdown } = require("./shutdown");
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Normalize and validate a port-like value.
|
|
@@ -109,6 +112,16 @@ function start(app, port, cb) {
|
|
|
109
112
|
// persist reference so we can check or close later
|
|
110
113
|
app.locals._kaelum_server = server;
|
|
111
114
|
|
|
115
|
+
// enable graceful shutdown by default unless explicitly disabled
|
|
116
|
+
const shutdownCfg = cfg.gracefulShutdown;
|
|
117
|
+
if (shutdownCfg !== false) {
|
|
118
|
+
const shutdownOpts =
|
|
119
|
+
typeof shutdownCfg === "object" && shutdownCfg !== null
|
|
120
|
+
? shutdownCfg
|
|
121
|
+
: {};
|
|
122
|
+
enableGracefulShutdown(app, shutdownOpts);
|
|
123
|
+
}
|
|
124
|
+
|
|
112
125
|
return server;
|
|
113
126
|
}
|
|
114
127
|
|
package/createApp.js
CHANGED
|
@@ -20,6 +20,7 @@ const registerHealth = require("./core/healthCheck");
|
|
|
20
20
|
const redirect = require("./core/redirect");
|
|
21
21
|
const { removeMiddlewareByFn } = require("./core/utils");
|
|
22
22
|
const { registerPlugin, getPlugins } = require("./core/plugin");
|
|
23
|
+
const { onShutdown, close } = require("./core/shutdown");
|
|
23
24
|
|
|
24
25
|
function createApp() {
|
|
25
26
|
const app = express();
|
|
@@ -31,6 +32,8 @@ function createApp() {
|
|
|
31
32
|
app.locals = app.locals || {};
|
|
32
33
|
app.locals.kaelumConfig = app.locals.kaelumConfig || {};
|
|
33
34
|
app.locals._kaelum_plugins = [];
|
|
35
|
+
app.locals._kaelum_shutdown_hooks = [];
|
|
36
|
+
app.locals._kaelum_shutdown_in_progress = false;
|
|
34
37
|
// persist baseline config so app.get("kaelum:config") is always available
|
|
35
38
|
app.set("kaelum:config", app.locals.kaelumConfig);
|
|
36
39
|
|
|
@@ -182,6 +185,17 @@ function createApp() {
|
|
|
182
185
|
return getPlugins(app);
|
|
183
186
|
};
|
|
184
187
|
|
|
188
|
+
// ---------------------------
|
|
189
|
+
// Graceful shutdown
|
|
190
|
+
// ---------------------------
|
|
191
|
+
app.onShutdown = function (fn) {
|
|
192
|
+
return onShutdown(app, fn);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
app.close = function (cb) {
|
|
196
|
+
return close(app, cb);
|
|
197
|
+
};
|
|
198
|
+
|
|
185
199
|
return app;
|
|
186
200
|
}
|
|
187
201
|
|
package/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ interface KaelumConfig {
|
|
|
10
10
|
port?: number;
|
|
11
11
|
views?: { engine?: string; path?: string };
|
|
12
12
|
logger?: boolean | false;
|
|
13
|
+
gracefulShutdown?: boolean | GracefulShutdownConfig;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
interface HealthOptions {
|
|
@@ -32,6 +33,13 @@ interface ErrorHandlerOptions {
|
|
|
32
33
|
onError?: (err: Error, req: any, res: any) => void;
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
interface GracefulShutdownConfig {
|
|
37
|
+
/** Timeout in milliseconds before forcing shutdown (default: 10000) */
|
|
38
|
+
timeout?: number;
|
|
39
|
+
/** Process signals to handle (default: ["SIGTERM", "SIGINT"]) */
|
|
40
|
+
signals?: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
interface RedirectEntry {
|
|
36
44
|
path: string;
|
|
37
45
|
to: string;
|
|
@@ -102,6 +110,13 @@ interface KaelumApp extends Express {
|
|
|
102
110
|
|
|
103
111
|
/** List registered plugin names */
|
|
104
112
|
getPlugins(): string[];
|
|
113
|
+
|
|
114
|
+
/** Gracefully close the server and run cleanup hooks */
|
|
115
|
+
close(): Promise<void>;
|
|
116
|
+
close(cb: (err?: Error | null) => void): KaelumApp;
|
|
117
|
+
|
|
118
|
+
/** Register a cleanup function to run during graceful shutdown */
|
|
119
|
+
onShutdown(fn: () => void | Promise<void>): KaelumApp;
|
|
105
120
|
}
|
|
106
121
|
|
|
107
122
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kaelum",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "A minimalist Node.js framework for building web pages and APIs with simplicity and speed.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"repository": {
|
|
40
40
|
"type": "git",
|
|
41
|
-
"url": "https://github.com/kaelumjs/kaelum.git"
|
|
41
|
+
"url": "git+https://github.com/kaelumjs/kaelum.git"
|
|
42
42
|
},
|
|
43
43
|
"bugs": {
|
|
44
44
|
"url": "https://github.com/kaelumjs/kaelum/issues"
|