|
|
|
@ -9,10 +9,13 @@ var kestrel = function( options ){ |
|
|
|
|
|
|
|
this._settings = { |
|
|
|
servers: ['127.0.0.1:22133'], |
|
|
|
connectionType: types.ROUND_ROBIN |
|
|
|
connectionType: types.ROUND_ROBIN, |
|
|
|
reconnect: false, |
|
|
|
reconnectDelay: 200 |
|
|
|
}; |
|
|
|
this._currentConnection = 0; |
|
|
|
this._connections = []; |
|
|
|
this._openConnection = null; |
|
|
|
|
|
|
|
if( options instanceof Object ){ |
|
|
|
for( var key in options ){ |
|
|
|
@ -65,8 +68,15 @@ function _createConnection(port, host, self){ |
|
|
|
var connection = net.connect(port,host); |
|
|
|
|
|
|
|
connection.on('error', function(err){ |
|
|
|
console.dir(err); |
|
|
|
self._openConnection = null; |
|
|
|
self.emit('error', err); |
|
|
|
if(self._settings.reconnect){ |
|
|
|
setTimeout(function(){ |
|
|
|
self.connect(); |
|
|
|
},self._settings.reconnectDelay); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
connection.on('data', function(data){ |
|
|
|
_handleData(data,self); |
|
|
|
self.emit('data', data); |
|
|
|
@ -77,7 +87,8 @@ function _createConnection(port, host, self){ |
|
|
|
|
|
|
|
function _handleData(data, self){ |
|
|
|
data = data.toString(); |
|
|
|
|
|
|
|
console.log(data.substr(0,20)+'...') |
|
|
|
|
|
|
|
if( data.match('STORED\r\n') ){ |
|
|
|
self.emit('stored', true); |
|
|
|
if(self._pendingSetCallback){ |
|
|
|
@ -231,7 +242,7 @@ kestrel.prototype.set = function( queue, value, lifetime, callback){ |
|
|
|
|
|
|
|
kestrel.prototype.get = function(queue, timeout){ |
|
|
|
var command = 'GET ' + queue; |
|
|
|
|
|
|
|
|
|
|
|
timeout = parseInt(timeout,10); |
|
|
|
if( timeout > 0 ){ |
|
|
|
command += '/t='+timeout; |
|
|
|
@ -245,6 +256,35 @@ kestrel.prototype.get = function(queue, timeout){ |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
kestrel.prototype.getNextOpen = function(queue, timeout){ |
|
|
|
var command = 'GET ' + queue; |
|
|
|
|
|
|
|
timeout = parseInt(timeout,10); |
|
|
|
if( timeout > 0 ){ |
|
|
|
command += '/t='+timeout; |
|
|
|
} |
|
|
|
|
|
|
|
var connection = this._openConnection; |
|
|
|
|
|
|
|
if(connection) { |
|
|
|
command += '/close'; |
|
|
|
} else { |
|
|
|
connection = this._getConnection(); |
|
|
|
this._openConnection = connection; |
|
|
|
} |
|
|
|
|
|
|
|
command += '/open'; |
|
|
|
|
|
|
|
if( connection ){ |
|
|
|
connection.write(command + '\r\n'); |
|
|
|
} |
|
|
|
|
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
kestrel.prototype.delete = function(queue){ |
|
|
|
//delete given queue
|
|
|
|
var connection = this._getConnection(); |
|
|
|
|