kueue
v0.9.0
Published
A JavaScript queue manager for background tasks
Downloads
2
Maintainers
Readme
kueue
Why?
I was working on an app that needed to be able to do some background tasks; uploading images to a server. I wanted a background uploader but wanted to make it generic so it could carry out any function and handle next, retry and cancellation of tasks.
Quick Start
- Download the latest version.
- Place in your lib folder
Wherever you want to initialise the API interface, put this:-
var kueue = require("kueue");
var queue = new kueue();
// simulates a REST call with timeout -- say uploading an image
queue.add("myFirstTask", function(status, next, retry, cancel) {
setTimeout(function() {
next();
}, 3000);
});
queue.add("mySecondTask", function(status, next, retry, cancel) {
setTimeout(function() {
next();
}, 3000);
});
queue.start();
When adding tasks, you wrap your task in a function that can receive four parameters; status, next, retry, cancel.
Once your task finishes, you can call next() to start the next task -- if your task fails, you can call retry() to have it go back in the queue to try again. If you call cancel() your task is removed. The "status" property is an object that gives you the id of the task, plus an "attempts" property that will show you how many times the task was called. This is useful if you want to keep track and then cancel() a task if it's failed too many times
You can stop the queue by issuing a .stop() command and clear it with .clear().