fastify-node-cron
v2.0.1
Published
This module provides a way run scheduler workers using node-cron with fastify, featuring native node-cron v4 options and graceful shutdown.
Downloads
10
Maintainers
Readme
fastify-node-cron
This module provides a way to configure scheduled workers using node-cron
Install
npm i fastify-node-cronUsage
Add it to you project with register and you are done!
const fastify = require('fastify')()
fastify.register(require('fastify-node-cron'), {
workersDir: path.join(__dirname, 'workers')
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})Basic Worker Example
Into workers directory:
// primary-worker.js
module.exports = class PrimaryWorker {
constructor (fastify) {
this.fastify = fastify
this.name = 'primary'
this.cron = '*/5 * * * * *' // 5 seconds
}
async handler () {
this.fastify.log.info(`worker primary running ${Date.now()}`)
}
}Advanced Worker Features
// advanced-worker.js
module.exports = class AdvancedWorker {
constructor (fastify) {
this.fastify = fastify
this.name = 'advanced'
this.cron = '*/10 * * * * *' // 10 seconds
// Optional: node-cron options
this.options = {
scheduled: true,
timezone: 'America/Sao_Paulo',
noOverlap: true, // Prevent overlapping executions (node-cron v4 native feature)
maxExecutions: 5 // Limit number of executions (node-cron v4 native feature)
}
}
async handler () {
// Your worker logic here
this.fastify.log.info(`worker ${this.name} running ${Date.now()}`)
// Simulate work that might fail
if (Math.random() < 0.3) {
throw new Error('Simulated failure')
}
}
}Worker Configuration
Each worker class must implement:
name(string): Unique identifier for the workercron(string): Valid cron expression for schedulinghandler(async function): The work to be executed
Optional properties:
options(object): Options passed to node-cron schedule method, including:noOverlap(boolean): Prevent overlapping executionsmaxExecutions(number): Limit number of executionstimezone(string): Timezone for the cron schedulescheduled(boolean): Whether the task should be scheduled immediately- And other node-cron v4 options
Plugin Configuration
workersDir(string, required): Directory containing worker files
API
After registration, the plugin decorates Fastify with a scheduler object:
// Access the scheduler
fastify.scheduler.cron // node-cron reference
fastify.scheduler.workers // Object containing all registered workers
// Worker control methods
const worker = fastify.scheduler.workers['worker-name']
await worker.task.stop() // Stop the scheduled task
await worker.task.start() // Start the scheduled task
await worker.task.execute() // Run the task immediately
await worker.task.getStatus() // Get task status: 'idle', 'running', 'stopped', etc.Documentation
More details about node-cron documentation, see node-cron docs
