| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Its is a job queue inspired by Resque, but is trying to improve certain shortcomings of Resque's design. In particular, mongo-queue makes it impossible to lose jobs. Jobs are kept in the database until workers have successfully completed them and only then are they removed from the database.
Even if your host or application crashes hard (without chance to catch the exception in the application's runtime), no jobs are lost.
An object that will hold the MongoDB connection and allow manipulation of the job queue.
Options are :
Adds a job to the queue.
Emitted in case of a connection error.
Emitted when the Connection object is ready to work.
Base class to create your own job templates, you need to inherit from it.
Function containing your job's code. You can define any arguments you need.
Any synchronous exception thrown will be caught and the job will go back to the queue.
You need to call this function when you are done with the job.
You can provide an error in case anything went wrong.
Worker object that will watch out for new jobs and run them.
You need to provide a Connection object and an array of Worker classes.
Options are :
Starts the polling of the database.
Stops the worker, it will not interrupt an ongoing job.
Emitted on polling errors, unknown template definitions or job error.
Emitted when the queue has no more job to process or if there is no job to run on a poll.
Emitted after a call to stop() and once all the jobs are stopped.
queue = require 'mongo-queue'
# First declare your job by extending queue.Template
class Addition extends queue.Template
perform: (a, b) ->
console.log a + ' + ' + b + ' = ' + (a + b)
@complete()
# Create a connection to the database
options = host: 'localhost', port: 27017, db: 'test'
connection = new queue.Connection options
# Listen to connection errors
connection.on 'error', console.error
# Put some jobs into the queue
connection.enqueue Addition.name, 1, 1, (err, job) -> if err then console.log(err) else console.log('Job added :', job)
connection.enqueue Addition.name, 2, 4, (err, job) -> if err then console.log(err) else console.log('Job added :', job)
connection.enqueue Addition.name, 3, 6, (err, job) -> if err then console.log(err) else console.log('Job added :', job)
connection.enqueue Addition.name, 4, 8, (err, job) -> if err then console.log(err) else console.log('Job added :', job)
# Now you need a worker who will process the jobs
worker = new queue.Worker connection, [ Addition ]
worker.on 'error', console.error
worker.poll()
| Back | FazBrowse Home | New Git URL |