Skip to content

Commit

Permalink
dedicated processes strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
pofider committed May 29, 2015
1 parent 54a908c commit d5f648c
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 104 deletions.
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@

module.exports = function(options) {
return new (require("./lib/manager.js"))(options);
var options = options || {};
options.timeout = options.timeout || 10000;
options.strategy = options.strategy || "http-servers";

if (options.strategy === "http-servers") {
return new (require("./lib/manager-servers.js"))(options);
}

if (options.strategy === "dedicated-process") {
return new (require("./lib/manager-processes.js"))(options);
}

throw new Error("Unsupported scripts manager strategy: " + options.strategy);
};

module.exports.ScriptManager = require("./lib/manager.js");
module.exports.ScriptManager = require("./lib/manager-servers.js");
module.exports.ScriptManagerOnHttpServers = module.exports.ScriptManager;

module.exports.ScriptManagerOnProcesses = require("./lib/manager-processes.js");
81 changes: 81 additions & 0 deletions lib/manager-processes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var childProcess = require("child_process");
var path = require("path");
var _ = require("underscore");
var S = require("string");

var ScriptsManager = module.exports = function (options) {
this.options = options;
this.options.timeout = this.options.timeout || 10000;
};


ScriptsManager.prototype.start = function (cb) {
cb();
};

ScriptsManager.prototype.ensureStarted = function (cb) {
cb();
};

ScriptsManager.prototype.execute = function (inputs, options, cb) {
var self = this;
var isDone = false;

//fix freeze during debugging
process.execArgv = _.filter(process.execArgv, function (arg) {
return !S(arg).startsWith("--debug");
});

process.execArgv.push("--expose-gc");

var worker = childProcess.fork(path.join(__dirname, "worker-processes.js"));

worker.on('message', function (m) {


if (m.error) {
isDone = true;
return cb(new Error(m.error));
}

if (m.action === "process-response") {
isDone = true;
return cb(null, m.value);
}

if (m.action === "callback") {
m.params.push(function(){
var args = Array.prototype.slice.call(arguments);
if (args.length && args[0]) {
args[0] = args[0].message;
}
worker.send({
action: "callback-response",
params: args
})
});
options.callback.apply(self, m.params);
//isDone = true;
//return cb(null, m);
}
});

worker.send({
inputs: inputs,
options: options
});

setTimeout(function () {
if (isDone)
return;

worker.kill();

cb(new Error("Timeout error during rendering"));
}, options.timeout || this.options.timeout);
};

ScriptsManager.prototype.kill = function () {
};


6 changes: 3 additions & 3 deletions lib/manager.js → lib/manager-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ var findFreePortInRange = function (host, portLeftBoundary, portRightBoundary, c
};

var ScriptsManager = module.exports = function (options) {
this.options = options || {};
this.options.numberOfWorkers = this.options.numberOfWorkers || 1;
this.options = options;
this.options.timeout = this.options.timeout || 10000;
this.options.numberOfWorkers = this.options.numberOfWorkers || 1;
this.options.host = this.options.host || "127.0.0.1";
this._runningRequests = [];

Expand Down Expand Up @@ -82,7 +82,7 @@ ScriptsManager.prototype.start = function (cb) {

process.execArgv.push("--expose-gc");

self.workersCluster = childProcess.fork(path.join(__dirname, "worker.js"), []);
self.workersCluster = childProcess.fork(path.join(__dirname, "worker-servers.js"), []);
self.workersCluster.on("message", function (m) {
if (m.action === "running") {
self.isStarted = true;
Expand Down
45 changes: 45 additions & 0 deletions lib/worker-processes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
process.on('uncaughtException', function (err) {
process.send({
error: err.message,
errorStack: err.stack
});

process.exit();
});

var cb;

function callback() {
cb = arguments[arguments.length - 1];
var args = Array.prototype.slice.call(arguments);
args.pop();
process.send({action: "callback", pid: process.pid, params: args.sort()});
}


process.on('message', function (m) {
if (m.action === "callback-response") {
if (m.params.length) {
if (m.params[0]) {
m.params[0] = new Error(m.params[0]);
}
}
cb.apply(this, m.params);
}

require(m.options.execModulePath)(m.inputs, callback, function (err, val) {
if (err) {
process.send({
error: err.message,
errorStack: err.stack
});
} else {
process.send({
action: "process-response",
value: val
});
}

process.exit();
});
});
File renamed without changes.
Loading

0 comments on commit d5f648c

Please sign in to comment.