@pyrodash/job-scheduler
v1.1.3
Published
A distributed job scheduler for node.js
Readme
@pyrodash/job-scheduler
A distributed job scheduler for Node.js. There are currently two drivers being wrapped by this library, one for Apache Pulsar and the other for Redis.
Apache Pulsar
Setup
- Install Apache Pulsar
- Install the library
npm install @pyrodash/job-scheduler pulsar-clientUsage
import { PulsarScheduler, JobContext, Job } from '@pyrodash/job-scheduler'
interface MyParams {
name: string
}
class MyJob extends Job<MyParams> {
handle(ctx: JobContext<MyParams>): void {
console.log(ctx)
}
}
const scheduler = new PulsarScheduler({
url: 'pulsar://localhost:6650',
topic: 'persistent://public/default',
producerTimeoutMs: 0, // How long before inactive producers are terminated
cleanupIntervalMs: 0, // How often producer garbage collection should run (0 for never)
})
// Registering a worker
await scheduler.register(new MyJob()) // Job name will be the class name in kebab-case
// This syntax is also supported
await scheduler.handle('my-job', (ctx: JobContext<MyParams>) => {
console.log(ctx)
})
// Scheduling a job
await scheduler.schedule(
MyJob
.withId('hello-world')
.withParams({ name: 'Pyrodash' })
.at(Date.now() + 5000) // 5 seconds from now
.recurring(1000), // every second
)
// This syntax is also supported
await scheduler.schedule('my-job', {
id: 'hello-world',
params: { name: 'Pyrodash' },
schedule: {
deliverAt: Date.now() + 5000,
rate: 1000,
},
})Redis
Setup
- Install Redis
- Install the library
npm install @pyrodash/job-scheduler bull ioredisUsage
import { RedisScheduler } from '@pyrodash/job-scheduler'
const scheduler = new RedisScheduler({
... ioredis config
})
// Usage is identical to the Pulsar scheduler
// The Redis Scheduler supports cancelation out of the box
await scheduler.cancel('my-job', 'job id')