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

dpa-distributed-lock-cron

v1.0.0

Published

Distributed Lock decorator for NestJS cron jobs using Redis - prevents duplicate execution across replicas

Readme

dpa-distributed-lock-cron

npm version License: MIT

Distributed Lock decorator for NestJS cron jobs using Redis. Prevents duplicate cron job execution in multi-replica environments (Docker Swarm, Kubernetes, PM2 cluster).

✨ Features

  • 🔒 Atomic Locking - Uses Redis SET NX PX for race-condition-free locking
  • 🎯 Simple Decorator - Just add @DistributedLock() to your cron method
  • 🔄 Auto Release - Lock is automatically released after method execution
  • 🛡️ Fail-Open - Allows execution if Redis is unavailable (configurable)
  • 📦 Zero Config - Works out of the box with environment variables

📦 Installation

npm install dpa-distributed-lock-cron ioredis

🚀 Quick Start

1. Import Module

// app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { DistributedLockModule, distributedLockConfig } from 'dpa-distributed-lock-cron';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [distributedLockConfig],
    }),
    DistributedLockModule,
  ],
})
export class AppModule {}

2. Set Environment Variables

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=       # optional
REDIS_DB=0            # optional, default: 0
REDIS_KEY_PREFIX=     # optional, e.g., "myapp:"

3. Use the Decorator

import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { DistributedLock } from 'dpa-distributed-lock-cron';

@Injectable()
export class MyService {
  @Cron(CronExpression.EVERY_DAY_AT_6AM)
  @DistributedLock('cron:daily-sync', 300000)  // key, TTL 5 minutes
  async dailySync(): Promise<void> {
    // ✅ Only ONE replica will execute this
    console.log('Running daily sync...');
  }
}

📖 API Reference

@DistributedLock(keyName: string, ttlMs: number)

Decorator to wrap your cron method with distributed locking.

| Parameter | Type | Description | |-----------|------|-------------| | keyName | string | Unique lock key (e.g., 'cron:my-job') | | ttlMs | number | Lock TTL in milliseconds (safety timeout) |

DistributedLockService

Injectable service for manual lock management.

import { DistributedLockService } from 'dpa-distributed-lock-cron';

@Injectable()
export class MyService {
  constructor(private readonly lockService: DistributedLockService) {}

  async manualLockExample() {
    const acquired = await this.lockService.acquireLock('my-lock', 60000);
    if (acquired) {
      try {
        // do work
      } finally {
        await this.lockService.releaseLock('my-lock');
      }
    }
  }
}

| Method | Returns | Description | |--------|---------|-------------| | acquireLock(key, ttlMs) | Promise<boolean> | Acquire lock, returns true if acquired | | releaseLock(key) | Promise<void> | Release lock (only if owned) | | isConnected() | boolean | Check Redis connection status |

⚙️ How It Works

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  Replica 1  │     │  Replica 2  │     │  Replica 3  │
└──────┬──────┘     └──────┬──────┘     └──────┬──────┘
       │                   │                   │
       │ SET lock NX PX    │ SET lock NX PX    │ SET lock NX PX
       │                   │                   │
       ▼                   ▼                   ▼
   ┌───────────────────────────────────────────────┐
   │                    REDIS                       │
   │  lock:cron:daily-sync = "replica-1-uuid"      │
   └───────────────────────────────────────────────┘
       │                   │                   │
       ▼                   ▼                   ▼
   ✅ Execute          ❌ Skip             ❌ Skip
  1. Before method execution, decorator tries to acquire lock using Redis SET key NX PX ttl
  2. If lock acquired (OK): method executes, then lock is released
  3. If lock not acquired (null): method is skipped silently
  4. Lock uses Lua script for safe release (only the owner can release)

🛡️ Fail-Open Behavior

If Redis is unavailable, the decorator allows execution (fail-open) to prevent blocking all replicas. This ensures your cron jobs continue to run even during Redis outages.

📋 Requirements

  • Node.js >= 18.0.0
  • NestJS >= 10.0.0
  • Redis >= 6.0

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

MIT © DPA Team