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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nats-scaled-scheduler

v1.3.0

Published

A scalable scheduler using NATS JetStream

Downloads

249

Readme

NATS Scaled Scheduler

nats-scaled-scheduler provides two powerful distributed systems built on top of NATS JetStream:

  1. A distributed job scheduler for running cron-based tasks
  2. A distributed job queue for processing work across multiple instances

Installation

npm install nats-scaled-scheduler

Scheduler Usage

The scheduler allows you to run cron-based jobs across multiple instances in a fault-tolerant manner.

import { createNatsScheduler } from 'nats-scaled-scheduler';

const scheduler = await createNatsScheduler({
  nats: {
    servers: ['localhost:4222'],
    user: 'a',
    pass: 'a',
  },
  streamName: 'MY_SCHEDULER'
});

// Add a job that runs every minute
await scheduler.addJob(async (data) => {
  console.log('Scheduled job running:', data);
}, '* * * * *', 'myJob');

// Remove a job
await scheduler.removeJob('myJob');

// Shutdown
await scheduler.shutdown();

Scheduler API

  • createNatsScheduler(options)

    • options.nats: NATS connection options or existing connection
    • options.streamName: Name for the scheduler stream
  • scheduler.addJob(fn, cron, name)

    • fn: Async function to execute
    • cron: Cron expression
    • name: Unique job name
  • scheduler.removeJob(name)

  • scheduler.healthCheck()

  • scheduler.shutdown()

Queue Usage

The queue system allows you to process jobs across multiple workers with features like retries and concurrency control.

import { createNatsQueue } from 'nats-scaled-scheduler';

const queue = await createNatsQueue({
  nats: {
    servers: ['localhost:4222'],
    user: 'a',
    pass: 'a',
  },
  name: 'MY_QUEUE'
});

// Add a job processor
queue.process('emails', { concurrency: 5 }, async (job) => {
  await sendEmail(job.data);
});

// Add jobs to the queue
await queue.push('emails', {
  to: '[email protected]',
  subject: 'Hello'
}, {
  priority: 'high',
  delay: '5m',
  retries: 3
});

// Process multiple items
await queue.pushBatch('emails', [
  { to: '[email protected]' },
  { to: '[email protected]' }
]);

// Get queue stats
const stats = await queue.getStats('emails');
console.log(stats);

// Shutdown
await queue.shutdown();

Queue API

  • createNatsQueue(options)

    • options.nats: NATS connection options or existing connection
    • options.name: Name for the queue stream
  • queue.push(queueName, data, options)

    • queueName: Name of the queue
    • data: Job payload
    • options.priority: 'low' | 'medium' | 'high'
    • options.delay: Delay time (e.g., '5m', '1h')
    • options.retries: Number of retry attempts
  • queue.pushBatch(queueName, items, options)

  • queue.process(queueName, options, handler)

    • options.concurrency: Number of concurrent jobs
  • queue.pause(queueName)

  • queue.resume(queueName)

  • queue.clear(queueName)

  • queue.getStats(queueName)

  • queue.shutdown()

Events

The queue emits the following events:

  • 'completed': When a job completes successfully
  • 'error': When a job fails

Running Tests

npm test

Docker Setup

To run a local NATS server:

docker-compose up

This will start the required NATS servers in a cluster configuration.