Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 6 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
8 README.md
View
@@ -63,6 +63,14 @@ watch the status of jobs entering and exiting the chain.
console.log(name, "has finished. Error:", err)
})
+ chain.on('empty', function() {
+ console.log('queue is empty')
+ })
+
+ chain.on('timeout', function(job) {
+ console.log(job.name, 'timed out')
+ })
+
## Use Case
Let's say you have an expensive child process to run when requests come in.
24 src/index.coffee
View
@@ -5,11 +5,10 @@ class ChainGang extends Events.EventEmitter
# Initializes a ChainGang instance, and a few Worker instances.
#
# options - Options Hash.
- # workers - Number of workers to create (default: 3)
- # timeout - Optional Number of seconds to wait for the job
- # to run.
- # timeoutCallback - Optional function to call when timeout is
- # triggered.
+ # workers - Number of workers to create (default: 3)
+ # timeout - Optional Number of seconds to wait for the job to run.
+ # onTimeout - Optional function to call when timeout is triggered.
+ # Deprecated, listen for the 'timeout' event instead.
#
# Returns ChainGang instance.
constructor: (options) ->
@@ -20,7 +19,8 @@ class ChainGang extends Events.EventEmitter
@index = {} # name: worker
@active = true
@timeout = options.timeout or 0
- @timeoutCb = options.timeoutCallback
+ if cb = (options.onTimeout or options.timeoutCallback)
+ @on 'timeout', cb
# Public: Queues a callback in the ChainGang.
#
@@ -73,7 +73,11 @@ class ChainGang extends Events.EventEmitter
delete @index[job.name]
delete job
- if @active then @perform()
+ if @active
+ if @queue.length == 0
+ @emit 'empty'
+ else
+ @perform()
# Public: Generates a message detailing the current status of the chain
# gang instance.
@@ -98,7 +102,7 @@ class ChainGang extends Events.EventEmitter
if @timeout <= 0
false
else
- setTimeout @triggerTimeout, 100, job
+ setTimeout @triggerTimeout, @timeout * 1000, job
# Handles a job that has been in the queue too long. This is the callback
# from setTimeout().
@@ -108,8 +112,8 @@ class ChainGang extends Events.EventEmitter
# Returns nothing.
triggerTimeout: (job) ->
job.timedOut = true
- job.chain.timeoutCb? job
- job.finish error: "Timed out"
+ job.chain.emit 'timeout', job
+ job.finish message: "timeout"
# Generates a default name for this Job by getting the MD5 hash of the task
# function.
17 test/empty_callback_test.coffee
View
@@ -0,0 +1,17 @@
+assert = require 'assert'
+chainGang = require '../src/index'
+
+calls = []
+onEmpty = false
+chain = chainGang.create()
+chain.on 'empty', -> onEmpty = true
+
+task = -> assert.fail(onEmpty)
+chain.add(task, 'a', -> calls.push(1))
+chain.add(task, 'a', -> calls.push(2))
+chain.add(task, 'b', -> calls.push(3))
+chain.perform()
+
+process.on 'exit', ->
+ assert.deepEqual [1,2,3], calls
+ assert.ok onEmpty
14 test/timer_test.coffee
View
@@ -6,13 +6,11 @@ timedOut = 0
calls = 0
timer = null # timer of the test
-chain = chainGang.create(
- workers: 1
- timeout: 0.1
- timeoutCallback: ->
- timedOut += 1
- clearTimeout timer
-)
+chain = chainGang.create workers: 1, timeout: 0.1
+
+chain.on 'timeout', ->
+ timedOut += 1
+ clearTimeout timer
# disable the chain so we can simulate long queueing times
chain.active = false
@@ -23,7 +21,7 @@ chain.add (worker) ->
calls += 1
worker.finish()
, 'a', (err) ->
- assert.equal "Timed out", err.error
+ assert.equal "timeout", err.message
callbacks += 1
setTimeout ->

No commit comments for this range

Something went wrong with that request. Please try again.