From 8c1afc3399b7e123de9097124a6c8b92768afcc6 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 14 Dec 2020 15:48:13 -0500 Subject: [PATCH] express: New expressCloseServer hook This will be used by a future commit to close all socket.io connections during server restart. --- doc/api/hooks_server-side.md | 19 +++++++++++++++++++ src/node/hooks/express.js | 5 ++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index 5a9802b7d..e13adfa97 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -76,6 +76,25 @@ Things in context: This hook gets called after the application object has been created, but before it starts listening. This is similar to the expressConfigure hook, but it's not guaranteed that the application object will have all relevant configuration variables. +## expressCloseServer + +Called from: src/node/hooks/express.js + +Things in context: Nothing + +This hook is called when the HTTP server is closing, which happens during +shutdown (see the shutdown hook) and when the server restarts (e.g., when a +plugin is installed via the `/admin/plugins` page). The HTTP server may or may +not already be closed when this hook executes. + +Example: + +``` +exports.expressCloseServer = async () => { + await doSomeCleanup(); +}; +``` + ## eejsBlock_`` Called from: src/node/eejs/index.js diff --git a/src/node/hooks/express.js b/src/node/hooks/express.js index 50cb8657e..b3d4f34e4 100644 --- a/src/node/hooks/express.js +++ b/src/node/hooks/express.js @@ -20,7 +20,10 @@ exports.server = null; const closeServer = async () => { if (exports.server == null) return; logger.info('Closing HTTP server...'); - await util.promisify(exports.server.close.bind(exports.server))(); + await Promise.all([ + util.promisify(exports.server.close.bind(exports.server))(), + hooks.aCallAll('expressCloseServer'), + ]); exports.server = null; logger.info('HTTP server closed'); };