Skip to content

Commit

Permalink
Merge pull request #20 from ph0bos/updated-election
Browse files Browse the repository at this point in the history
increment version: 0.5.6
  • Loading branch information
ph0bos authored Nov 7, 2016
2 parents 7c0f5f8 + 904d8e0 commit 84320b4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 35 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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!');
});
Expand All @@ -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);
});

```
15 changes: 12 additions & 3 deletions example/leader-election.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
Expand All @@ -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);
});
});
});




18 changes: 1 addition & 17 deletions lib/LeaderElection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Expand All @@ -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'));
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.5",
"version": "0.5.6",
"description": "A Curator-esque framework for ZooKeeper",
"main": "index.js",
"keywords": [
Expand Down
104 changes: 104 additions & 0 deletions test/LeaderElection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

Expand All @@ -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);
});
Expand All @@ -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);

Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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);
});
Expand All @@ -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();
});
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 84320b4

Please sign in to comment.