added support for specifying which ports to use

if no port is specified in config.json port from url will be used if no
port in url then default to 4567

port in config.json can be an array or a single port number
v1.18.x
barisusakli 10 years ago
parent f67bc8d056
commit d62cdd5127

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var nconf = require('nconf'), var nconf = require('nconf'),
net = require('net'),
fs = require('fs'), fs = require('fs'),
url = require('url'), url = require('url'),
path = require('path'), path = require('path'),
@ -16,7 +15,6 @@ var nconf = require('nconf'),
output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }), output = logrotate({ file: __dirname + '/logs/output.log', size: '1m', keep: 3, compress: true }),
silent = process.env.NODE_ENV !== 'development', silent = process.env.NODE_ENV !== 'development',
numProcs, numProcs,
workers = [], workers = [],
Loader = { Loader = {
@ -153,6 +151,7 @@ Loader.addWorkerEvents = function(worker) {
}; };
Loader.start = function(callback) { Loader.start = function(callback) {
numProcs = getPorts().length;
console.log('Clustering enabled: Spinning up ' + numProcs + ' process(es).\n'); console.log('Clustering enabled: Spinning up ' + numProcs + ' process(es).\n');
for (var x=0; x<numProcs; ++x) { for (var x=0; x<numProcs; ++x) {
@ -165,15 +164,18 @@ Loader.start = function(callback) {
}; };
function forkWorker(index, isPrimary) { function forkWorker(index, isPrimary) {
var urlObject = url.parse(nconf.get('url')); var ports = getPorts();
var port = urlObject.port || nconf.get('port') || nconf.get('PORT') || 4567;
if(!ports[index]) {
return console.log('[cluster] invalid port for worker : ' + index + ' ports: ' + ports.length);
}
var worker = fork('app.js', [], { var worker = fork('app.js', [], {
silent: silent, silent: silent,
env: { env: {
isPrimary: isPrimary, isPrimary: isPrimary,
isCluster: true, isCluster: true,
port: parseInt(port, 10) + index port: ports[index]
} }
}); });
@ -192,6 +194,15 @@ function forkWorker(index, isPrimary) {
} }
} }
function getPorts() {
var urlObject = url.parse(nconf.get('url'));
var port = nconf.get('port') || nconf.get('PORT') || urlObject.port || 4567;
if (!Array.isArray(port)) {
port = [port];
}
return port;
}
Loader.restart = function(callback) { Loader.restart = function(callback) {
killWorkers(); killWorkers();
@ -233,9 +244,6 @@ nconf.argv().file({
file: path.join(__dirname, '/config.json') file: path.join(__dirname, '/config.json')
}); });
numProcs = nconf.get('cluster') || 1;
numProcs = (numProcs === true) ? require('os').cpus().length : numProcs;
if (nconf.get('daemon') !== false) { if (nconf.get('daemon') !== false) {
if (fs.existsSync(pidFilePath)) { if (fs.existsSync(pidFilePath)) {
try { try {

Loading…
Cancel
Save