From 75463ab98aba1a20c9aa28622f1fec237bc8eaa7 Mon Sep 17 00:00:00 2001 From: "M. Peter" Date: Mon, 20 Jun 2016 13:35:34 +0200 Subject: [PATCH] Optimized event listeners #15 --- .gitignore | 3 +- README.md | 1 + _src/lib/rsmq-worker.coffee | 67 ++++++++++++++++++++++++++----------- package.json | 4 +-- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 23ac77e..8cc1282 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ config.json config*.json deploy.json index.js +test.js /node_modules /test -/lib \ No newline at end of file +/lib diff --git a/README.md b/README.md index 5d8b429..24f7630 100644 --- a/README.md +++ b/README.md @@ -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 )| diff --git a/_src/lib/rsmq-worker.coffee b/_src/lib/rsmq-worker.coffee index a21eb87..0e2ed95 100644 --- a/_src/lib/rsmq-worker.coffee +++ b/_src/lib/rsmq-worker.coffee @@ -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 @@ -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 @ @@ -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 diff --git a/package.json b/package.json index 99356e2..4b64b22 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",