This module provides a way to configure scheduled workers using node-cron
npm i fastify-node-cron
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
})
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()}`)
}
}
// secondary-worker.js
module.exports = class SecondaryWorker {
constructor (fastify) {
this.fastify = fastify
this.name = 'secondary'
this.cron = '*/10 * * * * *' // 10 seconds
}
async handler () {
this.fastify.log.info(`worker secondary running ${Date.now()}`)
}
}
More details about node-cron
documentation, see node-cron docs