diff --git a/README.md b/README.md index 8feb9c3..fd2cff0 100755 --- a/README.md +++ b/README.md @@ -107,13 +107,15 @@ serviceProvider.getInstance(function onInstanceReturn(err, data) { ```javascript '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. */ @@ -122,6 +124,10 @@ client.start(); var election = new LeaderElection(client, '/my/path', 'my-id'); +election.start(function(err, res){ + console.log(res); +}); + election.on('groupLeader', function () { console.log('I am the leader, watch me lead!'); }); @@ -140,21 +146,14 @@ election.on('topologyChange', function (data) { election.on('error', function (err) { console.log('Error: ' + err); - - // Restart election listener. - election.start(function(err){ - console.log("Election restarting!"); - }); -}); - -setInterval(function () { - console.log(election.hasLeadership()); -}, 5000); -setInterval(function () { election.withdraw(function(err){ - console.log("Withdrawing Election!"); + console.log("Withdrawn the election!"); + + election.start(function(err, res){ + console.log(res); + }); }); -}, 10000); +}); ``` diff --git a/example/leader-election.js b/example/leader-election.js index b0fd729..59f0d69 100644 --- a/example/leader-election.js +++ b/example/leader-election.js @@ -17,6 +17,10 @@ client.start(); var election = new LeaderElection(client, '/my/path', 'my-id'); +election.start(function(err, res){ + console.log(res); +}); + election.on('groupLeader', function () { console.log('I am the leader, watch me lead!'); }); @@ -36,10 +40,15 @@ election.on('topologyChange', function (data) { election.on('error', function (err) { console.log('Error: ' + err); - // Restart election listener. - election.start(function (err) { - console.log("Election restarting!"); + election.withdraw(function(err){ + console.log("Withdrawn the election!"); + + election.start(function(err, res){ + console.log(res); + }); }); }); + + diff --git a/lib/LeaderElection.js b/lib/LeaderElection.js index 89e1f13..0ec45b7 100644 --- a/lib/LeaderElection.js +++ b/lib/LeaderElection.js @@ -30,7 +30,7 @@ util.inherits(LeaderElection, EventEmitter); /** * */ -function LeaderElection(client, path, id, callback) { +function LeaderElection(client, path, id) { var self = this; self.watcher = function(){ self._watch() }; @@ -41,22 +41,6 @@ function LeaderElection(client, path, id, callback) { self._isGroupLeader = false; - /** - * Override the connection callback. - */ - self.connectionCallback = callback || function (err, node) { - if (err) log.error(err, 'connection error on start up', self.path); - else log.debug('connection started', self.znode); - }; - - if (self.client.started === true) { - self.start(self.connectionCallback); - } - - self.client.on('connected', function () { - self.start(self.connectionCallback); - }); - self.client.on('disconnected', function () { self._disconnect(); self.emit('error', new Error('disconnected from zk')); diff --git a/package.json b/package.json index 28f5d1f..1985885 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zoologist", - "version": "0.5.5", + "version": "0.5.6", "description": "A Curator-esque framework for ZooKeeper", "main": "index.js", "keywords": [ diff --git a/test/LeaderElection.test.js b/test/LeaderElection.test.js index 5b1cd26..20ba0f2 100644 --- a/test/LeaderElection.test.js +++ b/test/LeaderElection.test.js @@ -20,6 +20,10 @@ describe('LeaderElection', function() { var election = new LeaderElection(client, '/my/path', 'my-name'); + election.start(function(err, res){ + console.log("Start the election") + }); + election.on('groupLeader', function () { election.hasLeadership().should.be.true(); @@ -35,6 +39,18 @@ describe('LeaderElection', function() { var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election3 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + + election3.start(function(err, res){ + console.log("Start the election") + }); + election1.on('groupLeader', function() { leaders.push('Robert Baratheon'); }); @@ -105,6 +121,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log(res); + }); + + election2.start(function(err, res){ + console.log(res); + }); + election2.on('myLeader', function(leader) { leader.should.equal(election1.znode); @@ -119,6 +143,18 @@ describe('LeaderElection', function() { var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election3 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + + election3.start(function(err, res){ + console.log("Start the election") + }); + election2.on('myLeader', function(leader) { leaders.push(leader); }); @@ -142,6 +178,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + election1.on('myFollower', function(follower) { follower.should.equal(election2.znode); @@ -157,6 +201,18 @@ describe('LeaderElection', function() { var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election3 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + + election3.start(function(err, res){ + console.log("Start the election") + }); + election1.on('myFollower', function(follower) { followers.push(follower); }); @@ -179,6 +235,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log(res); + }); + + election2.start(function(err, res){ + console.log(res); + }); + election1.on('topologyChange', function(topology) { topology[0].should.equal(election1.znode); topology[1].should.equal(election2.znode); @@ -193,6 +257,18 @@ describe('LeaderElection', function() { var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election3 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + + election3.start(function(err, res){ + console.log("Start the election") + }); + election1.on('topologyChange', function(topology) { topologys.push(topology); }); @@ -215,6 +291,10 @@ describe('LeaderElection', function() { var election = new LeaderElection(client, '/my/path', 'my-name'); + election.start(function(err, res){ + console.log("Start the election") + }); + election.on('groupLeader', function() { client.close(); }); @@ -229,6 +309,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'pink-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + var leaders = []; election1.on('groupLeader', function() { @@ -252,6 +340,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + var errors = []; election1.on('error', function(err) { @@ -281,6 +377,14 @@ describe('LeaderElection', function() { var election1 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); var election2 = new LeaderElection(client, '/seven/kingdoms', 'iron-throne'); + election1.start(function(err, res){ + console.log("Start the election") + }); + + election2.start(function(err, res){ + console.log("Start the election") + }); + var errors = []; var leader;