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');
|
2021-02-19 18:13:09 +03:00
|
|
|
const {i18n} = require('./lib/common');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../shared/logging');
|
2021-02-16 18:29:40 +03:00
|
|
|
const notify = require('./notify');
|
2020-04-29 18:44:27 +03:00
|
|
|
const moment = require('moment');
|
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 {
|
2021-01-27 03:33:55 +03:00
|
|
|
constructor() {
|
|
|
|
this.rootApp = null;
|
2020-08-09 20:55:41 +03:00
|
|
|
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) {
|
2021-02-16 14:54:19 +03:00
|
|
|
if (_.has(config.get('server'), 'socket')) {
|
2020-08-09 20:55:41 +03:00
|
|
|
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;
|
|
|
|
|
2021-02-16 15:26:06 +03:00
|
|
|
if (error.code === 'EADDRINUSE') {
|
2020-08-09 20:55:41 +03:00
|
|
|
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')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:32:37 +03:00
|
|
|
debug('Notifying server started (error)');
|
|
|
|
return notify.notifyServerStarted()
|
|
|
|
.finally(() => {
|
|
|
|
reject(ghostError);
|
|
|
|
});
|
2020-08-09 20:55:41 +03:00
|
|
|
});
|
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')) {
|
2021-02-16 14:54:19 +03:00
|
|
|
self._startTestMode();
|
2020-08-18 17:36:21 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:32:37 +03:00
|
|
|
debug('Notifying server ready (success)');
|
2021-02-16 18:29:40 +03:00
|
|
|
return notify.notifyServerStarted()
|
2020-08-09 20:55:41 +03:00
|
|
|
.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
|
|
|
|
*/
|
Added new, simpler, linear boot process
Background:
- Ghosts existing boot process is split across multiple files, has affordances for outdated ways of running Ghost and is generally non-linear making it nigh-impossible to follow
- The web of dependencies that are loaded on boot are also impossible to unpick, which makes it really hard to decouple Ghost
- With 4.0 we want to introduce a new, linear, simpler, clearer way to boot up Ghost to unlock decoupling Ghost into much smaller pieces
This commit:
- adds a new ghost.js file which switches between boot mode with `node index` or `node index old` so that if we find bugs we can work around them this week
- Note: the old boot process will go away very soon, but ghost.js will remain as the interface between the command to start Ghost and the application code
- reworks the database migration process into a standalone utility, so that the DB is handled as one simple step of the boot process, decoupled from everything else
- is missing tests for this new db utility
- leaves a lot of work to do around loading core code, services, express apps in a sensible order, as work to fix this would start to break the old boot process
- doesn't use the new maintenance app because we aren't restarting the server here, instead we have the concept of a "core app" that starts in maintenance mode - need to think about how apps will be decoupled in the near future
2021-02-02 17:47:16 +03:00
|
|
|
async shutdown(code = 0) {
|
2020-08-11 15:28:04 +03:00
|
|
|
try {
|
|
|
|
logging.warn(i18n.t('notices.httpServer.ghostIsShuttingDown'));
|
|
|
|
await this.stop();
|
Added new, simpler, linear boot process
Background:
- Ghosts existing boot process is split across multiple files, has affordances for outdated ways of running Ghost and is generally non-linear making it nigh-impossible to follow
- The web of dependencies that are loaded on boot are also impossible to unpick, which makes it really hard to decouple Ghost
- With 4.0 we want to introduce a new, linear, simpler, clearer way to boot up Ghost to unlock decoupling Ghost into much smaller pieces
This commit:
- adds a new ghost.js file which switches between boot mode with `node index` or `node index old` so that if we find bugs we can work around them this week
- Note: the old boot process will go away very soon, but ghost.js will remain as the interface between the command to start Ghost and the application code
- reworks the database migration process into a standalone utility, so that the DB is handled as one simple step of the boot process, decoupled from everything else
- is missing tests for this new db utility
- leaves a lot of work to do around loading core code, services, express apps in a sensible order, as work to fix this would start to break the old boot process
- doesn't use the new maintenance app because we aren't restarting the server here, instead we have the concept of a "core app" that starts in maintenance mode - need to think about how apps will be decoupled in the near future
2021-02-02 17:47:16 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(code);
|
|
|
|
}, 100);
|
2020-08-11 15:28:04 +03:00
|
|
|
} catch (error) {
|
|
|
|
logging.error(error);
|
Added new, simpler, linear boot process
Background:
- Ghosts existing boot process is split across multiple files, has affordances for outdated ways of running Ghost and is generally non-linear making it nigh-impossible to follow
- The web of dependencies that are loaded on boot are also impossible to unpick, which makes it really hard to decouple Ghost
- With 4.0 we want to introduce a new, linear, simpler, clearer way to boot up Ghost to unlock decoupling Ghost into much smaller pieces
This commit:
- adds a new ghost.js file which switches between boot mode with `node index` or `node index old` so that if we find bugs we can work around them this week
- Note: the old boot process will go away very soon, but ghost.js will remain as the interface between the command to start Ghost and the application code
- reworks the database migration process into a standalone utility, so that the DB is handled as one simple step of the boot process, decoupled from everything else
- is missing tests for this new db utility
- leaves a lot of work to do around loading core code, services, express apps in a sensible order, as work to fix this would start to break the old boot process
- doesn't use the new maintenance app because we aren't restarting the server here, instead we have the concept of a "core app" that starts in maintenance mode - need to think about how apps will be decoupled in the near future
2021-02-02 17:47:16 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
process.exit(1);
|
|
|
|
}, 100);
|
2020-08-11 15:28:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
try {
|
2021-02-15 22:14:48 +03:00
|
|
|
// If we never fully started, there's nothing to stop
|
|
|
|
if (this.httpServer && this.httpServer.listening) {
|
|
|
|
// We stop the server first so that no new long running requests or processes can be started
|
|
|
|
await this._stopServer();
|
|
|
|
}
|
2020-08-11 23:26:34 +03:00
|
|
|
// Do all of the cleanup tasks
|
|
|
|
await this._cleanup();
|
|
|
|
} finally {
|
|
|
|
// Wrap up
|
|
|
|
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
|
|
|
|
2021-02-16 14:54:19 +03:00
|
|
|
/**
|
|
|
|
* Add a task that should be called on shutdown
|
|
|
|
*/
|
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) => {
|
2021-02-15 22:14:48 +03:00
|
|
|
this.httpServer
|
|
|
|
.stop((error, status) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(status);
|
|
|
|
});
|
2020-08-11 15:28:04 +03:00
|
|
|
});
|
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()));
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:54:19 +03:00
|
|
|
/**
|
|
|
|
* Internal Method for TestMode.
|
|
|
|
*/
|
|
|
|
_startTestMode() {
|
|
|
|
// This is horrible and very temporary
|
|
|
|
const jobService = require('./services/jobs');
|
|
|
|
|
|
|
|
// Output how many connections are open every 5 seconds
|
|
|
|
const connectionInterval = setInterval(() => this.httpServer.getConnections(
|
|
|
|
(err, connections) => logging.warn(`${connections} connections currently open`)
|
|
|
|
), 5000);
|
|
|
|
|
|
|
|
// Output a notice when the server closes
|
|
|
|
this.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()}`);
|
|
|
|
|
|
|
|
const runningScheduledjobs = Object.keys(jobService.bree.workers);
|
|
|
|
if (Object.keys(jobService.bree.workers).length) {
|
|
|
|
logging.warn(`${Object.keys(jobService.bree.workers).length} jobs running: ${runningScheduledjobs}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const scheduledJobs = Object.keys(jobService.bree.intervals);
|
|
|
|
if (Object.keys(jobService.bree.intervals).length) {
|
|
|
|
logging.warn(`${Object.keys(jobService.bree.intervals).length} scheduled jobs: ${scheduledJobs}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runningScheduledjobs.length === 0 && scheduledJobs.length === 0) {
|
|
|
|
logging.warn('No scheduled or running jobs');
|
|
|
|
}
|
|
|
|
}, 5000);
|
2020-08-11 23:26:34 +03:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:55:41 +03:00
|
|
|
/**
|
2021-02-16 14:54:19 +03:00
|
|
|
* Log Start Messages
|
2020-08-09 20:55:41 +03:00
|
|
|
*/
|
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
|
|
|
/**
|
2021-02-16 14:54:19 +03:00
|
|
|
* Log Stop Messages
|
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;
|