Ghost/core/server/notify.js
Hannah Wolfe 176433e307 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
2021-02-19 20:42:10 +00:00

69 lines
1.9 KiB
JavaScript

/**
* We call notify server started when the server is ready to serve traffic
* When the server is started, but not ready, it is only able to serve 503s
*
* If the server isn't able to reach started, notifyServerStarted is called with an error
* A status message, any error, and debug info are all passed to managing processes via IPC and the bootstrap socket
*/
// Required Ghost internals
const config = require('../shared/config');
const logging = require('../shared/logging');
let notified = {
started: false,
ready: false
};
const debugInfo = {
versions: process.versions,
platform: process.platform,
arch: process.arch,
release: process.release
};
async function notify(type, error = null) {
// If we already sent this notification, we should not do it again
if (notified[type]) {
return;
}
// Mark this function as called
notified[type] = true;
// Build our message
// - 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) {
message[type] = false;
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
if (process.send) {
process.send(message);
}
// CASE: use bootstrap socket to communicate with CLI for systemd
let socketAddress = config.get('bootstrap-socket');
if (socketAddress) {
const bootstrapSocket = require('@tryghost/bootstrap-socket');
return bootstrapSocket.connectAndSend(socketAddress, logging, message);
}
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);
};