2015-10-01 00:30:16 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-05 14:25:36 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
2015-10-01 00:30:16 +02:00
|
|
|
/**
|
|
|
|
* `strapi.prototype.initialize()`
|
|
|
|
*
|
|
|
|
* Start the Strapi server
|
|
|
|
* NOTE: `strapi.load()` should be run first.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function initialize(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
// Callback is optional.
|
|
|
|
cb = cb || function (err) {
|
|
|
|
if (err) {
|
|
|
|
self.log.error(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle `SIGUSR2` events.
|
|
|
|
process.once('SIGUSR2', function () {
|
|
|
|
self.stop(function () {
|
|
|
|
process.kill(process.pid, 'SIGUSR2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle `SIGINT` events.
|
|
|
|
process.on('SIGINT', function () {
|
|
|
|
self.stop(process.exit);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle `SIGTERM` events.
|
|
|
|
process.on('SIGTERM', function () {
|
|
|
|
self.stop(process.exit);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle `exit` events.
|
|
|
|
process.on('exit', function () {
|
|
|
|
if (!self._exiting) {
|
|
|
|
self.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure the configured port is not already used
|
|
|
|
// by an application or a service.
|
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
if (err.errno === 'EADDRINUSE') {
|
|
|
|
self.log.error('Port ' + self.config.port + ' already in use.');
|
|
|
|
self.stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-05 14:25:36 +01:00
|
|
|
if (cluster.isMaster) {
|
2015-11-10 16:29:34 +01:00
|
|
|
_.forEach(cluster.workers, function (worker) {
|
|
|
|
worker.on('message', function () {
|
|
|
|
self.emit('bootstrap:done');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only run the application bootstrap on master cluster if we don't have any workers.
|
|
|
|
// Else, run the bootstrap logic on the workers.
|
|
|
|
if ((cluster.isWorker && strapi.config.reload.workers > 0) || (cluster.isMaster && strapi.config.reload.workers < 1)) {
|
2015-11-05 14:25:36 +01:00
|
|
|
self.runBootstrap(function afterBootstrap(err) {
|
|
|
|
if (err) {
|
|
|
|
self.log.error('Bootstrap encountered an error.');
|
|
|
|
return cb(self.log.error(err));
|
|
|
|
}
|
2015-11-10 16:29:34 +01:00
|
|
|
|
|
|
|
if (cluster.isWorker) {
|
|
|
|
process.send('message');
|
|
|
|
} else {
|
|
|
|
self.emit('bootstrap:done');
|
|
|
|
}
|
2015-11-05 14:25:36 +01:00
|
|
|
});
|
|
|
|
}
|
2015-10-01 00:30:16 +02:00
|
|
|
|
2015-11-05 14:25:36 +01:00
|
|
|
// And fire the `ready` event.
|
|
|
|
// This is listened to by attached servers, etc.
|
|
|
|
self.emit('ready');
|
|
|
|
cb(null, self);
|
2015-10-01 00:30:16 +02:00
|
|
|
};
|