Monq is a MongoDB-backed job queue for Node.js.
Connect to MongoDB by specifying a URI or providing host
, port
and database
options:
Monq = require "monq"
Client = Monq "mongodb://localhost:27017/monq_example"
Enqueue jobs by supplying a job name and a set of parameters. Below, the job reverse
is being placed into the example
queue:
queue = Client.queue "example"
queue.enqueue "reverse", { text: "foobar" }, (err, job) ->
console.log "enqueued:", job.data
Create workers to process the jobs from one or more queues. The functions responsible for performing a job must be registered with each worker:
worker = Client.worker ["example"]
worker.register
reverse: (params, callback) ->
try
reversed = params.text.split("").reverse().join("")
callback null, reversed
catch (err)
callback err
worker.start()
Create workers' lock functions to dequeue only unlocked jobs. The functions responsible for performing a job must be registered with each worker: (The registerLock
callbacks should return an error when lock exists)
worker = Client.worker ["example"]
worker.register
reverse: (params, callback) -> ...
worker.registerLock
reverse: (job, callback) -> ...
worker.start()
Workers will emit various events while processing jobs:
worker.on "dequeued", (data) -> …
worker.on "failed", (data) -> …
worker.on "complete", (data) -> …
worker.on "error", (err) -> …
Written in coffeescript, so a coffee compiler is needed to build
npm install -g coffee-script
npm install monq
npm test