Skip to content
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
23 changes: 12 additions & 11 deletions lib/compute-cluster.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const
util = require('util'),
path = require('path'),
fs = require('fs'),
child_process = require('child_process'),
events = require('events');
EventEmitter = require('events').EventEmitter,
os = require('os');

// decaying factor for heurstics calculating how much work costs.
const MAX_HISTORY = 100;
Expand All @@ -12,7 +14,7 @@ function ComputeCluster(options) {
throw "missing required 'module' argument";
}
// is module a file?
if (!path.existsSync(options.module)) {
if (!fs.existsSync(options.module)) {
throw "module doesn't exist: " + options.module;
}
if (options.max_processes &&
Expand All @@ -26,11 +28,11 @@ function ComputeCluster(options) {
throw "when provided, max_request_time must be a number";
}

events.EventEmitter.call(this);
EventEmitter.call(this);

// an array of child processes
this._kids = {};
this._MAX_KIDS = (options.max_processes || Math.ceil(require('os').cpus().length * 1.25));
this._MAX_KIDS = (options.max_processes || Math.ceil(os.cpus().length * 1.25));
this._work_q = [];
this._exiting = false;
this._exit_cb;
Expand All @@ -43,7 +45,7 @@ function ComputeCluster(options) {
this._jobs_run = 0;
};

util.inherits(ComputeCluster, events.EventEmitter);
util.inherits(ComputeCluster, EventEmitter);

ComputeCluster.prototype._onWorkerExit = function(pid) {
var self = this;
Expand Down Expand Up @@ -72,8 +74,8 @@ ComputeCluster.prototype._getEnvForWorker = function() {
env[i] = process.env[i];
}

delete env.NODE_WORKER_ID; //Node.js cluster worker marker for v0.6
delete env.NODE_UNIQUE_ID; //Node.js cluster worker marker for v0.7
delete env.NODE_WORKER_ID; // Node.js cluster worker marker for v0.6
delete env.NODE_UNIQUE_ID; // Node.js cluster worker marker for v0.7

return env;
};
Expand All @@ -85,7 +87,7 @@ ComputeCluster.prototype._getFreeWorker = function() {
if (!this._kids[i].job) return this._kids[i];
}

// no workers! can we spawn one?
// no workers! can we spawn one?
if (Object.keys(this._kids).length < this._MAX_KIDS) {
var k = {
worker: child_process.fork(
Expand Down Expand Up @@ -136,8 +138,8 @@ ComputeCluster.prototype._runWorkOnWorker = function(work, worker) {
worker.job = work;
};

// assign as many work units from work_q as possible to avialable
// compute processes
// assign as many work units from work_q as possible to avialable
// compute processes
ComputeCluster.prototype._assignWork = function() {
while (this._work_q.length > 0) {
var worker = this._getFreeWorker();
Expand Down Expand Up @@ -197,4 +199,3 @@ ComputeCluster.prototype.exit = function(cb) {
};

module.exports = ComputeCluster;

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
"engines": {
"node": ">= 0.6.2"
},
"dependencies": {
"devDependencies": {
"vows": "0.6.0"
},
"devDependencies": {},
"scripts": {
"test": "vows"
},
Expand Down