Skip to content

Instantly share code, notes, and snippets.

@tblobaum
Created June 20, 2012 20:39
Show Gist options
  • Select an option

  • Save tblobaum/2962070 to your computer and use it in GitHub Desktop.

Select an option

Save tblobaum/2962070 to your computer and use it in GitHub Desktop.
var Drone = require('worker-drone')
, queue = [ { val : 1 }, { val : 2 }, { val : 0 }, { val : 2 }, { val : 4 } ]
, results = [ ]
var worker = new Drone({
work: function (data, callback) {
data.val += 5
callback(null, data)
}
, poll: 1000
, concurrency: 15
})
worker
.on('pop', function (callback) {
// worker drone is requesting a new thing
if (queue.length) {
callback(null, queue.pop())
}
// Note:
// the queue could easily be persisted in redis
// or another location. e.g.
// redis.lpop('worker-set', callback)
})
.on('error', function (error) {
// work function callback contained an error
console.error(error)
})
.on('push', function (item) {
// worker drone finished with an item
results.push(item)
})
.start()
//
// push a value onto the worker queue directly
worker.push({ val : 0 })
//
// push new items onto the queue that our worker will look at
setInterval(function () {
queue.push({ val : Math.random() })
console.log(results)
}, 1500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment