Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mikeyburkman/burk-queue

An in-memory NodeJs queue for throttling work items that implement ES6-like promise
https://github.com/mikeyburkman/burk-queue

Last synced: about 5 hours ago
JSON representation

An in-memory NodeJs queue for throttling work items that implement ES6-like promise

Awesome Lists containing this project

README

        

# burk-queue
A light-weight in-memory NodeJs queue for throttling asynchronous work items that implement ES6-like promises. (Any promise that implements finally().)

## Usage
```js
var http = require(...);

var BurkQueue = require('burk-queue');

var processHttpRequestItem = function(method, url) {
return http.request(method, url) // Returns a Q-like response
.then(function(resp) {
console.log('response = ', resp.text);
});
};

// Create the queue
var requestQueue = new BurkQueue({
concurrency: 2,
callback: processHttpRequestItem
});

// Add some work items
for (var i = 0; i < 20; i += 1) {
requestQueue.push('GET', 'https://github.com/MikeyBurkman/work-queue');
}

// Keep checking, and exit when the queue is empty.
setInterval(function() {
if (requestQueue.empty()) {
console.log('Complete!');
process.exit(0);
}
}, 1000);
```

## Description of the Example
- A maximum of 2 concurrent requests (`concurrency: 2`) will be happening at any given time.
- When the queue can process work, the `callback` function will be called.
- The `push(args)` method on the queue puts work items on the queue.
- The `empty()` returns true if the queue has finished processing all the work given to it.

## Constructor Options
`callback`: Required. The function to be executed when a work item can be processed.
- The callback function is expected to return a [Q-like](https://www.npmjs.com/package/q) promise in order to throttle correctly.
- If the callback function returns something else, it is assumed to be finished as soon as the function returns.
- Any arguments passed to `push(args)` will be given as arguments to the callback, in the same order.

`concurrency`: Optional. The maximum number of items that can be processed concurrently. Defaults to 1.

`interval`: Optional. When there are items on the queue, the queue will check every `interval` milliseconds to see if it can start processing a new work item. Defaults to 50ms.

## API
`push(args)`: Pushes a work item on to the queue.
- Any arguments may be given. The arguments given to `push` are given to the `callback` function exactly.

`empty()`: Returns true if the queue is empty and is not currently processing anything.

`notEmpty()`: The inverse of `empty()`.

`clear()`: Clears the queue.
- Anything currently processing will finish, but no more work items in the queue will be processed.