Skip to content

add force order option, and fix unit tests #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/compute-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function ComputeCluster(options) {
this._MAX_REQUEST_TIME = options.max_request_time || 0;
this._work_duration = 0;
this._jobs_run = 0;

//promise callback order
//this make sure all callback are called in order
this._promise = !! options.promise;
this._jobs_id = 0;
this._jobs_result = [];
this._jobs_result_index_start = 0;

};

util.inherits(ComputeCluster, events.EventEmitter);
Expand Down Expand Up @@ -108,20 +116,42 @@ ComputeCluster.prototype._getFreeWorker = function() {
}
};

ComputeCluster.prototype._promiseResult = function(order, result, callback) {
var cb, result, jobResult;
this._jobs_result[order - this._jobs_result_index_start] = {
result : result,
callback : callback
};
while (this._jobs_result[0]) {
jobResult = this._jobs_result.shift();
cb = jobResult.callback;
result = jobResult.result;

if (cb) cb(null, result);

this._jobs_result_index_start++;
}
};

ComputeCluster.prototype._runWorkOnWorker = function(work, worker) {
var self = this;
this.emit("debug", "passing compute job to process " + worker.worker.pid);
var startTime = new Date();
worker.worker.once('message', function(m) {
// clear the in-progress job
var cb = worker.job.cb;
var orderId = worker.job.id;
delete worker.job;

// start the next
self._assignWork();

// call our client's callback
if (cb) cb(null, m);
if (!self._promise) {
if (cb) cb(null, m);
} else {
self._promiseResult(orderId, m, cb);
}
// emit some debug info
var timeMS = (new Date() - startTime);
self.emit("debug", "process " + worker.worker.pid + " completed work in " +
Expand All @@ -139,6 +169,9 @@ ComputeCluster.prototype._runWorkOnWorker = function(work, worker) {
});
worker.worker.send(work.job);
worker.job = work;
// assign the order to the worker, so we know the order to send result
worker.job.id = this._jobs_id;
this._jobs_id++;
};

// assign as many work units from work_q as possible to avialable
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"node": ">= 0.6.2"
},
"dependencies": {
"vows": "0.6.0"
"vows": "0.7.0"
},
"devDependencies": {},
"scripts": {
Expand Down
62 changes: 62 additions & 0 deletions test/a-promise-order-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env node

const
vows = require('vows'),
assert = require('assert'),
computeCluster = require('../lib/compute-cluster'),
path = require('path'),
events = require('events');

var suite = vows.describe('basic tests');

// disable vows (often flakey?) async error behavior
suite.options.error = false;

suite.addBatch({
"allocation of a compute cluster": {
topic: function() {
return new computeCluster({
module: path.join(__dirname, 'workers', 'echo.js'),
promise: true
});
},
"runs without issue": function (cc) {
assert.isObject(cc);
},
"schedule many works, and check order": {
topic: function(cc) {
// cb = this.callback;
var index = 0;
var total = 4;
var self = this;
for (var i = 0; i < total; i++) {
cc.enqueue(i, function (err, r) {
assert.equal(r, index);
index++;
//console.log(index);
if (index === total) {
//cb({ err: err, cc: cc});
self.callback({ err: err, cc: cc})
}
});
}
},
"succeeds": function (r) {
assert.isNull(r.err);
},
"finally, exit": {
topic: function(r) {
r.cc.exit(this.callback);
this.callback(null);
},
"also succeeds": function(err) {
assert.isNull(err);
}
}
}
}
});

// run or export the suite.
if (process.argv[1] === __filename) suite.run();
else suite.export(module);
3 changes: 1 addition & 2 deletions test/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ suite.addBatch({
topic: function(cc) {
var cb = this.callback;
cc.enqueue("hello", function(e, r) {
cb.call(self, { cc: cc, r: r });
cb.call(null, { cc: cc, r: r });
});

},
"succeeds": function (r) {
assert.equal(r.r, 'hello');
Expand Down
2 changes: 1 addition & 1 deletion test/child-death-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ suite.addBatch({
topic: function(cc) {
var cb = this.callback;
cc.enqueue("hello", function(e, r) {
cb.call(self, { cc: cc, r: r });
cb.call(null, { cc: cc, r: r });
});

},
Expand Down
2 changes: 1 addition & 1 deletion test/child-env-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ suite.addBatch({
process.env['FOO'] = 'bar';
var cb = this.callback;
cc.enqueue("FOO", function(e, r) {
cb.call(self, { cc: cc, r: r });
cb.call(null, { cc: cc, r: r });
});
},
"succeeds": function (r) {
Expand Down
2 changes: 1 addition & 1 deletion test/information-output-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite.addBatch({
topic: function(cc) {
var cb = this.callback;
cc.enqueue("hello", function(e, r) {
cb.call(self, { cc: cc, r: r });
cb.call(null, { cc: cc, r: r });
});

},
Expand Down
2 changes: 1 addition & 1 deletion test/inside-nodecluster-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ suite.addBatch({
topic: function(cc) {
var cb = this.callback;
cc.enqueue("hello", function(e, r) {
cb.call(self, { cc: cc, r: r });
cb.call(null, { cc: cc, r: r });
});

},
Expand Down
1 change: 1 addition & 0 deletions test/maximum-backlog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ suite.addBatch({
"finally, exit": {
topic: function(r) {
r.cc.exit(this.callback);
this.callback(null);
},
"also succeeds": function(err) {
assert.isNull(err);
Expand Down
3 changes: 2 additions & 1 deletion test/maximum-duration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ suite.addBatch({
}
},
"fails": function (r) {
assert.ok(/cannot enqueue work: maximum expected work duration exceeded \(\d.\d+s\)/.test(r.err));
assert.ok(/cannot enqueue work: maximum expected work duration exceeded \(\d.\.?\d+s\)/.test(r.err));
},
"finally, exit": {
topic: function(r) {
r.cc.exit(this.callback);
this.callback(null);
},
"also succeeds": function(err) {
assert.isNull(err);
Expand Down