Skip to content

This module provides a way to configure scheduled workers using node-cron

License

Notifications You must be signed in to change notification settings

leandroandrade/fastify-node-cron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastify-node-cron

This module provides a way to configure scheduled workers using node-cron

Install

npm i fastify-node-cron

Usage

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

MIT License

About

This module provides a way to configure scheduled workers using node-cron

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published