Skip to content

Commit

Permalink
Optimized event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Peter committed Jun 20, 2016
1 parent b72e124 commit 75463ab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ config.json
config*.json
deploy.json
index.js
test.js
/node_modules
/test
/lib
/lib
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ This is an advanced example showing some features in action.
## Release History
|Version|Date|Description|
|:--:|:--:|:--|
|0.4.3|2016-06-20|Optimized event listeners [Issue#15](https://github.com/mpneuried/rsmq-worker/issues/15). Thanks to [Kevin Turner](https://github.com/kpturner )|
|0.4.2|2016-05-06|Added the `.quit()` function [Issue#11](https://github.com/mpneuried/rsmq-worker/issues/11). Thanks to [Sam Fung](https://github.com/5amfung )|
|0.4.1|2016-04-05|Fixed missing isNumber function|
|0.4.0|2016-03-30|Updated dependencies (especially lodash to 4.x). Fixed a config bug caused by the array merge from `extend` [Issue#7](https://github.com/mpneuried/rsmq-worker/issues/7). Thanks to [Peter Hanneman](https://github.com/timelessvirtues )|
Expand Down
67 changes: 48 additions & 19 deletions _src/lib/rsmq-worker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class RSMQWorker extends require( "mpbasic" )()
###
start: =>
if @ready
# reconnect connection listener
#console.log "check", @queue.listeners('disconnect')?.indexOf( @_onDisconnect )
if @queue.listeners('disconnect')?.indexOf( @_onDisconnect ) < 0
@queue.on( "disconnect", @_onDisconnect )

#console.log "START", @queue.listeners('disconnect')

@stopped = false
@interval()
return
Expand All @@ -120,6 +127,7 @@ class RSMQWorker extends require( "mpbasic" )()
stop: =>
if not @stopped
@stopped = true
@queue.removeListener( "disconnect", @_onDisconnect )
clearTimeout( @timeout ) if @timeout?
@emit( "stopped" )
return @
Expand Down Expand Up @@ -227,31 +235,52 @@ class RSMQWorker extends require( "mpbasic" )()

@reconnectActive = false

# handle redis disconnect
@queue.on "disconnect", ( err )=>
@warning "redis connection lost"
_interval = @timeout?
if not @reconnectActive
@reconnectActive = true
@stop() if _interval

# on reconnect
@queue.once "connect", =>
@waitCount = 0
@reconnectActive = false
@queue = new @_getRsmq( true )
@_runOfflineMessages()
@interval() if _interval
@warning "redis connection reconnected"
return

return
if @queue.connected
@_initQueue()
else
@queue.once "connect", @_initQueue

return

###
## _onDisconnect
`RSMQWorker._onDisconnect()`
internal handler on disconnect
@param { Error } the redis connection error
@api private
###
_onDisconnect: ( err )=>
@warning "redis connection lost", err
_interval = @timeout?
if not @reconnectActive
@reconnectActive = true
@stop() if _interval

# on reconnect
@queue.once( "connect", @_onReConnect )
return

###
## _onReConnect
`RSMQWorker._onReConnect()`
internal handler on a reconnect
@api private
###
_onReConnect: =>
@waitCount = 0
@reconnectActive = false
@queue = @_getRsmq( true )
@_runOfflineMessages()
@interval() if _interval
@warning "redis connection reconnected"
return

###
## _getRsmq
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsmq-worker",
"version": "0.4.2",
"version": "0.4.3",
"description": "RSMQ helper to simply implement a worker around the message queue",
"keywords": [],
"homepage": "https://github.com/mpneuried/rsmq-worker",
Expand Down Expand Up @@ -29,7 +29,7 @@
"rsmq": "0.7.x"
},
"devDependencies": {
"should": "8.x",
"should": "9.x",
"grunt": "1.x",
"grunt-contrib-watch": "1.x",
"grunt-contrib-coffee": "1.x",
Expand Down

0 comments on commit 75463ab

Please sign in to comment.