diff --git a/fixtures/slave.js b/fixtures/slave.js index c0d0840..823b049 100644 --- a/fixtures/slave.js +++ b/fixtures/slave.js @@ -15,7 +15,7 @@ var app = http.createServer(function (req, res) { return res.end(process.env.CFORK_ENV_TEST); } if (req.url === '/worker_index') { - return res.end(`worker index: ${process.env.CFORK_WORKER_INDEX}`); + return res.end(`slave worker index: ${process.env.CFORK_SLAVE_WORKER_INDEX}, ${process.env.CFORK_SLAVE_WORKER_COUNT}`); } res.end(req.method + ' ' + req.url); }).listen(port); diff --git a/fixtures/worker.js b/fixtures/worker.js index a05bc67..8fc59ac 100644 --- a/fixtures/worker.js +++ b/fixtures/worker.js @@ -25,7 +25,7 @@ var app = http.createServer(function (req, res) { return res.end(process.env.CFORK_ENV_TEST); } if (req.url === '/worker_index') { - return res.end(`worker index: ${process.env.CFORK_WORKER_INDEX}`); + return res.end(`worker index: ${process.env.CFORK_WORKER_INDEX}, ${process.env.CFORK_WORKER_COUNT}`); } res.end(req.method + ' ' + req.url); }).listen(port); diff --git a/index.js b/index.js index 78721d9..b12821f 100644 --- a/index.js +++ b/index.js @@ -169,7 +169,7 @@ function fork(options) { }); for (var i = 0; i < count; i++) { - const env = { CFORK_WORKER_INDEX: String(i) }; + const env = { CFORK_WORKER_INDEX: String(i), CFORK_WORKER_COUNT: count }; newWorker = forkWorker(null, env); newWorker._clusterSettings = cluster.settings; newWorker._clusterWorkerEnv = env; @@ -181,7 +181,7 @@ function fork(options) { slaves.map(normalizeSlaveConfig) .forEach(function(settings, index) { if (settings) { - const env = { CFORK_WORKER_INDEX: String(count + index) }; + const env = { CFORK_SLAVE_WORKER_INDEX: String(index), CFORK_SLAVE_WORKER_COUNT: slaves.length }; newWorker = forkWorker(settings, env); newWorker._clusterSettings = settings; newWorker._clusterWorkerEnv = env; diff --git a/test/cfork.test.js b/test/cfork.test.js index e49e2d0..f6cadf6 100644 --- a/test/cfork.test.js +++ b/test/cfork.test.js @@ -93,7 +93,7 @@ describe('test/cfork.test.js', () => { resp.statusCode.should.equal(200); urllib.request('http://localhost:1985/worker_index', function (err, body, resp) { should.ifError(err); - body.toString().should.equal('worker index: 4'); + body.toString().should.equal('slave worker index: 0, 1'); resp.statusCode.should.equal(200); done(); }); @@ -107,13 +107,13 @@ describe('test/cfork.test.js', () => { should.ifError(err); const text = body.toString(); // console.log('%o', text); - assert(text === 'worker index: 0' || text === 'worker index: 1' - || text === 'worker index: 2' || text === 'worker index: 3', text); + assert(text === 'worker index: 0, 4' || text === 'worker index: 1, 4' + || text === 'worker index: 2, 4 ' || text === 'worker index: 3, 4', text); resp.statusCode.should.equal(200); urllib.request('http://localhost:1985/worker_index', function (err, body, resp) { should.ifError(err); const text = body.toString(); - assert.equal(text, 'worker index: 4'); + assert.equal(text, 'slave worker index: 0, 1'); resp.statusCode.should.equal(200); done(); });