Skip to content

Commit

Permalink
Merge pull request #19 from ph0bos/updated-election
Browse files Browse the repository at this point in the history
increment version: 0.5.5
  • Loading branch information
ph0bos authored Nov 4, 2016
2 parents 4627be6 + 51d42a2 commit 7c0f5f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 10 additions & 4 deletions example/leader-election.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict';

var async = require('async');

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');
Expand All @@ -29,11 +37,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);

27 changes: 9 additions & 18 deletions lib/LeaderElection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
};

Expand All @@ -63,7 +65,6 @@ function LeaderElection(client, path, id, callback) {

LeaderElection.prototype.start = function (callback) {
var self = this;

self._isWithdrawn = false;

if (self.znode) {
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 7c0f5f8

Please sign in to comment.