Permalink
Browse files

using a more round-robin approach to scheduling.

  • Loading branch information...
1 parent 72d2888 commit b8da1273170af70514362fa09a9685ee0e2036c7 Joachim Kainz committed Oct 6, 2011
Showing with 46 additions and 19 deletions.
  1. +12 −11 examples/multi-master.js
  2. +14 −1 examples/multi-worker.js
  3. +17 −4 lib/backgrounder.js
  4. +3 −3 package.json
View
@@ -1,16 +1,17 @@
var backgrounder = require("../lib/backgrounder");
var worker = backgrounder.spawn(__dirname + "/multi-worker.js", {
"children-count" : 5
-});
+}, function(){
+ console.log("Master: Config finished,");
+ var counter = 0;
-var counter = 0;
+ for(var idx=0; idx<100; idx++) {
+ worker.send({}, function(arg1, arg2, arg3, arg4, arg5) {
+ console.log("Master: client called the callback with %s arguments:",
+ arguments.length, arg1, arg2, arg3, arg4, arg5);
+ if (++counter === 100) {
+ worker.terminate();
+ }
+ });
+}});
-for(var idx=0; idx<10; idx++) {
- worker.send({}, function(arg1, arg2, arg3) {
- console.log("Master: client called the callback with %s arguments:",
- arguments.length, arg1, arg2, arg3);
- if (++counter === 10) {
- worker.terminate();
- }
- });
-}
View
@@ -1,5 +1,14 @@
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) {
+ callback();
+});
process.on('message', function(message, callback) {
var options = {
@@ -9,10 +18,14 @@ process.on('message', function(message, callback) {
};
http.get(options, function(res) {
- callback(id, "status-code:", res.statusCode);
+ callback(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: Started %s!', id);
View
@@ -167,6 +167,7 @@ function Manager(module, config) {
this.emitter = new events.EventEmitter();
this.children = [];
this.pending = [];
+ this.lastUsed = -1;
var self = this;
var count = getCount(config);
@@ -186,14 +187,26 @@ Manager.prototype.on = function(event, listener) {
//
// Pick a child that does not have any pending requests
//
-function getAvailableChild(children) {
- for(var idx in children) {
- var child = children[idx];
+function getAvailableChild(manager) {
+ for(var idx = manager.lastUsed+1; idx < manager.children.length; idx++) {
+ var child = manager.children[idx];
if (!child.pending) {
+ manager.lastUsed = idx;
return child;
}
}
+
+ for(var idx = .0; idx < manager.children.length; idx++) {
+ var child = manager.children[idx];
+
+ if (!child.pending) {
+ manager.lastUsed = idx;
+ return child;
+ }
+ }
+
+ manager.lastUsed = -1;
return undefined;
}
//
@@ -205,7 +218,7 @@ Manager.prototype.send = function(message, callback) {
"content": message
};
- var child = getAvailableChild(this.children);
+ var child = getAvailableChild(this);
if (child) {
sendMessage(child, _message, callback);
View
@@ -1,7 +1,7 @@
{
"name": "backgrounder",
- "description": "yet another library for spawning multiple independent background workers as well as multiple pool of workers, v0.5 fork API to communicate with them; there is also communications API based on callbacks",
- "version": "0.2.3",
+ "description": "yet another library for spawning multiple independent background workers as well as multiple pool of workers, v0.5 fork API to communicate with them; the library also supports callback between processes",
+ "version": "0.2.4",
"homepage": "http://jolira.github.com/backgrounder",
"author": "Joachim Kainz <info@jolira.com>",
"contributors": [{
@@ -18,6 +18,6 @@
"node": ">= 0.4.9"
},
"dependencies": {
- "underscore": ">= 1.1.7"
+ "underscore": "1.1.7"
}
}

0 comments on commit b8da127

Please sign in to comment.