Job Queue in LevelDB for Node.js
$ npm install level-jobs --save
var levelup = ;var db =
var Jobs = ;
This function will take care of a work unit.
This function gets 3 arguments:
iduniquely identifies a job to be executed.
workerneed to process the job.
cbis the callback function that must be called when the job is done.
This callback function accepts an error as the first argument. If an error is provided, the work unit is retried.
var queue = ;
This database will be at the mercy and control of level-jobs, don't use it for anything else!
(this database can be a root levelup database or a sublevel)
You can define a maximum concurrency (the default is
var maxConcurrency = 2;var queue = ;
As an alternative the third argument can be an options object with these defaults:
var options =maxConcurrency: InfinitymaxRetries: 10backoff:randomisationFactor: 0initialDelay: 10maxDelay: 300;var queue = ;
var payload = what: 'ever';var jobId = queue;
(Only works for jobs that haven't started yet!)
queue.pendingStream() emits queued jobs.
queue.runningStream() emits currently running jobs.
var stream = queue;stream;
A queue object emits the following event:
drain— when there are no more jobs pending. Also happens on startup after consuming the backlog work units.
error- when something goes wrong.
retry- when a job is retried because something goes wrong.
If you simply want a pure queue client that is only able to push jobs into the queue, you can use
level-jobs/client like this:
var QueueClient = ;var client = ;client;