0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Added stoppable for graceful shutdown of requests

- stopppable is a dependency that handles closing connections properly, which server.close does not
    - active connections are allowed to complete what they are doing
    - idle connections are closed
    - no new connections are allowed
- we call stoppable in stop() instead of server.close so that idle connections don't hold the server open
- calling await stop() from shutdown then ensures that we have a consistent experience of stop
- all together this allows ghost to shutdown gracefully when there are long-running requests
- @TODO: handle graceful shutdown of long-running processes
- @TODO: consider do we need to send 503s whilst the server is shutting down?
This commit is contained in:
Hannah Wolfe 2020-08-10 11:42:31 +01:00
parent e72cc193c6
commit 19e3b70c7a
4 changed files with 26 additions and 6 deletions

View file

@ -13,6 +13,8 @@ const {events, i18n} = require('./lib/common');
const logging = require('../shared/logging');
const moment = require('moment');
const bootstrapSocket = require('@tryghost/bootstrap-socket');
const stoppable = require('stoppable');
const {reject} = require('bluebird');
/**
* ## GhostServer
@ -109,10 +111,16 @@ class GhostServer {
});
});
function shutdown() {
// @TODO: await self.stop() here for consistency - but first we need stop to behave correctly
self.logStopMessages();
stoppable(self.httpServer, config.get('server:shutdownTimeout'));
async function shutdown() {
try {
await self.stop();
process.exit(0);
} catch (error) {
logging.error(error);
process.exit(-1);
}
}
// ensure that Ghost exits correctly on Ctrl+C and SIGTERM
@ -135,7 +143,12 @@ class GhostServer {
if (self.httpServer === null) {
resolve(self);
} else {
self.httpServer.close(function () {
// The stop function comes from stoppable
self.httpServer.stop(function (err) {
if (err) {
reject(self);
}
events.emit('server.stop');
self.httpServer = null;
self.logStopMessages();

View file

@ -2,7 +2,8 @@
"url": "http://localhost:2368",
"server": {
"host": "127.0.0.1",
"port": 2368
"port": 2368,
"shutdownTimeout": 600000
},
"admin": {
"redirects": true

View file

@ -127,6 +127,7 @@
"rss": "1.2.2",
"sanitize-html": "1.27.2",
"semver": "7.3.2",
"stoppable": "1.1.0",
"tough-cookie": "4.0.0",
"uuid": "8.3.0",
"validator": "6.3.0",

View file

@ -8343,6 +8343,11 @@ stealthy-require@^1.1.1:
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b"
integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=
stoppable@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/stoppable/-/stoppable-1.1.0.tgz#32da568e83ea488b08e4d7ea2c3bcc9d75015d5b"
integrity sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw==
stream-buffers@^2.1.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/stream-buffers/-/stream-buffers-2.2.0.tgz#91d5f5130d1cef96dcfa7f726945188741d09ee4"