0.1.0 • Published 1 year ago
fastify-node-cron v0.1.0
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
})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()}`)
  }
}Documentation
More details about node-cron documentation, see node-cron docs
License
0.1.0
1 year ago