lcherone-task-queue
v1.0.3
Published
A simple wrapper for fastq
Downloads
3
Readme
lcherone-task-queue
This library provides a simple task queue for handling asynchronous tasks. Which uses a basic interval timer and fastq.
Installation
To install lcherone-task-queue
, run the following command:
npm i lcherone-task-queue
Usage
To use lcherone-task-queue
, follow these steps:
- Import the
lcherone-task-queue
library:
const Tasks = require('lcherone-task-queue')
- Create a new instance of the
Tasks
class:
const tasks = new Tasks()
- Add a new task queue to the
Tasks
instance:
tasks.add(
// Name of the task queue
'email',
{
// The finder function should return a list of jobs as a Promise
finder: () => {
return Promise.resolve(Array.from({ length: 10000 }, (_, i) => i + 1))
},
// The job worker, gets passed the single item should callback
worker: function (input, cb) {
// Log the input to the console
console.log('worker', input)
// Call the callback with a null error and the result of the job (in this case, the input multiplied by 2)
cb(null, input * 2)
},
// The success handler, handles if the single job item completed
onSuccess: (input, result) => {
// Log the result and input to the console
console.log('onSuccess', result, input)
},
// The error handler, handles if the single job item failed
onError: (input, err) => {
// Log the error and input to the console
console.log('onError', err, input)
},
// The interval in ms on when to fire the job finder
interval: 1000
}
)
- Run the task queue:
tasks.run()
Options
finder
: A function that returns a Promise resolving to a list of jobs.worker
: A function that processes a single job item. It should accept two arguments:input
(the job item) andcb
(a callback function). The callback should be called with two arguments:err
(an error if one occurred) andresult
(the result of the job).onSuccess
: A function that is called when a job item is successfully processed. It should accept two arguments:input
(the job item) andresult
(the result of the job).onError
: A function that is called when an error occurs while processing a job item. It should accept two arguments:input
(the job item) anderr
(the error).interval
: The interval in milliseconds at which to run thefinder
function.