Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added CLUSTER_WORKER env var with the workers id

  • Loading branch information...
commit 26dce2531deb416b193fcfdd8dc5ff1f2f767d88 1 parent 132e18e
@tj tj authored
View
3  docs/api.md
@@ -115,6 +115,9 @@
// do something
}
+ Alternatively we can use the __CLUSTER_WORKER__ env var, populated with
+ the worker's id.
+
### Master#isMaster
`true` when the script is executed as master.
View
7 lib/master.js
@@ -451,16 +451,17 @@ Master.prototype.spawn = function(n){
*/
Master.prototype.spawnWorker = function(id){
- var worker = new Worker(this).spawn();
+ var worker;
// id given
if ('number' == typeof id) {
+ worker = new Worker(this).spawn(id)
this.children[id] = worker;
worker.id = id;
// generate an id
} else {
- var len = this.children.push(worker);
- worker.id = len - 1;
+ worker = new Worker(this).spawn(this.children.length);
+ this.children.push(worker);
}
// TODO: refactor
View
20 lib/worker.js
@@ -159,18 +159,30 @@ Worker.prototype.close = function(){
};
/**
- * Spawn the worker.
+ * Spawn the worker with the given `id`.
*
+ * @param {Number} id
* @return {Worker} for chaining
* @api private
*/
-Worker.prototype.spawn = function(){
+Worker.prototype.spawn = function(id){
var fds = binding.socketpair()
- , customFds = [fds[0]].concat(this.master.customFds);
+ , customFds = [fds[0]].concat(this.master.customFds)
+ , env = {};
+
+ // merge env
+ for (var key in process.env) {
+ env[key] = process.env[key];
+ }
+
+ this.id = env.CLUSTER_WORKER = id;
// spawn worker process
- this.proc = spawn(node, this.master.cmd, { customFds: customFds });
+ this.proc = spawn(
+ node
+ , this.master.cmd
+ , { customFds: customFds, env: env });
// unix domain socket for ICP + fd passing
this.sock = new net.Socket(fds[1], 'unix');
View
3  test/test.env.js
@@ -4,6 +4,7 @@
*/
var cluster = require('../')
+ , assert = require('assert')
, http = require('http');
require('./common');
@@ -18,8 +19,10 @@ cluster = cluster(server)
if (cluster.isMaster) {
process.env.FOO = 'bar';
+ assert.ok(!process.env.CLUSTER_WORKER);
} else {
process.env.FOO.should.equal('bar');
+ assert.ok(process.env.CLUSTER_WORKER);
}
cluster.on('listening', function(){
Please sign in to comment.
Something went wrong with that request. Please try again.