From d4ed3117f341352e99e7b1a75d114fc6516aca4c Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 3 Oct 2014 23:07:07 -0700 Subject: [PATCH 1/2] enable async or sync work modes --- lib/cylon.js | 7 +++++++ lib/robot.js | 32 ++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/cylon.js b/lib/cylon.js index 46c2d77..4b406bf 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -78,6 +78,13 @@ Cylon.start = function start() { for (var bot in this.robots) { this.robots[bot].start(); } + var mode = Utils.fetch(Config, 'workMode', 'async'); + if (mode === 'sync') { + console.log('sync'); + for (var bot in this.robots) { + this.robots[bot].startWork(); + } + } }; // Public: Sets the internal configuration, based on passed options diff --git a/lib/robot.js b/lib/robot.js index dd1326a..06e6933 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -240,20 +240,17 @@ Robot.prototype.start = function() { return this; } - var begin = function(callback) { - Logger.info('Working.'); - - this.emit('ready', this); - this.work.call(this, this); - this.running = true; - - callback(null, true); - }.bind(this); + var mode = Utils.fetch(Config, 'workMode', 'async'); Async.series([ this.startConnections, this.startDevices, - begin + function(callback) { + if (mode === 'async') { + this.startWork(); + } + callback(null, true); + }.bind(this) ], function(err) { if (!!err) { Logger.fatal("An error occured while trying to start the robot:"); @@ -268,6 +265,21 @@ Robot.prototype.start = function() { return this; }; +// Public: Starts the Robot's work and triggers a callback +// +// callback - callback function to be triggered +// +// Returns nothing +Robot.prototype.startWork = function() { + Logger.info('Working.'); + + this.emit('ready', this); + this.work.call(this, this); + this.running = true; + + //callback(null, true); +}; + // Public: Starts the Robot's connections and triggers a callback // // callback - callback function to be triggered From b80ad6e167606a46192613006aa5d7542bbd88dd Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Sat, 4 Oct 2014 00:56:08 -0700 Subject: [PATCH 2/2] start the robots in parallel --- lib/cylon.js | 18 ++++++++++-------- lib/robot.js | 9 +++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/cylon.js b/lib/cylon.js index 4b406bf..afcc374 100644 --- a/lib/cylon.js +++ b/lib/cylon.js @@ -75,16 +75,18 @@ Cylon.api = function api() { // // Returns nothing Cylon.start = function start() { + var starters = []; for (var bot in this.robots) { - this.robots[bot].start(); - } - var mode = Utils.fetch(Config, 'workMode', 'async'); - if (mode === 'sync') { - console.log('sync'); - for (var bot in this.robots) { - this.robots[bot].startWork(); - } + starters.push(this.robots[bot].start); } + Async.parallel(starters, function(err, results) { + var mode = Utils.fetch(Config, 'workMode', 'async'); + if (mode === 'sync') { + for (var bot in this.robots) { + this.robots[bot].startWork(); + } + } + }.bind(this)); }; // Public: Sets the internal configuration, based on passed options diff --git a/lib/robot.js b/lib/robot.js index 06e6933..1d2d12e 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -235,7 +235,7 @@ Robot.prototype.initDevices = function(devices) { // Starts the connections, devices, and work. // // Returns the result of the work -Robot.prototype.start = function() { +Robot.prototype.start = function(callback) { if (this.running) { return this; } @@ -251,7 +251,7 @@ Robot.prototype.start = function() { } callback(null, true); }.bind(this) - ], function(err) { + ], function(err, results) { if (!!err) { Logger.fatal("An error occured while trying to start the robot:"); Logger.fatal(err); @@ -260,6 +260,9 @@ Robot.prototype.start = function() { } this.emit('error', err); } + if (typeof(callback) === 'function') { + callback(err, results); + } }.bind(this)); return this; @@ -276,8 +279,6 @@ Robot.prototype.startWork = function() { this.emit('ready', this); this.work.call(this, this); this.running = true; - - //callback(null, true); }; // Public: Starts the Robot's connections and triggers a callback