2015-11-05 14:25:36 +01:00
|
|
|
'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');
|
2015-11-05 14:25:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Programmatically restart the server
|
|
|
|
* (useful for the Studio)
|
|
|
|
*/
|
|
|
|
|
2015-12-02 17:09:09 +01:00
|
|
|
module.exports = cb => {
|
2015-11-09 15:10:53 +01:00
|
|
|
console.log();
|
|
|
|
|
|
|
|
// Update the Strapi status (might be used
|
|
|
|
// by the core or some hooks).
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.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.
|
2015-12-02 11:18:05 +01:00
|
|
|
dictionaries: cb => {
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.on('hook:_config:reloaded', () => {
|
|
|
|
strapi.on('hook:_api:reloaded', () => cb());
|
|
|
|
strapi.hooks._api.reload();
|
2015-11-10 16:29:34 +01:00
|
|
|
});
|
2015-11-30 16:15:14 +01:00
|
|
|
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.hooks._config.reload();
|
2015-11-09 15:10:53 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Callback.
|
2015-12-02 11:18:05 +01:00
|
|
|
err => {
|
2015-12-03 14:38:46 +01:00
|
|
|
let count = 0;
|
2015-11-09 15:10:53 +01:00
|
|
|
|
|
|
|
// Just in case there is an error.
|
|
|
|
if (err) {
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.log.error('Impossible to reload the server');
|
|
|
|
strapi.log.error('Please restart the server manually');
|
|
|
|
strapi.stop();
|
2015-11-09 15:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the application the framework is reloading
|
|
|
|
// (might be used by some hooks).
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.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-17 22:42:19 +01:00
|
|
|
strapi.hooks.waterline.installation();
|
2015-12-03 13:21:59 +01:00
|
|
|
|
2015-12-03 14:38:46 +01:00
|
|
|
++count;
|
2015-12-03 13:21:59 +01:00
|
|
|
|
2015-12-17 22:42:19 +01:00
|
|
|
if (_.isPlainObject(strapi.config.views) && !_.isBoolean(strapi.config.views)) {
|
|
|
|
strapi.hooks.views.installation();
|
2015-12-03 13:21:59 +01:00
|
|
|
|
2015-12-03 14:38:46 +01:00
|
|
|
++count;
|
2015-12-03 13:21:59 +01:00
|
|
|
}
|
2015-11-30 12:08:47 +01:00
|
|
|
}
|
2015-11-10 16:29:34 +01:00
|
|
|
|
2015-12-03 14:38:46 +01:00
|
|
|
const installed = _.after(count, () => {
|
|
|
|
if (_.isPlainObject(strapi.config.reload) && !_.isEmpty(strapi.config.reload) && strapi.config.reload.workers > 0) {
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.log.warn('Application is restarting...');
|
2015-12-03 14:38:46 +01:00
|
|
|
console.log();
|
|
|
|
}
|
2015-11-30 12:08:47 +01:00
|
|
|
|
|
|
|
// Teardown Waterline adapters and
|
|
|
|
// reload the Waterline ORM.
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.after('hook:waterline:reloaded', () => {
|
|
|
|
strapi.after('hook:router:reloaded', () => {
|
2015-12-02 16:55:56 +01:00
|
|
|
process.nextTick(() => cb());
|
2015-11-10 16:29:34 +01:00
|
|
|
|
2015-11-30 12:08:47 +01:00
|
|
|
// Update `strapi` status.
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.reloaded = true;
|
|
|
|
strapi.reloading = false;
|
2015-11-10 16:29:34 +01:00
|
|
|
|
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-17 22:42:19 +01:00
|
|
|
strapi.log.info('Application\'s dictionnary updated');
|
|
|
|
strapi.log.warn('You still need to restart your server to fully enjoy changes...');
|
2015-11-30 12:08:47 +01:00
|
|
|
}
|
2015-11-23 17:36:59 +01:00
|
|
|
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.once('restart:done', function () {
|
2015-11-30 12:08:47 +01:00
|
|
|
strapi.log.info('Application successfully restarted');
|
2015-11-23 17:36:59 +01:00
|
|
|
});
|
|
|
|
|
2015-11-30 12:08:47 +01:00
|
|
|
if (cluster.isMaster) {
|
2015-12-17 22:42:19 +01:00
|
|
|
_.forEach(cluster.workers, worker => worker.on('message', () => strapi.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-10 16:29:34 +01:00
|
|
|
});
|
2015-11-30 12:08:47 +01:00
|
|
|
|
|
|
|
// Reloading the router.
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.hooks.router.reload();
|
2015-11-10 16:29:34 +01:00
|
|
|
});
|
|
|
|
|
2015-11-30 12:08:47 +01:00
|
|
|
// Reloading the ORM.
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.hooks.waterline.reload();
|
2015-11-10 16:29:34 +01:00
|
|
|
});
|
2015-12-03 13:21:59 +01:00
|
|
|
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.after('hook:waterline:installed', () => {
|
2015-12-03 13:21:59 +01:00
|
|
|
installed();
|
|
|
|
});
|
|
|
|
|
2015-12-17 22:42:19 +01:00
|
|
|
strapi.after('hook:views:installed', () => {
|
2015-12-03 13:21:59 +01:00
|
|
|
installed();
|
|
|
|
});
|
2015-11-05 14:25:36 +01:00
|
|
|
});
|
|
|
|
};
|