YAQS
YAQS (Yet Another Queue System) is a Node.js module to manage job queues using Redis.
YAQS manage any number of queues, any number of servers and any concurrency per server per queue.
Installation
npm install yaqs
Client
To use YAQS you need to create a client:
var client = opts
You can use this client to create, use and remove queues.
Options
prefix
(default='yaqs'
): A prefix for Redis keysredis
(default={}
): Configuration to connect to Redisport
(default=6379
): Redis porthost
(default="localhost"
): Redis hostpass
(default=undefined
): Redis password
defaultConcurrency
(default=1
): Default concurrencydefaultTimeoutOnStop
(default=2000
): Default timeoutOnStop in ms (timeout before force-killing a queue)defaultPriority
(default=client.PRIORITY.NORMAL
): Default priority when we create job withoutpriority
optiondefaultRetry
(default=0
): Default number of retry before giving up on a jobdefaultRetryDelay
(default=-1
): Default retry delay in ms; values lower than 0 means direct retry.defaultTimeout
(default=-1
): Default timeout in ms
Usage
// An object of constant prioritiesclientPRIORITY; // Create queue with a name and some optionsclient; // Call queue.stop() on all queuesclient; // Get stats of all queues with this prefix in Redis// Stats for the queue 'test' are in stats.testclient;
Queue
var queue = client;
Options
concurrency
(default=client.defaultConcurrency
): Concurrency for this queue in this servertimeoutOnStop
(default=client.defaultTimeoutOnStop
): When stoppping the queue, maximum amount of time in ms to wait before force-stopping workers. If the timeout is less than or equal to 0, no limit is enforced.defaultPriority
(default=client.PRIORITY.NORMAL
): Default priority for new jobsdefaultRetry
(default=0): Default retry for new jobsdefaultRetryDelay
(default=-1): Default retry delay in ms for new jobsdefaultTimeout
(default=-1): Default timeout in ms for new jobs
Usage
// Start processing jobs,// Callback can get an error if unable to retrieve jobs from Redis.queuestart {}; // Set the function used to process jobs.// Can be hot-swapped while the queue is workingqueue; // Create a job with data and somes options (optional)// This job needs to be saved to be processedvar job = queue; // Stop processing jobsqueue; // Stop processing jobs and remove all jobsqueue; // Get stats for this queue// stats.pending = number of pending jobs// stats.processing = number of processing jobs// stats.total = total number of jobs added to this queuequeue; // Remove stucked processing jobsqueue;
Events
queue; // When an error occuredqueue; // When we start the queuequeue; // When we stop the queuequeue; // When we remove the queuequeue; // When a queue has no new jobs to process queue; // When a job completqueue; // When a job failqueue; // When a job timeout
Example of worker function
{ // Use job.data to do something console; // Return an error for event 'job.failed' ; // Return some data for event 'job.complete' ;} queue;
Job
var job = queue;
Options
priority
(default:queue.defaultPriority
): Priority for the job, greater is better. Useclient.PRIORITY
values (VERY_HIGH, HIGH, NORMAL, LOW, VERY_LOW)timeout
(default:queue.defaultTimeout
): Timeout of the job in ms. If the job is not finished after the timeout, it is considered finished (although the job is not killed, it is your responsability to ensure you're not leaking resources). If the timeout is less than or equal to 0, it is not taken into consideration. Ajob.timeout
event is sent on the queue.retry
(default:queue.defaultRetry
): number of retry to use if the worker returns an error.retryDelay
(default:queue.defaultRetryDelay
): delay in ms between two retry
Usage
// Set job's priorityjob; // Save job in redis for processingjob; // Remove job from pending listjob;