2015-05-28 18:16:09 +03:00
|
|
|
// # Ghost Server
|
|
|
|
// Handles the creation of an HTTP Server for Ghost
|
2020-04-29 18:44:27 +03:00
|
|
|
const debug = require('ghost-ignition').debug('server');
|
|
|
|
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const _ = require('lodash');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../shared/config');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('./../shared/url-utils');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {events, i18n} = require('./lib/common');
|
|
|
|
const logging = require('../shared/logging');
|
2020-04-29 18:44:27 +03:00
|
|
|
const moment = require('moment');
|
2020-08-09 19:22:27 +03:00
|
|
|
const bootstrapSocket = require('@tryghost/bootstrap-socket');
|
2020-08-10 13:42:31 +03:00
|
|
|
const stoppable = require('stoppable');
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2015-05-28 18:16:09 +03:00
|
|
|
/**
|
|
|
|
* ## GhostServer
|
|
|
|
*/
|
2020-08-09 20:55:41 +03:00
|
|
|
class GhostServer {
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} rootApp - parent express instance
|
|
|
|
*/
|
|
|
|
constructor(rootApp) {
|
|
|
|
this.rootApp = rootApp;
|
|
|
|
this.httpServer = null;
|
|
|
|
|
|
|
|
// Expose config module for use externally.
|
|
|
|
this.config = config;
|
2020-08-11 23:26:34 +03:00
|
|
|
|
|
|
|
// Tasks that should be run before the server exits
|
|
|
|
this.cleanupTasks = [];
|
2020-08-09 20:55:41 +03:00
|
|
|
}
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
|
|
|
* ## Public API methods
|
|
|
|
*
|
|
|
|
* ### Start
|
|
|
|
* Starts the ghost server listening on the configured port.
|
|
|
|
* Alternatively you can pass in your own express instance and let Ghost
|
|
|
|
* start listening for you.
|
|
|
|
* @param {Object} externalApp - Optional express app instance.
|
|
|
|
* @return {Promise} Resolves once Ghost has started
|
|
|
|
*/
|
|
|
|
start(externalApp) {
|
|
|
|
debug('Starting...');
|
|
|
|
const self = this;
|
|
|
|
const rootApp = externalApp ? externalApp : self.rootApp;
|
|
|
|
let socketConfig;
|
|
|
|
|
|
|
|
const socketValues = {
|
|
|
|
path: path.join(config.get('paths').contentPath, config.get('env') + '.socket'),
|
|
|
|
permissions: '660'
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(config.get('server'), 'socket')) {
|
|
|
|
socketConfig = config.get('server').socket;
|
|
|
|
|
|
|
|
if (_.isString(socketConfig)) {
|
|
|
|
socketValues.path = socketConfig;
|
|
|
|
} else if (_.isObject(socketConfig)) {
|
|
|
|
socketValues.path = socketConfig.path || socketValues.path;
|
|
|
|
socketValues.permissions = socketConfig.permissions || socketValues.permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the socket is gone before trying to create another
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(socketValues.path);
|
|
|
|
} catch (e) {
|
|
|
|
// We can ignore this.
|
|
|
|
}
|
|
|
|
|
|
|
|
self.httpServer = rootApp.listen(socketValues.path);
|
|
|
|
fs.chmod(socketValues.path, socketValues.permissions);
|
|
|
|
config.set('server:socket', socketValues);
|
|
|
|
} else {
|
|
|
|
self.httpServer = rootApp.listen(
|
|
|
|
config.get('server').port,
|
|
|
|
config.get('server').host
|
|
|
|
);
|
2016-09-13 21:30:47 +03:00
|
|
|
}
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
self.httpServer.on('error', function (error) {
|
|
|
|
let ghostError;
|
|
|
|
|
|
|
|
if (error.errno === 'EADDRINUSE') {
|
|
|
|
ghostError = new errors.GhostError({
|
|
|
|
message: i18n.t('errors.httpServer.addressInUse.error'),
|
|
|
|
context: i18n.t('errors.httpServer.addressInUse.context', {port: config.get('server').port}),
|
|
|
|
help: i18n.t('errors.httpServer.addressInUse.help')
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
ghostError = new errors.GhostError({
|
|
|
|
message: i18n.t('errors.httpServer.otherError.error', {errorNumber: error.errno}),
|
|
|
|
context: i18n.t('errors.httpServer.otherError.context'),
|
|
|
|
help: i18n.t('errors.httpServer.otherError.help')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reject(ghostError);
|
|
|
|
});
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
self.httpServer.on('listening', function () {
|
|
|
|
debug('...Started');
|
2020-08-11 15:28:04 +03:00
|
|
|
self._logStartMessages();
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2020-08-18 17:36:21 +03:00
|
|
|
// Debug logs output in testmode only
|
|
|
|
if (config.get('server:testmode')) {
|
|
|
|
// This is horrible and very temporary
|
|
|
|
const jobService = require('./services/jobs');
|
|
|
|
|
|
|
|
// Output how many connections are open every 5 seconds
|
|
|
|
const connectionInterval = setInterval(() => self.httpServer.getConnections(
|
|
|
|
(err, connections) => logging.warn(`${connections} connections currently open`)
|
|
|
|
), 5000);
|
|
|
|
|
|
|
|
// Output a notice when the server closes
|
|
|
|
self.httpServer.on('close', function () {
|
|
|
|
clearInterval(connectionInterval);
|
|
|
|
logging.warn('Server has fully closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Output job queue length every 5 seconds
|
|
|
|
setInterval(() => logging.warn(`${jobService.queue.length()} jobs in the queue. Idle: ${jobService.queue.idle()}`), 5000);
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
return GhostServer.announceServerReadiness()
|
|
|
|
.finally(() => {
|
|
|
|
resolve(self);
|
|
|
|
});
|
|
|
|
});
|
2016-11-07 17:25:29 +03:00
|
|
|
|
2020-08-10 13:42:31 +03:00
|
|
|
stoppable(self.httpServer, config.get('server:shutdownTimeout'));
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
// ensure that Ghost exits correctly on Ctrl+C and SIGTERM
|
|
|
|
process
|
2020-08-11 15:28:04 +03:00
|
|
|
.removeAllListeners('SIGINT').on('SIGINT', self.shutdown.bind(self))
|
|
|
|
.removeAllListeners('SIGTERM').on('SIGTERM', self.shutdown.bind(self));
|
2015-05-28 18:16:09 +03:00
|
|
|
});
|
2020-08-09 20:55:41 +03:00
|
|
|
}
|
2020-08-07 21:50:09 +03:00
|
|
|
|
2020-08-11 15:28:04 +03:00
|
|
|
/**
|
|
|
|
* ### Shutdown
|
|
|
|
* Stops the server, handles cleanup and exits the process = a full shutdown
|
|
|
|
* Called on SIGINT or SIGTERM
|
|
|
|
*/
|
|
|
|
async shutdown() {
|
|
|
|
try {
|
|
|
|
logging.warn(i18n.t('notices.httpServer.ghostIsShuttingDown'));
|
|
|
|
await this.stop();
|
|
|
|
process.exit(0);
|
|
|
|
} catch (error) {
|
|
|
|
logging.error(error);
|
|
|
|
process.exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
|
|
|
* ### Stop
|
2020-08-11 15:28:04 +03:00
|
|
|
* Stops the server & handles cleanup, but does not exit the process
|
|
|
|
* Used in tests for quick start/stop actions
|
|
|
|
* Called by shutdown to handle server stop and cleanup before exiting
|
2020-08-09 20:55:41 +03:00
|
|
|
* @returns {Promise} Resolves once Ghost has stopped
|
|
|
|
*/
|
2020-08-11 15:28:04 +03:00
|
|
|
async stop() {
|
2020-08-11 23:26:34 +03:00
|
|
|
// If we never fully started, there's nothing to stop
|
2020-08-11 15:28:04 +03:00
|
|
|
if (this.httpServer === null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-09 20:55:41 +03:00
|
|
|
|
2020-08-11 23:26:34 +03:00
|
|
|
try {
|
|
|
|
// We stop the server first so that no new long running requests or processes can be started
|
|
|
|
await this._stopServer();
|
|
|
|
// Do all of the cleanup tasks
|
|
|
|
await this._cleanup();
|
|
|
|
} finally {
|
|
|
|
// Wrap up
|
|
|
|
events.emit('server.stop');
|
|
|
|
this.httpServer = null;
|
|
|
|
this._logStopMessages();
|
|
|
|
}
|
2020-08-09 20:55:41 +03:00
|
|
|
}
|
2020-08-07 22:14:31 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
|
|
|
* ### Hammertime
|
|
|
|
* To be called after `stop`
|
|
|
|
*/
|
2020-08-11 15:28:04 +03:00
|
|
|
async hammertime() {
|
2020-08-09 20:55:41 +03:00
|
|
|
logging.info(i18n.t('notices.httpServer.cantTouchThis'));
|
2020-08-11 15:28:04 +03:00
|
|
|
}
|
2020-08-07 22:14:31 +03:00
|
|
|
|
2020-08-11 23:26:34 +03:00
|
|
|
registerCleanupTask(task) {
|
|
|
|
this.cleanupTasks.push(task);
|
|
|
|
}
|
|
|
|
|
2020-08-11 15:28:04 +03:00
|
|
|
/**
|
|
|
|
* ### Stop Server
|
|
|
|
* Does the work of stopping the server using stoppable
|
|
|
|
* This handles closing connections:
|
|
|
|
* - New connections are rejected
|
|
|
|
* - Idle connections are closed immediately
|
|
|
|
* - Active connections are allowed to complete in-flight requests before being closed
|
|
|
|
*
|
|
|
|
* If server.shutdownTimeout is reached, requests are terminated in-flight
|
|
|
|
*/
|
|
|
|
async _stopServer() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.httpServer.stop((err, status) => (err ? reject(err) : resolve(status)));
|
|
|
|
});
|
2020-08-09 20:55:41 +03:00
|
|
|
}
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2020-08-11 23:26:34 +03:00
|
|
|
async _cleanup() {
|
|
|
|
// Wait for all cleanup tasks to finish
|
|
|
|
await Promise
|
|
|
|
.all(this.cleanupTasks.map(task => task()));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onShutdownComplete() {
|
|
|
|
// Wrap up
|
|
|
|
events.emit('server.stop');
|
|
|
|
this.httpServer = null;
|
|
|
|
this._logStopMessages();
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
|
|
|
* ### Log Start Messages
|
|
|
|
*/
|
2020-08-11 15:28:04 +03:00
|
|
|
_logStartMessages() {
|
2020-08-09 20:55:41 +03:00
|
|
|
logging.info(i18n.t('notices.httpServer.ghostIsRunningIn', {env: config.get('env')}));
|
2015-05-28 18:16:09 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
if (config.get('env') === 'production') {
|
|
|
|
logging.info(i18n.t('notices.httpServer.yourBlogIsAvailableOn', {url: urlUtils.urlFor('home', true)}));
|
2015-05-28 18:16:09 +03:00
|
|
|
} else {
|
2020-08-09 20:55:41 +03:00
|
|
|
logging.info(i18n.t('notices.httpServer.listeningOn', {
|
|
|
|
host: config.get('server').socket || config.get('server').host,
|
|
|
|
port: config.get('server').port
|
|
|
|
}));
|
|
|
|
logging.info(i18n.t('notices.httpServer.urlConfiguredAs', {url: urlUtils.urlFor('home', true)}));
|
2015-05-28 18:16:09 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
logging.info(i18n.t('notices.httpServer.ctrlCToShutDown'));
|
2014-08-19 20:36:46 +04:00
|
|
|
}
|
2017-07-11 11:00:07 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
|
|
|
* ### Log Stop Messages
|
2020-08-11 15:28:04 +03:00
|
|
|
* Private / internal API
|
2020-08-09 20:55:41 +03:00
|
|
|
*/
|
2020-08-11 15:28:04 +03:00
|
|
|
_logStopMessages() {
|
2020-08-09 20:55:41 +03:00
|
|
|
logging.warn(i18n.t('notices.httpServer.ghostHasShutdown'));
|
2014-08-19 20:36:46 +04:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
// Extra clear message for production mode
|
|
|
|
if (config.get('env') === 'production') {
|
|
|
|
logging.warn(i18n.t('notices.httpServer.yourBlogIsNowOffline'));
|
|
|
|
}
|
2020-08-07 21:45:22 +03:00
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
// Always output uptime
|
|
|
|
logging.warn(
|
|
|
|
i18n.t('notices.httpServer.ghostWasRunningFor'),
|
|
|
|
moment.duration(process.uptime(), 'seconds').humanize()
|
|
|
|
);
|
2020-08-07 21:45:22 +03:00
|
|
|
}
|
2020-08-09 20:55:41 +03:00
|
|
|
}
|
2014-08-19 20:36:46 +04:00
|
|
|
|
|
|
|
module.exports = GhostServer;
|
2018-07-27 16:19:39 +03:00
|
|
|
|
2018-08-22 14:28:31 +03:00
|
|
|
/**
|
2020-08-09 18:40:47 +03:00
|
|
|
* We call announce server readiness when the server is ready
|
|
|
|
* When the server is started, but not ready, it is only able to serve 503s
|
2018-08-22 14:28:31 +03:00
|
|
|
*
|
2020-08-09 18:40:47 +03:00
|
|
|
* If the server isn't able to reach readiness, announceServerReadiness 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
|
2018-08-22 14:28:31 +03:00
|
|
|
*/
|
2020-08-09 18:40:47 +03:00
|
|
|
let announceServerReadinessCalled = false;
|
2018-08-14 20:04:25 +03:00
|
|
|
|
2020-08-09 18:40:47 +03:00
|
|
|
const debugInfo = {
|
|
|
|
versions: process.versions,
|
|
|
|
platform: process.platform,
|
|
|
|
arch: process.arch,
|
|
|
|
release: process.release
|
2018-07-27 16:19:39 +03:00
|
|
|
};
|
|
|
|
|
2020-08-09 18:40:47 +03:00
|
|
|
module.exports.announceServerReadiness = function (error = null) {
|
|
|
|
// If we already announced readiness, we should not do it again
|
|
|
|
if (announceServerReadinessCalled) {
|
2018-08-22 14:28:31 +03:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2020-08-09 18:40:47 +03:00
|
|
|
// Mark this function as called
|
|
|
|
announceServerReadinessCalled = true;
|
|
|
|
|
|
|
|
// Build our message
|
|
|
|
// - if there's no error then the server is ready
|
2020-08-07 18:27:04 +03:00
|
|
|
let message = {
|
2020-08-09 18:40:47 +03:00
|
|
|
started: true,
|
2020-08-07 18:27:04 +03:00
|
|
|
debug: debugInfo
|
|
|
|
};
|
|
|
|
|
2020-08-09 18:40:47 +03:00
|
|
|
// - if there's an error then the server is not ready, include the errors
|
|
|
|
if (error) {
|
|
|
|
message.started = false;
|
|
|
|
message.error = error;
|
2020-08-11 12:07:00 +03:00
|
|
|
} else {
|
|
|
|
events.emit('server.start');
|
2020-08-09 18:40:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: IPC communication to the CLI for local process manager
|
2018-07-27 16:19:39 +03:00
|
|
|
if (process.send) {
|
2020-08-07 18:27:04 +03:00
|
|
|
process.send(message);
|
2018-07-27 16:19:39 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 18:40:47 +03:00
|
|
|
// CASE: use bootstrap socket to communicate with CLI for systemd
|
|
|
|
let socketAddress = config.get('bootstrap-socket');
|
2020-08-07 18:27:04 +03:00
|
|
|
if (socketAddress) {
|
2020-08-09 19:22:27 +03:00
|
|
|
return bootstrapSocket.connectAndSend(socketAddress, logging, message);
|
2018-07-27 16:19:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|