2021-02-16 18:29:40 +03:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
2021-02-19 23:32:37 +03:00
|
|
|
let notified = {
|
|
|
|
started: false,
|
|
|
|
ready: false
|
|
|
|
};
|
2021-02-16 18:29:40 +03:00
|
|
|
|
|
|
|
const debugInfo = {
|
|
|
|
versions: process.versions,
|
|
|
|
platform: process.platform,
|
|
|
|
arch: process.arch,
|
|
|
|
release: process.release
|
|
|
|
};
|
|
|
|
|
2021-02-19 23:32:37 +03:00
|
|
|
async function notify(type, error = null) {
|
|
|
|
// If we already sent this notification, we should not do it again
|
|
|
|
if (notified[type]) {
|
|
|
|
return;
|
2021-02-16 18:29:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark this function as called
|
2021-02-19 23:32:37 +03:00
|
|
|
notified[type] = true;
|
2021-02-16 18:29:40 +03:00
|
|
|
|
|
|
|
// Build our message
|
|
|
|
// - if there's an error then the server is not ready, include the errors
|
2021-02-19 23:32:37 +03:00
|
|
|
// - if there's no error then the server has started
|
|
|
|
let message = {};
|
2021-02-16 18:29:40 +03:00
|
|
|
if (error) {
|
2021-02-19 23:32:37 +03:00
|
|
|
message[type] = false;
|
2021-02-16 18:29:40 +03:00
|
|
|
message.error = error;
|
2021-02-19 23:32:37 +03:00
|
|
|
} else {
|
|
|
|
message[type] = true;
|
2021-02-16 18:29:40 +03:00
|
|
|
}
|
2021-02-19 23:32:37 +03:00
|
|
|
// Add debug info to the message
|
|
|
|
message.debug = debugInfo;
|
2021-02-16 18:29:40 +03:00
|
|
|
|
|
|
|
// 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();
|
2021-02-19 23:32:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.notifyServerStarted = async function (error = null) {
|
|
|
|
return await notify('started', error);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.notifyServerReady = async function (error = null) {
|
|
|
|
return await notify('ready', error);
|
2021-02-16 18:29:40 +03:00
|
|
|
};
|