async-timed-cargo
v0.0.3
Published
An lib to periodically flush a collection with a limited size based on async.cargo
Downloads
2,346
Readme
Async Timed Cargo
async.cargo based task execution.
Differences from async.cargo
Async.cargo:
- Payload number is used as a limit to the message, but most times the callback is called with a fewer number of items
- It tries to process tasks as soon as possible when they are pushed, which is bad if used to control batch messages processing, as it will result in more calls with fewer items
async-timed-cargo
- Adds a timer concept to the task execution
- Payload tries to be respected, if number of messages received is less than payload it does not do anything until timer is triggered or more messages are received, whatever comes first
- It is better for message batch process as it waits until the queue is full
Install
add async-timed-cargo
to you package.json
Example
var asyncTimedCargo = require('async-timed-cargo');
var cargo = asyncTimedCargo(function(tasks, callback) {
// the tasks array will be [1, 2, 3]
// this will be called after 1000ms, as number of items (3) is smaller than payload (10)
callback('err', 'arg');
}, 10, 1000);
asyncTimedCargo.push(1);
asyncTimedCargo.push(2);
asyncTimedCargo.push(3, function(err, arg) {
// err will be 'err'
// arg will be 'arg'
});
Also, check the example app for a working example on how to use the middleware
Usage
async-timed-cargo(worker, [[payload], timeout])
Arguments
worker(tasks, callback)
- An asynchronous function for processing an array of queued tasks, which must call itscallback(err, arg)
argument when finished, with an optionalerr
andarg
argument.payload
- An optionalinteger
for determining how many tasks should be processed per round; if omitted, the default is unlimited and only timer will be consideredtimeout
- An optionalinteger
for determining the interval (in ms) to be used to flush data each round; if omitted, the default is 1000ms
Cargo objects
The cargo
object returned by this function has the following properties and
methods:
length()
- A function returning the number of items waiting to be processed.push(task, [callback])
- Addstask
to thequeue
. The callback is called once theworker
has finished processing the task. Instead of a single task, an array oftasks
can be submitted. The respective callback is used for every task in the list.
Test
$ npm install
$ npm test