Skip to content

Commit

Permalink
Added the .quit() function to be able to stop the script
Browse files Browse the repository at this point in the history
  • Loading branch information
M. Peter committed May 6, 2016
1 parent adde3f8 commit 9ec49b6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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)|
Expand Down
24 changes: 21 additions & 3 deletions _src/lib/rsmq-worker.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions _src/test/stoptest.coffee
Original file line number Diff line number Diff line change
@@ -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!" )
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 9ec49b6

Please sign in to comment.