Socket
Book a DemoInstallSign in
Socket

@s1seven/nestjs-tools-lock

Package Overview
Dependencies
Maintainers
5
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@s1seven/nestjs-tools-lock

Lock service

latest
Source
npmnpm
Version
0.7.0
Version published
Weekly downloads
13
62.5%
Maintainers
5
Weekly downloads
 
Created
Source

Lock

npm

The Lock Service produce distributed locks to prevent duplicated/conflicted actions to be executed in a distributed environment. It is based on Redlock module.

Installation

$ npm install --save @s1seven/nestjs-tools-lock

Usage

Inside a CronJob service

import { LockService } from '@s1seven/nestjs-tools-lock';
import { Inject, Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';

@Injectable()
export class TasksService implements OnModuleDestroy {
  constructor(@Inject(LockService) private lockService: LockService) {}

  onModuleDestroy() {
    this.lockService.close();
  }

  // using returned unlock function
  @Cron('*/10 * * * * *')
  async doThis(): Promise<void> {
    const lockKey = 'doThis';
    const { unlock } = await this.lockService.lock(lockKey, 2000);
    if (typeof unlock !== 'function') {
      return;
    }
    unlock();
  }

  // using lock id
  @Cron('*/10 * * * * *')
  async doThat(): Promise<void> {
    const lockKey = 'doThat';
    await this.lockService.optimist(lockKey, 2000);
    const { lockId } = await this.lockService.lock(lockKey, 2000);
    this.lockService.unlock(lockKey, lockId);
  }
}

FAQs

Package last updated on 04 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts