Permalink
Browse files

fixed worker is still alive after executing

  • Loading branch information...
1 parent 2e0d496 commit 545751e5022f6a27b38ea932e6ed9e3c873e526d maneulyori committed Jan 14, 2013
Showing with 14 additions and 11 deletions.
  1. +7 −5 modules/eval.js
  2. +7 −6 modules/eval/worker.js
View
@@ -7,7 +7,7 @@ exports.init = init;
exports.messageHandler = messageHandler;
cluster.setupMaster({
- exec : "eval/worker.js",
+ exec : "modules/eval/worker.js",
args : process.argv.slice(2),
silent : false
});
@@ -25,14 +25,16 @@ function messageHandler(message)
{
var source = message.content.match(/([^\s]+)\ (.+)/)[2];
+ console.log("executing " + source);
+
//This will be fired when the forked process becomes online
cluster.on( "online", function(worker) {
var timer = 0;
worker.on( "message", function(msg) {
clearTimeout(timer); //The worker responded in under 1 seconds, clear the timeout
- console.log(msg);
- worker.destroy(); //Don't leave him hanging
+ client.privmsg(message.channel, msg);
+ worker.destroy(); //Don't leave him hanging
});
timer = setTimeout( function() {
@@ -41,8 +43,8 @@ function messageHandler(message)
}, 1000);
worker.send(source); //Send the code to run for the worker
-
- cluster.fork();
});
+
+ cluster.fork();
}
}
View
@@ -2,15 +2,16 @@ process.on('message', function(source) {
var vm = require("vm");
- var obj = {
- out: ""
- print: function(str) { out += str };
+ var sandbox = {
+ out: "",
+ print: function(str) { out += str }
};
- var ctx = vm.createContext(obj);
var script = vm.createScript(source);
- script.runInNewContext(ctx);
+ script.runInNewContext(sandbox);
- process.send(out); //Send the finished message to the parent process
+ process.send(sandbox.out); //Send the finished message to the parent process
+
+ process.exit();
});

0 comments on commit 545751e

Please sign in to comment.