Ghost/core/server/notify.js
Daniel Lockyer ec28478435
🐛 Fixed error when sending messages via bootstrap socket
refs https://github.com/TryGhost/Ghost/issues/13837

- a [refactor](9fa8800b9d) in `@tryghost/bootstrap-socket` changed the signature of the method it exprots, but the use of this
  library wasn't updated in Ghost
- therefore, the library was trying to send the `@tryghost/logging` library, which caused
  errors with Ghost-CLI down the line, as mentioned in the issue
- this commit updates the method signature to match what is expected
2021-12-06 08:04:26 +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('@tryghost/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, 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);
};