Kochegar
Simple job queue utility
This library is intended to be used in Typescript projects, but can be built to Javascript and used directly.
What does it do?
It allows you to define tasks and peform jobs on them in an asynchrous manner with concurrency limiting. It can be run indefinitely and doesn't run idle cycles if all jobs are completed.
Configuration
-
concurrency
is how many concurrent jobs can be run at a time -
interval
is an interval between batch scheduling
Basic usage
import { Kochegar } from 'kochegar';
const worker = new Kochegar({ concurrency: 10, interval: 1000 });
let sum = 0;
worker.createTask(
'increment', payload => sum = sum + payload,
);
worker.createTask(
'decrement', payload => sum = sum - payload,
);
worker.push({ task: 'increment', payload: 10 });
worker.push({ task: 'increment', payload: 20 });
worker.push(
{ task: 'increment', payload: 10 },
{ task: 'increment', payload: 20 },
);
worker.push({ task: 'decrement', payload: 50 });
console.log(worker.jobs());
// 5
console.log(worker.jobs('increment'));
// 4
console.log(worker.jobs('decrement'));
// 1
worker.wait().then(() => {
console.log(sum);
});
// 10