strapi/lib/restart.js

115 lines
2.8 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)
*/
module.exports = function (cb) {
2015-11-09 15:10:53 +01:00
const self = this;
console.log();
// Update the Strapi status (might be used
// by the core or some hooks).
self.reloading = true;
// Async module loader to rebuild a
// dictionary of the application.
async.auto({
// Rebuild the dictionaries.
dictionaries: function (cb) {
self.on('hook:_config:reloaded', function () {
self.on('hook:_api:reloaded', function () {
cb();
});
self.hooks._api.reload();
});
2015-11-30 16:15:14 +01:00
2015-11-09 15:10:53 +01:00
self.hooks._config.reload();
}
},
// Callback.
function (err) {
// Just in case there is an error.
if (err) {
self.log.error('Impossible to reload the server');
self.log.error('Please restart the server manually');
self.stop();
}
// Tell the application the framework is reloading
// (might be used by some hooks).
self.reloading = true;
2015-11-30 12:08:47 +01:00
// Run adapters installation
if (cluster.isMaster) {
self.hooks.waterline.installation();
}
2015-11-30 12:08:47 +01:00
// Install new adapters
strapi.after('hook:waterline:installed', function () {
self.log.warn('Application is restarting...');
console.log();
// Teardown Waterline adapters and
// reload the Waterline ORM.
self.after('hook:waterline:reloaded', function () {
self.after('hook:router:reloaded', function () {
process.nextTick(function () {
cb();
});
2015-11-30 12:08:47 +01:00
// Update `strapi` status.
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) {
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
self.once('restart:done', function () {
strapi.log.info('Application successfully restarted');
});
2015-11-30 12:08:47 +01:00
if (cluster.isMaster) {
_.forEach(cluster.workers, function (worker) {
worker.on('message', function () {
self.emit('restart:done');
});
});
}
// Kill every worker processes.
_.forEach(cluster.workers, function () {
process.kill(process.pid, 'SIGHUP');
});
});
2015-11-30 12:08:47 +01:00
// Reloading the router.
self.hooks.router.reload();
});
2015-11-30 12:08:47 +01:00
// Reloading the ORM.
self.hooks.waterline.reload();
});
});
};