0.0.1 • Published 9 years ago
task-stream v0.0.1
task-stream
This is 0.x version. API will change.
npm install task-stream and process tasks with Node's Stream API and get backpressure and other neat features for free.
Basic Usage
const taskStream = require('task-stream');
// Provide a function that fetches tasks one by one
// callback(error, task)
const getTask = (callback) => db.tasks.findOne(callback);
// Provide a processing function
// callback(error)
const processTask = (task, callback) => {
httpGet(task.url, (error, html) => {
db.results.insert({
error: error,
result: html
}, callback);
});
};
// Combine everything together
// and start processing tasks.
const worker = taskStream(getTask, processTask);
worker.start();
// You can also stop selecting new tasks.
// Note that this won't stop any ongoing processing,
// but make sure that no more tasks are queued.
worker.stop();Notes
getTaskmust callcallback(null, null)when no more task are to be scheduled.
Options
taskStream accepts optional options object
that may include one or more of the following props:
readAheadpositive integer specifies how many tasks will be buffered (defaults to0, sogetTask()will only get called after all currently queued tasks are processed);parallelTaskspositive integer specifies how many tasks can be processed in parallel (defaults to1);onErrorfunction will get called on any error;onReadErrorfunction will get called ifgetTaskfails as;onWriteErrorfunction will get called ifprocessTaksfails;onFinishfunction will get called when all the tasks are processed successfuly.
Data Flow
To get a better idea on the data flow, check out test.js and play around with reader and writer options.
0.0.1
9 years ago