diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 935a687..b7de2af 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -80,6 +80,11 @@ module.exports = (grunt) -> grunt.registerTask "docs", "docker" grunt.registerTask "clear", [ "clean:base" ] grunt.registerTask "test", [ "build", "mochacli:main" ] + + # SHORTCUT ALIAS + grunt.registerTask "w", "watch" + grunt.registerTask "b", "build" + grunt.registerTask "t", "test" # build the project grunt.registerTask "build", [ "clear", "coffee:base", "includereplace" ] diff --git a/README.md b/README.md index 6e81d85..c59a497 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,9 @@ If you haven't defined the config `autostart` to `true` you have to call the `.s ### `.stop()` -Stop the receive interval. +Just stop the receive interval. +This will not cut the connection to rsmq/redis. +If you want you script to end call `.quit()` **Return** @@ -135,6 +137,12 @@ Change the interval timeouts in operation. *( Self )*: The instance itself for chaining. +### `.quit()` + +Stop the worker and close the connection. +After this it's no longer possible to reuse the worker-instance. +It's just intended to kill all timers and connections so your script will end. + ## Events ### `message` @@ -298,6 +306,7 @@ This is an advanced example showing some features in action. ## Release History |Version|Date|Description| |:--:|:--:|:--| +|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 )| |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)| diff --git a/_src/lib/rsmq-worker.coffee b/_src/lib/rsmq-worker.coffee index 88e3b17..a21eb87 100644 --- a/_src/lib/rsmq-worker.coffee +++ b/_src/lib/rsmq-worker.coffee @@ -62,7 +62,7 @@ class RSMQWorker extends require( "mpbasic" )() ### ## constructor ### - constructor: ( @queuename, options )-> + 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 ) @@ -118,9 +118,27 @@ class RSMQWorker extends require( "mpbasic" )() @api public ### stop: => - @stopped = true - clearTimeout( @timeout ) if @timeout? + if not @stopped + @stopped = true + clearTimeout( @timeout ) if @timeout? + @emit( "stopped" ) return @ + + ### + ## quit + + `RSMQWorker.quit()` + + Stop the worker and quit the connection + + @api public + ### + quit: => + @stop() + if @queue? + @queue.quit() + @queue = null + return ### ## send diff --git a/_src/test/stoptest.coffee b/_src/test/stoptest.coffee new file mode 100644 index 0000000..e9554aa --- /dev/null +++ b/_src/test/stoptest.coffee @@ -0,0 +1,16 @@ +RW = require( "../." ) +worker = new RW( "stoptest" ) + +worker.on "error", ( err )-> + console.log "ERR", err + return + +worker.on "message", ( msg, next, id )-> + console.log "MSG", msg, id + next() + console.log "stop?" + setTimeout( worker.quit, 500 ) + return + +worker.start() +worker.send( "TEST!" ) diff --git a/package.json b/package.json index 003c112..99356e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rsmq-worker", - "version": "0.4.1", + "version": "0.4.2", "description": "RSMQ helper to simply implement a worker around the message queue", "keywords": [], "homepage": "https://github.com/mpneuried/rsmq-worker", @@ -30,10 +30,10 @@ }, "devDependencies": { "should": "8.x", - "grunt": "0.4.x", + "grunt": "1.x", "grunt-contrib-watch": "1.x", "grunt-contrib-coffee": "1.x", - "grunt-include-replace": "3.x", + "grunt-include-replace": "4.x", "grunt-mocha-cli": "2.x", "grunt-docker": "0.x", "grunt-contrib-clean": "1.x"