Changed where we trigger server start/stop announcement (#9815)

closes #9802

- we have to trigger both functions within Ghost core, otherwise people who are using Ghost as NPM module have to call these functions
- this is internal logic
- plus: this logic is conditional, because of our internal maintenance flag
- make it backwards compatible in case you call announceServerStart or announceServerStopped twice
- tested with "Ghost as NPM module" and with the CLI on production
This commit is contained in:
Katharina Irrgang 2018-08-22 13:28:31 +02:00 committed by GitHub
parent 85213ed553
commit 57a8bf229e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 22 deletions

View File

@ -1,6 +1,8 @@
// ## Server Loader
// Passes options through the boot process to get a server instance back
var server = require('./server');
const server = require('./server');
const common = require('./server/lib/common');
const GhostServer = require('./server/ghost-server');
// Set the default environment to be `development`
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
@ -8,7 +10,17 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development';
function makeGhost(options) {
options = options || {};
return server(options);
return server(options)
.catch((err) => {
if (!common.errors.utils.isIgnitionError(err)) {
err = new common.errors.GhostError({message: err.message, err: err});
}
return GhostServer.announceServerStopped(err)
.finally(() => {
throw err;
});
});
}
module.exports = makeGhost;

View File

@ -94,9 +94,12 @@ GhostServer.prototype.start = function (externalApp) {
self.httpServer.on('connection', self.connection.bind(self));
self.httpServer.on('listening', function () {
debug('...Started');
self.logStartMessages();
resolve(self);
return GhostServer.announceServerStart()
.finally(() => {
resolve(self);
});
});
});
};
@ -312,7 +315,19 @@ const connectToBootstrapSocket = (message) => {
});
};
/**
* @NOTE announceServerStartCalled:
*
* - backwards compatible logic, because people complained that not all themes were loaded when using Ghost as NPM module
* - we told them to call `announceServerStart`, which is not required anymore, because we restructured the code
*/
let announceServerStartCalled = false;
module.exports.announceServerStart = function announceServerStart() {
if (announceServerStartCalled || config.get('maintenance:enabled')) {
return Promise.resolve();
}
announceServerStartCalled = true;
common.events.emit('server.start');
// CASE: IPC communication to the CLI via child process.
@ -332,7 +347,19 @@ module.exports.announceServerStart = function announceServerStart() {
return Promise.resolve();
};
/**
* @NOTE announceServerStopCalled:
*
* - backwards compatible logic, because people complained that not all themes were loaded when using Ghost as NPM module
* - we told them to call `announceServerStart`, which is not required anymore, because we restructured code
*/
let announceServerStopCalled = false;
module.exports.announceServerStopped = function announceServerStopped(error) {
if (announceServerStopCalled) {
return Promise.resolve();
}
announceServerStopCalled = true;
// CASE: IPC communication to the CLI via child process.
if (process.send) {
process.send({

View File

@ -3,7 +3,7 @@
var startTime = Date.now(),
debug = require('ghost-ignition').debug('boot:index'),
ghost, express, common, urlService, parentApp, config, GhostServer;
ghost, express, common, urlService, parentApp;
debug('First requires...');
@ -12,8 +12,6 @@ ghost = require('./core');
debug('Required ghost');
express = require('express');
GhostServer = require('./core/server/ghost-server');
config = require('./core/server/config');
common = require('./core/server/lib/common');
urlService = require('./core/server/services/url');
parentApp = express();
@ -28,21 +26,10 @@ ghost().then(function (ghostServer) {
return ghostServer.start(parentApp)
.then(function afterStart() {
common.logging.info('Ghost boot', (Date.now() - startTime) / 1000 + 's');
if (!config.get('maintenance:enabled')) {
return GhostServer.announceServerStart();
}
});
}).catch(function (err) {
if (!common.errors.utils.isIgnitionError(err)) {
err = new common.errors.GhostError({message: err.message, err: err});
}
return GhostServer.announceServerStopped(err)
.finally(() => {
common.logging.error(err);
setTimeout(() => {
process.exit(-1);
}, 100);
});
common.logging.error(err);
setTimeout(() => {
process.exit(-1);
}, 100);
});