Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Clean-up addLoop

* Added delay and callback arguments
* Made 'cluster' module mandatory
* This makes node 0.6 mandatory
  • Loading branch information...
commit b3d52d349b0ad2dfa7dc64b57078fcdbef646867 1 parent c60a0f5
@jadonk authored
Showing with 41 additions and 49 deletions.
  1. +41 −49 node_modules/bonescript/index.js
View
90 node_modules/bonescript/index.js
@@ -6,6 +6,7 @@ var child_process = require('child_process');
var http = require('http');
var url = require('url');
var path = require('path');
+var cluster = require('cluster');
var eeprom = require('./eeprom');
bone = require('./bone').bone;
@@ -26,7 +27,6 @@ var socketio = myrequire('socket.io', function() {
console.log("Dynamic web features not enabled");
});
-var cluster = myrequire('cluster');
var misc = myrequire('./build/Release/misc');
OUTPUT = exports.OUTPUT = "out";
@@ -379,28 +379,27 @@ getEeproms = exports.getEeproms = function(callback) {
return(eeproms);
};
-addLoop = exports.addLoop = function(loop) {
- var workers = [];
- if(typeof loop === "object") {
- for(var n = 0; n < loop.length; n++) {
- console.log('Adding loop ' + (n+1));
- workers[n] = cluster.fork();
- }
- } else if(typeof loop === "function") {
- workers[0] = cluster.fork();
- } else {
- return(false);
- }
+addLoop = exports.addLoop = function(myloop, delay, callback) {
+ delay = delay || 0;
+ callback = callback || function(){};
+ var worker = cluster.fork();
cluster.on('death', function(worker) {
console.log('Loop with PID ' + worker.pid + ' died');
});
process.on('SIGTERM', function() {
- for(var n = 0; n < workers.length; n++) {
- //console.log('Killing loop ' + (n+1));
- workers[n].kill();
- }
- process.exit(0);
+ worker.kill();
});
+ worker.on('message', function(m) {
+ if(m.getLoop) {
+ var message = {
+ myloop: myloop,
+ delay: delay,
+ callback: (callback?true:false)
+ };
+ worker.send(message);
+ } else if(m.callback) {
+ callback(m.callback);
+ }
return(true);
};
@@ -420,41 +419,34 @@ if(misc.exists) {
}
// This is where everything is meant to happen
-if(cluster.exists) {
- run = exports.run = function() {
- if(cluster.isMaster) {
- setup();
- if(typeof loop != 'undefined') addLoop(loop);
- } else {
- if(typeof loop === "function") {
- //console.log('Starting loop');
- var repeat = function repeat() {
- loop();
- process.nextTick(repeat);
- };
- repeat();
- } else if(typeof loop === "object") {
- //console.log('Starting loop ' + process.env.NODE_WORKER_ID);
- var repeat = function repeat() {
- loop[process.env.NODE_WORKER_ID-1]();
- process.nextTick(repeat);
- };
- repeat();
+run = exports.run = function(mysetup, myloop) {
+ mysetup = mysetup || setup || function(){};
+ myloop = myloop ||
+ ((typeof loop === "function") ? [ loop ] | false) ||
+ ((typeof loop === "object") ? loop | false) ||
+ [];
+ if(cluster.isMaster) {
+ mysetup();
+ for(var x in myloop) addLoop(myloop[x], 0);
+ } else {
+ process.send({getLoop: process.env.NODE_WORKER_ID});
+ process.on('message', function(m) {
+ console.log('Child got message ' + JSON.stringify(m));
+ var loopfunc = function() {};
+ try {
+ eval('loopfunc = ' + m.loop);
+ } catch(ex) {
+ console.error('Unable to eval loop in ' + JSON.stringify(m));
}
- }
- };
-} else {
- run = exports.run = function() {
- setup();
- if(typeof loop === "function") {
var repeat = function repeat() {
- loop();
- process.nextTick(repeat);
+ loopfunc();
+ if(m.delay) setTimeout(repeat, m.delay);
+ else process.nextTick(repeat);
};
repeat();
- }
- };
-}
+ });
+ }
+};
// This is a helper function for web servers
var loadFile = function(uri, subdir, res, type) {
Please sign in to comment.
Something went wrong with that request. Please try again.