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'); };