strapi/lib/restart.js

104 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Module dependencies
*/
// Node.js core.
const cluster = require('cluster');
// Public node modules.
const _ = require('lodash');
2015-11-09 15:10:53 +01:00
const async = require('async');
/**
* Programmatically restart the server
* (useful for the Studio)
*/
2015-12-02 17:09:09 +01:00
module.exports = cb => {
const self = this;
2015-11-09 15:10:53 +01:00
console.log();
// Update the Strapi status (might be used
// by the core or some hooks).
this.reloading = true;
2015-11-09 15:10:53 +01:00
// Async module loader to rebuild a
// dictionary of the application.
async.auto({
// Rebuild the dictionaries.
dictionaries: cb => {
2015-12-02 17:09:09 +01:00
self.on('hook:_config:reloaded', () => {
self.on('hook:_api:reloaded', () => cb());
self.hooks._api.reload();
});
2015-11-30 16:15:14 +01:00
2015-12-02 17:09:09 +01:00
self.hooks._config.reload();
2015-11-09 15:10:53 +01:00
}
},
// Callback.
err => {
2015-11-09 15:10:53 +01:00
// Just in case there is an error.
if (err) {
2015-12-02 17:09:09 +01:00
self.log.error('Impossible to reload the server');
self.log.error('Please restart the server manually');
self.stop();
2015-11-09 15:10:53 +01:00
}
// Tell the application the framework is reloading
// (might be used by some hooks).
2015-12-02 17:09:09 +01:00
self.reloading = true;
2015-11-09 15:10:53 +01:00
2015-11-30 12:08:47 +01:00
// Run adapters installation
if (cluster.isMaster) {
2015-12-02 17:09:09 +01:00
self.hooks.waterline.installation();
2015-11-30 12:08:47 +01:00
}
2015-11-30 12:08:47 +01:00
// Install new adapters
2015-12-02 16:55:56 +01:00
strapi.after('hook:waterline:installed', () => {
2015-12-02 17:09:09 +01:00
self.log.warn('Application is restarting...');
2015-11-30 12:08:47 +01:00
console.log();
// Teardown Waterline adapters and
// reload the Waterline ORM.
2015-12-02 17:09:09 +01:00
self.after('hook:waterline:reloaded', () => {
self.after('hook:router:reloaded', () => {
2015-12-02 16:55:56 +01:00
process.nextTick(() => cb());
2015-11-30 12:08:47 +01:00
// Update `strapi` status.
2015-12-02 17:09:09 +01:00
self.reloaded = true;
self.reloading = false;
2015-11-30 12:08:47 +01:00
// Finally inform the developer everything seems ok.
if (cluster.isMaster && _.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers < 1) {
2015-12-02 17:09:09 +01:00
self.log.info('Application\'s dictionnary updated');
self.log.warn('You still need to restart your server to fully enjoy changes...');
2015-11-30 12:08:47 +01:00
}
2015-12-02 17:09:09 +01:00
self.once('restart:done', function () {
2015-11-30 12:08:47 +01:00
strapi.log.info('Application successfully restarted');
});
2015-11-30 12:08:47 +01:00
if (cluster.isMaster) {
2015-12-02 17:09:09 +01:00
_.forEach(cluster.workers, worker => worker.on('message', () => self.emit('restart:done')));
2015-11-30 12:08:47 +01:00
}
// Kill every worker processes.
2015-12-02 16:55:56 +01:00
_.forEach(cluster.workers, () => process.kill(process.pid, 'SIGHUP'));
});
2015-11-30 12:08:47 +01:00
// Reloading the router.
2015-12-02 17:09:09 +01:00
self.hooks.router.reload();
});
2015-11-30 12:08:47 +01:00
// Reloading the ORM.
2015-12-02 17:09:09 +01:00
self.hooks.waterline.reload();
});
});
};