README
skedgy
Periodically check for and queue work to be done!
Skedgy (pronounced "ske-gee") is an opinionated scheduling library for NodeJS. The purpose of skedgy is to periodically check for work with a random delay between each check, as well as queue and execute tasks with a random delay between each execution. The goal was to create a scheduler that acts naturally, not with exact timing that can be tracked. Tasks are always run one at a time and never retried.
Example:
import { Scheduler } from 'skedgy';
// Create a limited set of random 'response' data...
const responses = Array(10).fill(0).map((x) => Math.random().toString(36).substr(2, 10));
// Pretend this is a much cooler check...
async function someExternalRequest(): Promise<string> {
await randomDelay(3000);
return responses.pop();
}
// Pretend this is a much cooler action...
async function processData(text: string) {
await randomDelay(3000);
console.log(text.toUpperCase());
await randomDelay(1000);
}
class ExampleScheduler extends Scheduler<string> {
protected async poll(): Promise<void> {
let result = await someExternalRequest();
this.enqueue(result);
}
protected async work(item: string): Promise<void> {
await processData(item);
}
}
const sched = new ExampleScheduler({
pollMinDelay: 3000,
pollMaxDelay: 8000,
taskMinDelay: 3000,
taskMaxDelay: 5000
});
// Start the scheduler...
sched.start();
setTimeout(() => {
// Force stop after 60s...
sched.stop();
process.exit(0);
}, 60 * 1000);
function randomDelay(delay: number) {
return new Promise(resolve => setTimeout(resolve, Math.random() * delay));
}
Features:
- Simple to use API.
- Built on Promises!
- Randomly vary the interval between polling.
- Throttle the work functions with a random range (so it's not predictable).
- Persists work to super simple, overridable queue (defaults to in-mem queue).
Contribute
- Fork it
npm i
npm run watch
- Make changes and write tests.
- Send pull request! :sunglasses:
License:
MIT