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

Refactored notify to send started + ready

- In the old boot the server wasn't started til we were ready
- In new boot, we start the server immediately and send the old started event
- Then, when we are ready to accept some traffic, we send a ready event
- At the moment, ready isn't quite sent at the right time:
   - It _should_ be when we're ready to serve real traffic, not just send 503s
   - This is after the URL generation has finished
   - But this requires more refactoring work :(
   - So for now we send when everything else is ready
- This really needs some tests
This commit is contained in:
Hannah Wolfe 2021-02-19 20:32:37 +00:00
parent 2527efd6fc
commit 176433e307
4 changed files with 37 additions and 26 deletions

View file

@ -187,16 +187,15 @@ function mountGhost(rootApp, ghostApp) {
debug('End: mountGhost'); debug('End: mountGhost');
} }
// @TODO: make this notification different function notifyServerReady(error) {
function notifyReadiness(error) {
const notify = require('./server/notify'); const notify = require('./server/notify');
if (error) { if (error) {
debug('Notifying readiness (error)'); debug('Notifying server ready (error)');
notify.notifyServerStarted(error); notify.notifyServerReady(error);
} else { } else {
debug('Notifying readiness (success)'); debug('Notifying server ready (success)');
notify.notifyServerStarted(); notify.notifyServerReady();
} }
} }
@ -269,7 +268,7 @@ async function bootGhost() {
// We are technically done here // We are technically done here
bootLogger.log('booted'); bootLogger.log('booted');
notifyReadiness(); notifyServerReady();
// Init our background jobs, we don't wait for this to finish // Init our background jobs, we don't wait for this to finish
initRecurringJobs({config}); initRecurringJobs({config});
@ -289,10 +288,10 @@ async function bootGhost() {
} }
logging.error(serverStartError); logging.error(serverStartError);
notifyReadiness(serverStartError);
// If ghost was started and something else went wrong, we shut it down // If ghost was started and something else went wrong, we shut it down
if (ghostServer) { if (ghostServer) {
notifyServerReady(serverStartError);
ghostServer.shutdown(2); ghostServer.shutdown(2);
} else { } else {
// Ghost server failed to start, set a timeout to give logging a chance to flush // Ghost server failed to start, set a timeout to give logging a chance to flush

View file

@ -96,7 +96,11 @@ class GhostServer {
}); });
} }
reject(ghostError); debug('Notifying server started (error)');
return notify.notifyServerStarted()
.finally(() => {
reject(ghostError);
});
}); });
self.httpServer.on('listening', function () { self.httpServer.on('listening', function () {
@ -108,7 +112,7 @@ class GhostServer {
self._startTestMode(); self._startTestMode();
} }
debug('server notifying started'); debug('Notifying server ready (success)');
return notify.notifyServerStarted() return notify.notifyServerStarted()
.finally(() => { .finally(() => {
resolve(self); resolve(self);

View file

@ -10,7 +10,10 @@
const config = require('../shared/config'); const config = require('../shared/config');
const logging = require('../shared/logging'); const logging = require('../shared/logging');
let notifyServerStartedCalled = false; let notified = {
started: false,
ready: false
};
const debugInfo = { const debugInfo = {
versions: process.versions, versions: process.versions,
@ -19,27 +22,27 @@ const debugInfo = {
release: process.release release: process.release
}; };
module.exports.notifyServerStarted = function (error = null) { async function notify(type, error = null) {
// If we already sent a ready notification, we should not do it again // If we already sent this notification, we should not do it again
if (notifyServerStartedCalled) { if (notified[type]) {
return Promise.resolve(); return;
} }
// Mark this function as called // Mark this function as called
notifyServerStartedCalled = true; notified[type] = true;
// Build our message // Build our message
// - if there's no error then the server is ready
let message = {
started: true,
debug: debugInfo
};
// - if there's an error then the server is not ready, include the errors // - if there's an error then the server is not ready, include the errors
// - if there's no error then the server has started
let message = {};
if (error) { if (error) {
message.started = false; message[type] = false;
message.error = error; message.error = error;
} else {
message[type] = true;
} }
// Add debug info to the message
message.debug = debugInfo;
// CASE: IPC communication to the CLI for local process manager // CASE: IPC communication to the CLI for local process manager
if (process.send) { if (process.send) {
@ -54,4 +57,12 @@ module.exports.notifyServerStarted = function (error = null) {
} }
return Promise.resolve(); return Promise.resolve();
}
module.exports.notifyServerStarted = async function (error = null) {
return await notify('started', error);
};
module.exports.notifyServerReady = async function (error = null) {
return await notify('ready', error);
}; };

View file

@ -251,9 +251,6 @@ const freshModeGhostStart = async (options) => {
// Actually boot Ghost // Actually boot Ghost
await bootGhost(options); await bootGhost(options);
// Ensure notify was called (this is idempotent)
notify.notifyServerStarted();
// Wait for the URL service to be ready, which happens after boot, but don't re-trigger db.ready // Wait for the URL service to be ready, which happens after boot, but don't re-trigger db.ready
await urlServiceUtils.isFinished({disableDbReadyEvent: true}); await urlServiceUtils.isFinished({disableDbReadyEvent: true});
}; };