Make admin shutdown trigger restart-friendly exit
@@ -2,7 +2,7 @@
2
2
3
3
## server/index.js
4
4
- start() - Initialises DB/keys, loads config, starts HTTP server (config.port) and optional HTTPS server (config.httpsPort + config.tlsCert + config.tlsKey). Both share the same Express app.
5
- shutdown(...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, force-exits after 5 s.
5
- shutdown(opts, ...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, and exits with configurable code after drain/timeout.
6
6
7
7
## server/db.js
8
8
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).
@@ -2,7 +2,7 @@2
2
3
## server/index.js3
## server/index.js4
- start() - Initialises DB/keys, loads config, starts HTTP server (config.port) and optional HTTPS server (config.httpsPort + config.tlsCert + config.tlsKey). Both share the same Express app.4
- start() - Initialises DB/keys, loads config, starts HTTP server (config.port) and optional HTTPS server (config.httpsPort + config.tlsCert + config.tlsKey). Both share the same Express app.5
- shutdown(...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, force-exits after 5 s.5
- shutdown(opts, ...servers) - Graceful teardown: closes DB, closes all provided http/https Server instances, and exits with configurable code after drain/timeout.6
6
7
## server/db.js7
## server/db.js8
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).8
- initDb() - Initializes SQLite schema for users, file metadata, and auth_sessions (idle timeout/revocation support).@@ -12,6 +12,7 @@
12
12
- `DELETE /api/admin/users/:id`
13
13
- `GET /api/admin/config`
14
14
- `PUT /api/admin/config`
15
- `POST /api/admin/shutdown`
15
16
16
17
---
17
18
@@ -12,6 +12,7 @@12
- `DELETE /api/admin/users/:id`12
- `DELETE /api/admin/users/:id`13
- `GET /api/admin/config`13
- `GET /api/admin/config`14
- `PUT /api/admin/config`14
- `PUT /api/admin/config`15
- `POST /api/admin/shutdown`15
16
16
---17
---17
18
@@ -28,8 +28,9 @@ app.get('*', (req, res) =>
28
28
});
29
29
30
30
// Exposed so the shutdown route can trigger a graceful stop.
31
function shutdown (...servers)
31
function shutdown (opts = {}, ...servers)
32
32
{
33
const exitCode = Number.isInteger(opts.exitCode) ? opts.exitCode : 0;
33
34
const active = servers.filter(Boolean);
34
35
if (active.length === 0 || active[0].__isShuttingDown) return;
35
36
active.forEach(s => { s.__isShuttingDown = true; });
@@ -40,7 +41,7 @@ function shutdown (...servers)
40
41
let closed = 0;
41
42
function onClose ()
42
43
{
43
if (++closed === active.length) process.exit(0);
44
if (++closed === active.length) process.exit(exitCode);
44
45
}
45
46
46
47
active.forEach(s => s.close(onClose));
@@ -48,7 +49,7 @@ function shutdown (...servers)
48
49
// Force-exit after 5 s if connections don't drain in time.
49
50
setTimeout(() =>
50
51
{
51
process.exit(0);
52
process.exit(exitCode);
52
53
}, 5000).unref();
53
54
}
54
55
@@ -88,11 +89,13 @@ function start ()
88
89
});
89
90
}
90
91
91
const shutdownAll = () => shutdown(httpServer, httpsServer);
92
const shutdownAll = () => shutdown({}, httpServer, httpsServer);
93
const shutdownForRestart = () => shutdown({ exitCode: 1 }, httpServer, httpsServer);
92
94
93
95
// Give the admin route access to the server handle for graceful shutdown.
94
96
app.set('server', httpServer);
95
97
app.set('shutdown', shutdownAll);
98
app.set('shutdownForRestart', shutdownForRestart);
96
99
97
100
process.on('SIGINT', shutdownAll);
98
101
process.on('SIGTERM', shutdownAll);
@@ -28,8 +28,9 @@ app.get('*', (req, res) =>28
});28
});29
29
30
// Exposed so the shutdown route can trigger a graceful stop.30
// Exposed so the shutdown route can trigger a graceful stop.31
function shutdown (...servers)31
function shutdown (opts = {}, ...servers)32
{32
{33
const exitCode = Number.isInteger(opts.exitCode) ? opts.exitCode : 0;33
const active = servers.filter(Boolean);34
const active = servers.filter(Boolean);34
if (active.length === 0 || active[0].__isShuttingDown) return;35
if (active.length === 0 || active[0].__isShuttingDown) return;35
active.forEach(s => { s.__isShuttingDown = true; });36
active.forEach(s => { s.__isShuttingDown = true; });@@ -40,7 +41,7 @@ function shutdown (...servers)40
let closed = 0;41
let closed = 0;41
function onClose ()42
function onClose ()42
{43
{43
if (++closed === active.length) process.exit(0);44
if (++closed === active.length) process.exit(exitCode);44
}45
}45
46
46
active.forEach(s => s.close(onClose));47
active.forEach(s => s.close(onClose));@@ -48,7 +49,7 @@ function shutdown (...servers)48
// Force-exit after 5 s if connections don't drain in time.49
// Force-exit after 5 s if connections don't drain in time.49
setTimeout(() =>50
setTimeout(() =>50
{51
{51
process.exit(0);52
process.exit(exitCode);52
}, 5000).unref();53
}, 5000).unref();53
}54
}54
55
@@ -88,11 +89,13 @@ function start ()88
});89
});89
}90
}90
91
91
const shutdownAll = () => shutdown(httpServer, httpsServer);92
const shutdownAll = () => shutdown({}, httpServer, httpsServer);93
const shutdownForRestart = () => shutdown({ exitCode: 1 }, httpServer, httpsServer);92
94
93
// Give the admin route access to the server handle for graceful shutdown.95
// Give the admin route access to the server handle for graceful shutdown.94
app.set('server', httpServer);96
app.set('server', httpServer);95
app.set('shutdown', shutdownAll);97
app.set('shutdown', shutdownAll);98
app.set('shutdownForRestart', shutdownForRestart);96
99
97
process.on('SIGINT', shutdownAll);100
process.on('SIGINT', shutdownAll);98
process.on('SIGTERM', shutdownAll);101
process.on('SIGTERM', shutdownAll);@@ -179,7 +179,7 @@ router.post('/shutdown', (req, res) =>
179
179
// Defer so the response is flushed before we start closing.
180
180
setImmediate(() =>
181
181
{
182
const fn = req.app.get('shutdown');
182
const fn = req.app.get('shutdownForRestart') || req.app.get('shutdown');
183
183
if (typeof fn === 'function') fn();
184
184
});
185
185
});
@@ -179,7 +179,7 @@ router.post('/shutdown', (req, res) =>179
// Defer so the response is flushed before we start closing.179
// Defer so the response is flushed before we start closing.180
setImmediate(() =>180
setImmediate(() =>181
{181
{182
const fn = req.app.get('shutdown');182
const fn = req.app.get('shutdownForRestart') || req.app.get('shutdown');183
if (typeof fn === 'function') fn();183
if (typeof fn === 'function') fn();184
});184
});185
});185
});@@ -75,9 +75,12 @@ beforeAll(() => {
75
75
// ── build minimal app ────────────────────────────────────────────────────────
76
76
let server;
77
77
let baseUrl;
78
let shutdownForRestartMock;
78
79
79
80
beforeAll(async () => {
80
81
const app = express();
82
shutdownForRestartMock = jest.fn();
83
app.set('shutdownForRestart', shutdownForRestartMock);
81
84
app.use(express.json());
82
85
app.use('/api/admin', adminRoutes);
83
86
app.use((err, req, res, next) => {
@@ -228,3 +231,16 @@ describe('PUT /api/admin/config', () => {
228
231
expect(status).toBe(400);
229
232
});
230
233
});
234
235
describe('POST /api/admin/shutdown', () => {
236
test('responds ok and invokes shutdown callback', async () => {
237
shutdownForRestartMock.mockClear();
238
const { status, body } = await req('POST', '/api/admin/shutdown');
239
expect(status).toBe(200);
240
expect(body.ok).toBe(true);
241
242
// Shutdown invocation is deferred via setImmediate().
243
await new Promise(resolve => setImmediate(resolve));
244
expect(shutdownForRestartMock).toHaveBeenCalledTimes(1);
245
});
246
});
@@ -75,9 +75,12 @@ beforeAll(() => {75
// ── build minimal app ────────────────────────────────────────────────────────75
// ── build minimal app ────────────────────────────────────────────────────────76
let server;76
let server;77
let baseUrl;77
let baseUrl;78
let shutdownForRestartMock;78
79
79
beforeAll(async () => {80
beforeAll(async () => {80
const app = express();81
const app = express();82
shutdownForRestartMock = jest.fn();83
app.set('shutdownForRestart', shutdownForRestartMock);81
app.use(express.json());84
app.use(express.json());82
app.use('/api/admin', adminRoutes);85
app.use('/api/admin', adminRoutes);83
app.use((err, req, res, next) => {86
app.use((err, req, res, next) => {@@ -228,3 +231,16 @@ describe('PUT /api/admin/config', () => {228
expect(status).toBe(400);231
expect(status).toBe(400);229
});232
});230
});233
});234
235
describe('POST /api/admin/shutdown', () => {236
test('responds ok and invokes shutdown callback', async () => {237
shutdownForRestartMock.mockClear();238
const { status, body } = await req('POST', '/api/admin/shutdown');239
expect(status).toBe(200);240
expect(body.ok).toBe(true);241
242
// Shutdown invocation is deferred via setImmediate().243
await new Promise(resolve => setImmediate(resolve));244
expect(shutdownForRestartMock).toHaveBeenCalledTimes(1);245
});246
});