hwm-task-queue
A task queue with high-water mark and low-water mark event.
This module implements a Javascript task queue that emits 'hwm' and 'lwm' events. To use this task queue, first we create a task queue, and then enqueue tasks into it. If the number of tasks in the queue is greater than high-water mark, an 'hwm' event will be emitted. If the number of tasks drop below low-water mark, an 'lwm' event will be emitted. If the task queue is full, enqueue will fail and return false, and the task will be dropped.
Features
- Simple interface
- Support synchronous and ES2015 Promise based asynchronous tasks
- Customizable task queue
- Customizable tasks with timeout option
Install
npm install hwm-task-queue
API
createTaskQueue(task_queue_options)
Task queue options are:
capacity: 100 // Task queue capacity (default=100) concurrency: 10 // Maximum number of concurrent tasks (default=10) hwm: 08 // High-water mark ratio, a number between 0 and 1 (default=0.8) lwm: 06 // High-water mark ratio, a number between 0 and 1 (default=0.6)
taskQueue.enqueue(task_options)
Task options are:
fn: taskFunction // A function that performs a task (default=Function.prototype) args: arg1 arg2 // Arguments of fn (default=[]) id: 'task_1' // A string identifying the task (default='') this: context // Call-site this binding (default=null) timeout: 100 // Task timeout in ms, -1 means never timeout (default=-1)
Events:
taskQueue.on('hwm', function() { ... })
Emitted when tasks in the task queue reach the high-water mark specified in createTaskQueue options.
taskQueue.on('lwm', function() { ... })
Emitted when tasks in the task queue drop below the ligh-water mark specified in createTaskQueue options.
taskQueue.on('task_done', function(value, task_id) { ... })
Emitted when a task is finished. The return value of the task function or resolved value of the task Promise along with task id can be accessed in the callback function.
taskQueue.on('task_failed', function(err, task_id) { ... })
Emitted when a task failed. The error object and task id can be accessed in the callback function.
Examples:
Example of creating a task queue:
const createTaskQueue = ; const taskQueue = ;
Example of adding tasks to a task queue:
// An asynchronous task function that returns an ES2015 Promise. { return { ; };} // A synchronous task function. { return a + b;} let success;// Add an asynchronous task to the task queue. If successful, it will return true.// Otherwise it will return false, indicating that the task queue is full.success = taskQueue; success = taskQueue;
Example of handling the task queue events:
taskQueue; taskQueue; taskQueue; taskQueue;
See example/example.js for task queue in action.
License
MIT © 2016-2017 Hong Yan.