Comparing version 0.0.2 to 0.0.3
98
index.js
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -39,3 +41,3 @@ var zmq = require('zmq'); | ||
var Node = function(opt) { | ||
var Node = function (opt) { | ||
opt = opt || {}; | ||
@@ -89,2 +91,6 @@ | ||
Node.prototype.getClusterTopology = function () { | ||
return this._clusterTopology; | ||
}; | ||
Node.prototype.inCluster = function () { | ||
@@ -139,3 +145,3 @@ return this._inCluster; | ||
], function (err, result) { | ||
], function (err) { | ||
if (err) { | ||
@@ -150,3 +156,3 @@ return this._error(err, callback); | ||
this._emitter.emit('join', this._cluster); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._cluster); }); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._cluster); }.bind(this)); | ||
@@ -160,16 +166,38 @@ }.bind(this)); | ||
Node.prototype.leave = function (callback) { | ||
this._sub.removeAllListeners(); | ||
this._sub.close(); | ||
this._pub.close(); | ||
var that = this; | ||
this._sub = null; | ||
this._pub = null; | ||
async.series([ | ||
function (next) { | ||
that._stopDiscovery(next); | ||
}, | ||
function (next) { | ||
if (that._advertising) { | ||
that.stopAdvertise(next); | ||
} else { | ||
next(); | ||
} | ||
}, | ||
function (next) { | ||
that._sub.removeAllListeners(); | ||
that._sub.close(); | ||
that._pub.close(); | ||
this._inCluster = false; | ||
that._sub = null; | ||
that._pub = null; | ||
// callback + emit | ||
this._emitter.emit('leave', this._cluster); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._cluster); }); | ||
that._inCluster = false; | ||
return this; | ||
next(); | ||
} | ||
], function (err) { | ||
if (err) { | ||
return that._error(err, callback); | ||
} | ||
// callback + emit | ||
that._emitter.emit('leave', that._cluster); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, that._cluster); }.bind(that)); | ||
}); | ||
return that; | ||
}; | ||
@@ -192,3 +220,3 @@ | ||
}; | ||
// mix the details with the banner, so that they also get advertised | ||
@@ -212,3 +240,3 @@ mout.object.mixIn(banner.txtRecord, details); | ||
this._emitter.emit('advertise_start', this._adInfo); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._adInfo); }); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._adInfo); }.bind(this)); | ||
@@ -229,3 +257,3 @@ }.bind(this)); | ||
this._emitter.emit('advertise_stop', this._adInfo); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._adInfo); }); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, this._adInfo); }.bind(this)); | ||
@@ -240,6 +268,8 @@ return this; | ||
this._assertChanValid(channel, callback); | ||
this._sub.subscribe(channel); | ||
this._emitter.emit('subscribe', channel); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, channel); }); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, channel); }.bind(this)); | ||
@@ -257,3 +287,3 @@ return this; | ||
this._emitter.emit('unsubscribe', channel); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, channel); }); | ||
if (typeof(callback) === 'function') process.nextTick(function () { callback(null, channel); }.bind(this)); | ||
@@ -268,5 +298,7 @@ return this; | ||
this._assertChanValid(channel); | ||
this._emitter.emit('publish', channel, payload); | ||
this._pub.send(channel + ':' + payload) | ||
this._pub.send(channel + ':' + payload); | ||
@@ -346,4 +378,5 @@ return this; | ||
// if node already in cluster or belongs to other cluster, ignore | ||
if (!mout.lang.isObject(this._clusterTopology[service.name]) | ||
&& service.txtRecord.cluster === this._cluster) { | ||
if (!mout.lang.isObject(this._clusterTopology[service.name]) && | ||
service.txtRecord.cluster === this._cluster) { | ||
// add node to this node's perception of the cluster | ||
@@ -354,4 +387,8 @@ var info = { | ||
address: service.addresses[0], | ||
port: service.port, | ||
port: service.port | ||
}; | ||
info.details = mout.lang.deepClone(service.txtRecord); | ||
delete info.details.cluster; | ||
this._clusterTopology[service.name] = info; | ||
@@ -387,9 +424,18 @@ | ||
return 'tcp://' + addr + ':' + port; | ||
} | ||
}; | ||
Node.prototype._assertChanValid = function (channel, callback) { | ||
if (channel.indexOf(':') > -1) { | ||
this._error(new Error('Can\'t use commas in channel names'), callback); | ||
} | ||
}; | ||
Node.prototype._error = function (err, callback) { | ||
// note that the error event is only thrown if a callback was not provided | ||
if (typeof(callback) === 'function') callback(err); | ||
else this._emitter.emit('error', err); | ||
} | ||
if (typeof(callback) === 'function') { | ||
return callback(err); | ||
} | ||
this._emitter.emit('error', err); | ||
}; | ||
@@ -396,0 +442,0 @@ |
{ | ||
"name": "1", | ||
"version": "0.0.2", | ||
"description": "Clustering module based on 0MQ", | ||
"version": "0.0.3", | ||
"description": "distributed message queue based on ØMQ", | ||
"main": "index.js", | ||
@@ -29,3 +29,7 @@ "scripts": { | ||
"async": "~0.1.22" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.8.1", | ||
"expect.js": "~0.2.0" | ||
} | ||
} |
# 1 ( *One* ) | ||
Distributed message queue based on [ØMQ](http://www.zeromq.org/). | ||
Clustering module based on [ØMQ](http://www.zeromq.org/). | ||
-------------------------------------------------------------------------------- | ||
## Introduction | ||
*1* is a sort of magnet module, gluing together all the nodes that you launch | ||
in a network, and providing a channel based pub/sub. | ||
## Getting started | ||
@@ -16,2 +21,12 @@ | ||
The first thing to do, is to install ØMQ. Head to | ||
[http://www.zeromq.org/intro:get-the-software](http://www.zeromq.org/intro:get-the-software) | ||
and follow the instructions for your operating system. Then, use | ||
the instructions below, once again, depending on your operating system. | ||
Also, you might want to tune your OS in order to solve some known | ||
issues with default configurations. To do this, head out to | ||
[http://www.zeromq.org/docs:tuning-zeromq](http://www.zeromq.org/docs:tuning-zeromq), | ||
and follow the instructions. | ||
**Note:** If you are installing on a system that is not covered by these | ||
@@ -18,0 +33,0 @@ instructions, and manage to install, please share your instructions, so we can |
TODO | ||
==== | ||
- prevent from using channels with ':' | ||
- emit error events | ||
- prevent from using channels with ':' |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19349
404
49
1
2