From c25c359895ec8131577d38446e6289982a0f08fc Mon Sep 17 00:00:00 2001 From: jonrobins Date: Fri, 4 Nov 2016 13:28:21 +0000 Subject: [PATCH 1/2] increment version: 0.5.5 - Fixed load issues --- example/leader-election.js | 9 +++++---- lib/LeaderElection.js | 27 +++++++++------------------ package.json | 2 +- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/example/leader-election.js b/example/leader-election.js index bb4982e..7d732cf 100644 --- a/example/leader-election.js +++ b/example/leader-election.js @@ -1,10 +1,13 @@ 'use strict'; +var async = require('async'); + var Zoologist = require('..').Zoologist; var LeaderElection = require('..').LeaderElection; var client = Zoologist.newClient('127.0.0.1:2181'); +client.setMaxListeners(1024); client.start(); var election = new LeaderElection(client, '/my/path', 'my-id'); @@ -29,11 +32,9 @@ election.on('error', function (err) { console.log('Error: ' + err); // Restart election listener. - election.start(function(err){ + election.start(function (err) { console.log("Election restarting!"); }); }); -setInterval(function () { - console.log(election.hasLeadership()); -}, 5000); + diff --git a/lib/LeaderElection.js b/lib/LeaderElection.js index c7aff08..89e1f13 100644 --- a/lib/LeaderElection.js +++ b/lib/LeaderElection.js @@ -33,6 +33,8 @@ util.inherits(LeaderElection, EventEmitter); function LeaderElection(client, path, id, callback) { var self = this; + self.watcher = function(){ self._watch() }; + self.client = client; self.path = path + '/' + id; self.znode = null; @@ -43,7 +45,7 @@ function LeaderElection(client, path, id, callback) { * Override the connection callback. */ self.connectionCallback = callback || function (err, node) { - if (err) log.error('connection error on start up', self.path); + if (err) log.error(err, 'connection error on start up', self.path); else log.debug('connection started', self.znode); }; @@ -63,7 +65,6 @@ function LeaderElection(client, path, id, callback) { LeaderElection.prototype.start = function (callback) { var self = this; - self._isWithdrawn = false; if (self.znode) { @@ -101,7 +102,7 @@ LeaderElection.prototype.start = function (callback) { self.znode = res[0].path.replace(self.path + '/', ''); self._watch(); - callback(err, res); + return callback(err, res); } }; @@ -159,19 +160,21 @@ LeaderElection.prototype.hasLeadership = function() { }; LeaderElection.prototype._watch = function () { + var self = this; + self.client.getClient().getChildren( self.path, self.watcher, getChildrenCallback); function getChildrenCallback(err, data) { // If we have withdrawn this election, just return; - if (self._isWithdrawn) return; + if (self._isWithdrawn || self.znode == undefined) return; // If the topology contains no nodes, then reset the election and error; - if(data !== undefined && data.length === 0){ + if( data == undefined || data.length === 0){ self._disconnect(); self.znode = null; + //log.error({err: err}, 'all nodes have gone'); self.emit('error', new Error("all nodes have gone")); - log.error({err: err}, 'all nodes have gone'); return; } @@ -248,18 +251,6 @@ LeaderElection.prototype._watch = function () { } } } - - self.client - .getClient() - .getChildren( - self.path, - function watcher(event) { - self._watch(); - self.client.getClient().getChildren(self.path, getChildrenCallback); - }, - function (err, data) { - getChildrenCallback(err, data); - }); }; LeaderElection.prototype._disconnect = function() { diff --git a/package.json b/package.json index e71bbf5..28f5d1f 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zoologist", - "version": "0.5.4", + "version": "0.5.5", "description": "A Curator-esque framework for ZooKeeper", "main": "index.js", "keywords": [ From 51d42a23064586833c4d9546f202d817314df203 Mon Sep 17 00:00:00 2001 From: jonrobins Date: Fri, 4 Nov 2016 13:54:45 +0000 Subject: [PATCH 2/2] increment version: 0.5.5 - Fixed load issues - updated readme --- README.md | 6 ++++++ example/leader-election.js | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/README.md b/README.md index e8e432e..8feb9c3 100755 --- a/README.md +++ b/README.md @@ -112,6 +112,12 @@ var Zoologist = require('..').Zoologist; var LeaderElection = require('..').LeaderElection; var client = Zoologist.newClient('127.0.0.1:2181'); + +/* + * This represents how many active elections you will need at one time. + * The default is 10. + */ +client.setMaxListeners(1024); client.start(); var election = new LeaderElection(client, '/my/path', 'my-id'); diff --git a/example/leader-election.js b/example/leader-election.js index 7d732cf..b0fd729 100644 --- a/example/leader-election.js +++ b/example/leader-election.js @@ -7,6 +7,11 @@ var Zoologist = require('..').Zoologist; var LeaderElection = require('..').LeaderElection; var client = Zoologist.newClient('127.0.0.1:2181'); + +/* + * This represents how many active elections you will need at one time. + * The default is 10. + */ client.setMaxListeners(1024); client.start();