npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

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

Readme

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
})

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 worker
  • cron (string): Valid cron expression for scheduling
  • handler (async function): The work to be executed

Optional properties:

  • options (object): Options passed to node-cron schedule method, including:
    • noOverlap (boolean): Prevent overlapping executions
    • maxExecutions (number): Limit number of executions
    • timezone (string): Timezone for the cron schedule
    • scheduled (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

License

MIT License