tasqueue
Promise-based Node.js job/task-queue library using disque.
How it works
- Create a client
- Register new job types handlers
- Push jobs
Tasqueue is a job/task-queue library based on disque and using Q. It aims to be simple, fast and to handle a high charge.
Monitoring functions of Tasqueue can only be entrusted when using a single-node instance of disque.
Create a client
var Tasqueue = ; // Default optionsvar opts = authPass: null // AUTH password for disque-server host: 'localhost' // disque-server host port: 7711 // disque-server port pollDelay: 1000 * 15 // Polling delay in ms when no workers are available jobTimeout: 1000 * 60 * 60 // Timeout in ms before a job is considered as failed failedTTL: 60 * 60 * 24 // Failed jobs TTL in sec completedTTL: 60 * 60 * 24 // Completed jobs TTL in sec queuedTTL: 60 * 60 * 24 // Queued jobs TTL in sec activeTTL: 60 * 60 * 1 // Active job TTL in sec maxAttempts: 60 // Max reconnection attempts retryMaxDelay: 1000 * 60 // Prevent exponential reconnection delay; var tasqueue = opts;
Queue API
tasqueue.init()
Async: Initialize the client.
Example
tasqueue;
tasqueue.shutdown(timeoutMs, callback)
Async: End the client.
Example
tasqueue;
tasqueue.poll()
Start polling and jobs execution. This function should be run only once.
Example
tasqueue;
tasqueue.registerHandler(handler)
Register a job handler. handler
should have the following properties:
var handler = type: 'jobType' // {String} will be used as the queue name concurrency: 5 // {Integer} max number of concurrent workers for this type, default = 1 maxAttempts: 5 // {Integer} max number of retry for this job type, default = 1 { // do whatever using the body passed for this job };
tasqueue.listHandlers()
List of registered handlers types as an array.
Example
var handler1 = type: 'type:1' ... ;var handler2 = type: 'type:2' ... ; tasqueue;tasqueue;var registeredHandlers = tasqueue;// registeredHandlers equals ['type:1', 'type:2']
tasqueue.pushJob(jobType, body)
Async:
Push a new job that will be processed by the corresponding jobType
handler. The worker will call the handler's exec
function with body
used as its argument.
When successful, returns the added job id.
Example
var handler1 = type: 'type:1' { console; }; tasqueue; // After some time...// Logs 'hello Johan'
tasqueue.getJob(id)
Async: Returns a Job object that can be easily manipulated. You can find the API for Jobs a bit below.
The promise is rejected if the queried job doesn't exist.
Example
tasqueue;
Count jobs
Async: Returns the count of jobs by state.
tasqueue.count(state)
state
must be one of ['active', 'queued', 'completed', 'failed']
.
tasqueue.countActive()
tasqueue.countQueued()
tasqueue.countCompleted()
tasqueue.countFailed()
List jobs
Async: Returns the list of jobs for each state and cursors to paginate through the jobs.
Example
var opts = start: 10 // Start/skip cursor limit: 10 // Number of jobs to return; tasqueue;
tasqueue.list(state)
state
must be one of ['active', 'queued', 'completed', 'failed']
.
tasqueue.listActive()
tasqueue.listQueued()
tasqueue.listCompleted()
tasqueue.listFailed()
Jobs API
job.details()
Get the job's informations in a pretty form.
Example
tasqueue
job.cancel()
Async: Cancels the job and set it as failed.
Only queued jobs may be cancelled. The promise is rejected if the job is not in the queued
state.
job.delete()
Async: Utterly delete a job, whichever its state is.
Events
Tasqueue inherits the Node.js EventEmitter
class. Below is the list of all events emitted by tasqueue during execution:
Client
Client connection
;
Client closed
;
Queue execution
Polling jobs
;
Polling delayed
;
No worker available
Error while polling
;
Jobs
Job started
;
Job successfully pushed
;
Job successfully canceled
;
Job successfully deleted
;
Job re-queued after failure
;
Job passed
;
Job failed
;
Error canceling a job
;
No handler registered for a job
;
Handlers
Handler successfully registered
;
Error: handler already exists
;