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

prod-cron-safe

v1.0.0

Published

A production-ready wrapper over node-cron with retries, overlap prevention, and timeout support.

Readme

🛡️ Prod-Cron-Safe

npm version License: ISC

A battle-tested, production-ready wrapper for node-cron. Stop worrying about overlapping tasks, thundering herds, or hung processes. Prod-Cron-Safe adds the resilience and observability your production environment demands.

Key Features

  • Intelligent Retries: Exponential backoff with jitter to protect your services.
  • Overlap Prevention: Ensure your tasks don't step on each other.
  • Execution Timeouts: Automatically kill tasks that take too long.
  • Fail-Fast Validation: Instantly validates cron expressions at schedule time.
  • Task Registry: Monitor and manage all active jobs from a single interface.
  • Graceful Shutdown: Automatic cleanup on SIGINT and SIGTERM.
  • Rich Observability: Detailed execution history, durations, and lifecycle hooks.
  • TypeScript First: Fully typed for a good developer experience.

📦 Installation

npm install prod-cron-safe

🛠️ Basic Usage

import { scheduleProdCron } from "prod-cron-safe";

const job = scheduleProdCron("*/5 * * * *", async () => {
    // Your mission-critical code here
    console.log("Daily cleanup started...");
}, {
    name: "DailyCleanup",
    preventOverlap: true,
    retries: 3
});

job.start();

⚙️ Advanced Configuration (Options)

| Option | Type | Default | Description | | :--- | :--- | :--- | :--- | | name | string | "unnamed-task" | Identification for logging and management. | | preventOverlap | boolean | false | If true, skips execution if the task is already running. | | executionTimeout| number | undefined | Timeout in ms. Kills the task if exceeded. | | retries | number | 0 | Number of retry attempts on failure. | | retryDelay | number | 1000 | Base delay in ms for retries. | | maxRetryDelay | number | 30000 | Ceiling for exponential backoff (ms). | | useExponentialBackoff | boolean | true | Enables exponential backoff (delay * 2^attempt). | | jitter | boolean | true | Adds random noise to delay (prevents thundering herd). |


🪝 Lifecycle Hooks

Every hook receives a TaskContext containing the task ID, name, current attempt, and start time.

scheduleProdCron("0 0 * * *", myTask, {
    onStart: (ctx) => console.log(`[${ctx.name}] Started (Attempt ${ctx.attempt})`),
    
    onSuccess: (result, ctx) => {
        console.log(`[${ctx.name}] Finished successfully in ${Date.now() - ctx.startTime.getTime()}ms`);
    },
    
    onError: (err, ctx) => {
        console.error(`[${ctx.name}] Failed after all retries: ${err.message}`);
    },
    
    onRetry: (err, attempt, delay, ctx) => {
        console.warn(`[${ctx.name}] Retry ${attempt} in ${Math.round(delay)}ms due to: ${err.message}`);
    }
});

📊 Management & Monitoring

Global Registry

Access all registered tasks to monitor their state or history.

import { getRegisteredTasks, stopAll } from "prod-cron-safe";

// get status of all tasks
const activeTasks = getRegisteredTasks();
activeTasks.forEach(task => {
    console.log(`${task.name} (${task.id}) - Running: ${task.isRunning}`);
    console.table(task.history); // Last 20 executions
});

// kill everything gracefully
stopAll();

Manual Trigger

Need to run a task right now? Use .trigger() without waiting for the cron schedule.

const job = scheduleProdCron("0 0 * * *", cleanup);
await job.trigger(); // Runs manually, returns Promise with result

🛡️ Production Best Practices

Graceful Shutdown

Prod-Cron-Safe automatically listens for SIGINT and SIGTERM to stop all jobs. This ensures your app exits cleanly without leaving dangling timers.

Error Handling

Always return a Promise from your task function. Rejections will trigger the retry logic automatically.

History Limits

For memory safety, only the last 20 execution records are kept per task.


📜 License

ISC © 2026