node-multi-queue
An async queue.
Features
- Multiple/Managable queues
Install
npm i -S multi-queue
Usage
var mqueue = ;var mq = ; // Request pages 1 - 5,// 3 pages at a time// disallow duplicate requests per unique, page 2 will NOT run twicemq1 2 2 3 4 5
API
Constructor
var mqueue = ;var mq = ; // Note: Exported is a function to be called, not a class
push(key, [options])
Queue#create Creates a new queue named by key
.
concurrency
Option: Default: 1 (aka no concurrency)
Executes N tasks specified by concurrency
.
var mq = ;mq;mq;mq;mq; // queued
push([key], task, [options])
Queue#push Add a task to a queue(key
).
task
is expected to be a function that receives a single callback argument
which must be called to continue processing tasks.
If no key is provided then the task is added to the default queue.
var mq = ;// add task to the `repos` queuemq; // Add to a queue uniquelymq; // IGNOREDmq;
name
Option: By specifying a name for a task you gain the ability to call the following methods:
- remove
unique
Option: This option guarantees your tasks are unique.
var mq = ;mq;mq; // equivalent to the above mq; // Will not be added to queuemq; // Added to the myKey queue
Note: If a
string
value is set tounique
then that will act as a name and unique will be set totrue
. In otherwords,{unique: 'tweets'}
is equivalent to{ name: 'tweets', unique: true }
.
meta
Option: Provide meta data to get insight into tasks when listening to events.
mq;
mq.push(uploadData, { meta: { id: 5 } });
start([key])
Queue#start Start a queue specified by key
.
If
key
is omitted then the default queue is started
stop([key])
Queue#stop Stop a queue specified by key
.
If
key
is omitted then the default queue is stopped
empty([key])
Queue#empty Remove all tasks from the queue specified by key
.
If
key
is omitted then the default queue is emptied
remove([key], name)
Queue#remove Remove a task(name
) from the queue(key
).
If
key
is omitted then the default queue is used
In order to remove a task then it must be named when it is added via queue.push.
Events
MultiQueue
extends EventEmitter
and emits the following events:
start
Called when start
is called
mqmqstart'foo';
stop
Called when stop
is called
mqmq;
empty
Called when a queue is emptied
mq;mq;
queue
Called when a task is added and queued (can't run immediately)
mq;mq;mq;
run
Called when a task is executed
mq;mq;
duplicate
Called when a task is executed
// called once in this scenariomq;mq;mq;
done
Called when a task is completed
Pass values into
done
in order to gain some insight into the task
// Expose an error { ;}mqmq;
// Expose values { ;}mqmq;
TODO
- Timeout tasks
- Active concurrency (When a task with a differenct concurrency is added to the queue update value)