Skip to content

Commit

Permalink
adding a proccess.id for workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Kainz committed Oct 12, 2011
1 parent 2322495 commit 60e4778
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
8 changes: 3 additions & 5 deletions examples/multi-worker.js
@@ -1,12 +1,12 @@
var http = require('http');
var id = process.pid;
var loaded = 0;

/**
* There really is no configuration here, but it is essential that we are calling the callback functtion.
* Failing to call the callback function will prevent the master from confinuing
*/
process.on('config', function(message, callback) {
console.log('Worker: Configured %s!', process.id);
callback();
});

Expand All @@ -18,14 +18,12 @@ process.on('message', function(message, callback) {
};

http.get(options, function(res) {
callback(id, "status-code:", res.statusCode, "loaded", ++loaded);
callback(process.id, "status-code:", res.statusCode, "loaded", ++loaded);
}).on('error', function(e) {
callback("error:", e);
});
});

process.on('terminate', function(message, callback) {
console.log("Worker: %s Loaded", id, ++loaded);
console.log("Worker: %s Loaded", process.id, ++loaded);
});

console.log('Worker: Started %s!', id);
4 changes: 4 additions & 0 deletions lib/backgrounder-launcher.js
Expand Up @@ -116,6 +116,10 @@ function processMessage(message) {
var parsed = JSON.parse(message);
var callback = getCallback(parsed.id);

if ("config" === parsed.type) {
process.id = parsed.clientID;
}

emitter.emit(parsed.type, parsed.content, callback);

if ("terminate" === parsed.type) {
Expand Down
20 changes: 10 additions & 10 deletions lib/backgrounder.js
Expand Up @@ -217,16 +217,16 @@ function startChild(manager, module, config, idx, callback) {
child.process.stdin.on('close', function(code, signal) {
child.restart();
});
if (config) {
sendMessage(manager, idx, {
"type": "config",
"content": config
}, function(){
if (callback) {
callback.apply(null, arguments);
}
});
}
var _config = config || {};
sendMessage(manager, idx, {
"clientID" : idx,
"type": "config",
"content": _config
}, function(){
if (callback) {
callback.apply(null, arguments);
}
});
return child;
};
starter(callback);
Expand Down

0 comments on commit 60e4778

Please sign in to comment.