From b133684be33e99840ce90978e77d86372e8696dc Mon Sep 17 00:00:00 2001 From: "M. Peter" Date: Thu, 31 Mar 2016 09:09:12 +0200 Subject: [PATCH] - Fixed a config bug caused by the array merge from `extend` - Updated dependencies (especially lodash to 4.x) close #7 --- Gruntfile.coffee | 20 +++++--------------- README.md | 3 ++- _src/lib/rsmq-worker.coffee | 32 +++++++++++++++++++++----------- _src/test/main.coffee | 15 ++++++++++++--- package.json | 22 +++++++++++----------- 5 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index f85b7e7..935a687 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -2,21 +2,12 @@ module.exports = (grunt) -> # Project configuration. grunt.initConfig pkg: grunt.file.readJSON('package.json') - regarde: + watch: module: files: ["_src/**/*.coffee"] - tasks: [ "coffee:changed" ] + tasks: [ "coffee:base" ] coffee: - changed: - expand: true - cwd: '_src' - src: [ '<% print( _.first( ((typeof grunt !== "undefined" && grunt !== null ? (_ref = grunt.regarde) != null ? _ref.changed : void 0 : void 0) || ["_src/nothing"]) ).slice( "_src/".length ) ) %>' ] - # template to cut off `_src/` and throw on error on non-regrade call - # CF: `_.first( grunt?.regarde?.changed or [ "_src/nothing" ] ).slice( "_src/".length ) - dest: '' - ext: '.js' - base: expand: true cwd: '_src', @@ -52,7 +43,7 @@ module.exports = (grunt) -> main: src: [ "test/main.js" ] options: - env: + env: severity_rsmqworker: "warning" @@ -74,7 +65,7 @@ module.exports = (grunt) -> # Load npm modules - grunt.loadNpmTasks "grunt-regarde" + grunt.loadNpmTasks "grunt-contrib-watch" grunt.loadNpmTasks "grunt-contrib-coffee" grunt.loadNpmTasks "grunt-contrib-clean" grunt.loadNpmTasks "grunt-mocha-cli" @@ -85,7 +76,6 @@ module.exports = (grunt) -> grunt.option('force', not grunt.option('force')) # ALIAS TASKS - grunt.registerTask "watch", "regarde" grunt.registerTask "default", "build" grunt.registerTask "docs", "docker" grunt.registerTask "clear", [ "clean:base" ] @@ -93,4 +83,4 @@ module.exports = (grunt) -> # build the project grunt.registerTask "build", [ "clear", "coffee:base", "includereplace" ] - grunt.registerTask "build-dev", [ "clear", "coffee:base", "docs", "test" ] \ No newline at end of file + grunt.registerTask "build-dev", [ "clear", "coffee:base", "docs", "test" ] diff --git a/README.md b/README.md index bbd0b4e..46bda43 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,8 @@ This is an advanced example showing some features in action. ## Release History |Version|Date|Description| |:--:|:--:|:--| -|0.3.8|2015-11-04|Fixed stop behaviour. [Pull#5](https://github.com/mpneuried/rsmq-worker/pull/5). Thanks to [Exinferis](https://github.com/exinferis)| +|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 )| +|0.3.8|2015-11-04|Fixed stop behavior. [Pull#5](https://github.com/mpneuried/rsmq-worker/pull/5). Thanks to [Exinferis](https://github.com/exinferis)| |0.3.7|2015-09-02|Added tests to check the behavior during errors within message processing; Added option `alwaysLogErrors` to prevent console logs if an error event handler was attached. [Issue #3](https://github.com/mpneuried/rsmq-worker/issues/3)| |0.3.6|2015-09-02|Updated dependencies; optimized readme (thanks to [Tobias Lidskog](https://github.com/tobli) for the [pull #4](https://github.com/mpneuried/rsmq-worker/pull/4))| |0.3.5|2015-04-27|again ... fixed argument dispatch for `.send()`| diff --git a/_src/lib/rsmq-worker.coffee b/_src/lib/rsmq-worker.coffee index cfaa6c8..852b06a 100644 --- a/_src/lib/rsmq-worker.coffee +++ b/_src/lib/rsmq-worker.coffee @@ -9,7 +9,13 @@ # # **node modules** -_ = require("lodash") +_isFunction = require( "lodash/isFunction" ) +_once = require( "lodash/once" ) +_isBoolean = require( "lodash/isBoolean" ) +_isArray = require( "lodash/isArray" ) +_last = require( "lodash/last" ) +_delay = require( "lodash/delay" ) + async = require("async") RSMQ = require("rsmq") @@ -57,6 +63,10 @@ class RSMQWorker extends require( "mpbasic" )() ### constructor: ( @queuename, options )-> super( options ) + # hard set of the interval because extend will merge the default with the given elements + if options.interval? and _isArray( options.interval ) + @config.interval = options.interval + @ready = false @waitCount = 0 @@ -128,7 +138,7 @@ class RSMQWorker extends require( "mpbasic" )() ### send: ( msg, args... )=> [ delay, cb ] = args - if _.isFunction( delay ) + if _isFunction( delay ) cb = delay delay = null @@ -159,11 +169,11 @@ class RSMQWorker extends require( "mpbasic" )() @queue.deleteMessage qname: @queuename, id: id, ( err, resp )=> if err @error "delete queue message", err - cb( err ) if _.isFunction( cb ) + cb( err ) if _isFunction( cb ) return @debug "delete queue message", resp @emit( "deleted", id ) - cb( null ) if _.isFunction( cb ) + cb( null ) if _isFunction( cb ) return return @ @@ -300,10 +310,10 @@ class RSMQWorker extends require( "mpbasic" )() @queue.sendMessage { qname: @queuename, message: msg, delay: delay }, ( err, resp )=> if err @error "send pending queue message", err - cb( err ) if cb? and _.isFunction( cb ) + cb( err ) if cb? and _isFunction( cb ) return @emit "new", resp - cb( null, resp ) if cb? and _.isFunction( cb ) + cb( null, resp ) if cb? and _isFunction( cb ) return return @@ -362,8 +372,8 @@ class RSMQWorker extends require( "mpbasic" )() return , @config.timeout ) - _fnNext = _.once ( del = true )=> - if _.isBoolean( del ) or _.isNumber( del ) + _fnNext = _once ( del = true )=> + if _isBoolean( del ) or _isNumber( del ) @del( _id ) if del else if del? # if there is a return value ant it's not a boolean or number i asume it's an error @@ -436,8 +446,8 @@ class RSMQWorker extends require( "mpbasic" )() if not wait @waitCount = 0 - if _.isArray( @config.interval ) - _timeout = if @config.interval[ @waitCount ]? then @config.interval[ @waitCount ] else _.last( @config.interval ) + if _isArray( @config.interval ) + _timeout = if @config.interval[ @waitCount ]? then @config.interval[ @waitCount ] else _last( @config.interval ) else if wait _timeout = @config.interval @@ -447,7 +457,7 @@ class RSMQWorker extends require( "mpbasic" )() @debug "wait", @waitCount, _timeout * 1000 if _timeout >= 0 clearTimeout( @timeout ) if @timeout? - @timeout = _.delay( @interval, _timeout * 1000 ) + @timeout = _delay( @interval, _timeout * 1000 ) @waitCount++ else @interval() diff --git a/_src/test/main.coffee b/_src/test/main.coffee index e2cdd3b..93e875d 100644 --- a/_src/test/main.coffee +++ b/_src/test/main.coffee @@ -9,7 +9,7 @@ describe "----- rsmq-worker TESTS -----", -> before ( done )-> RSMQWorker = require( "../." ) - worker = new RSMQWorker( _queuename, { interval: [ 0, 1 ] } ) + worker = new RSMQWorker( _queuename, { interval: [ 0, 1, 5 ] } ) worker.on "ready", -> done() @@ -25,7 +25,16 @@ describe "----- rsmq-worker TESTS -----", -> return describe 'Main Tests', -> - + + # Implement tests cases here + it "check interval config", ( done )-> + worker.config.interval.length.should.equal( 3 ) + worker.config.interval[ 0 ].should.equal( 0 ) + worker.config.interval[ 1 ].should.equal( 1 ) + worker.config.interval[ 2 ].should.equal( 5 ) + done() + return + # Implement tests cases here it "first test", ( done )-> _examplemsg = utils.randomString( utils.randRange( 4, 99 ), 3 ) @@ -54,9 +63,9 @@ describe "----- rsmq-worker TESTS -----", -> should.equal( msg, _examplemsg ) next() + worker.removeListener( "message", _testFn ) _diff = Math.round( ( Date.now() - _start )/1000 ) _diff.should.be.above(_delay) - worker.removeListener( "message", _testFn ) done() return diff --git a/package.json b/package.json index 7a579b9..9b8f20b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsmq-worker", - "version": "0.3.8", + "version": "0.4.0", "description": "RSMQ helper to simply implement a worker around the message queue", "keywords": [], "homepage": "https://github.com/mpneuried/rsmq-worker", @@ -23,19 +23,19 @@ }, "license": "MIT", "dependencies": { - "async": "1.4.x", - "lodash": "3.x", + "async": "1.5.x", + "lodash": "4.x", "mpbasic": "0.0.x", - "rsmq": "0.3.x" + "rsmq": "0.7.x" }, "devDependencies": { - "should": "4.3.x", + "should": "8.x", "grunt": "0.4.x", - "grunt-regarde": "*", - "grunt-contrib-coffee": "0.13.x", - "grunt-include-replace": "3.0.x", - "grunt-mocha-cli": "1.13.x", - "grunt-docker": "0.0.x", - "grunt-contrib-clean": "0.6.x" + "grunt-contrib-watch": "1.x", + "grunt-contrib-coffee": "1.x", + "grunt-include-replace": "3.x", + "grunt-mocha-cli": "2.x", + "grunt-docker": "0.x", + "grunt-contrib-clean": "1.x" } }