# @s1seven/nestjs-tools-lock

> Lock service

Latest version **0.7.0** (published 2023-09-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install @s1seven/nestjs-tools-lock
pnpm add @s1seven/nestjs-tools-lock
yarn add @s1seven/nestjs-tools-lock
bun add @s1seven/nestjs-tools-lock
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2023-09-04 |
| First published | 2022-03-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 118.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | getlarge |
| Maintainers | christophbuehler, eamon0989, s1seven-service, stiebitzhofer, getlarge |

## Links

- npm: https://www.npmjs.com/package/@s1seven/nestjs-tools-lock
- Repository: https://github.com/s1seven/nestjs-tools
- Homepage: https://github.com/s1seven/nestjs-tools/tree/main/packages/lock#readme
- Issues: https://github.com/s1seven/nestjs-tools/issues
- npm.io page: https://npm.io/package/@s1seven/nestjs-tools-lock

## Dependencies (2)

- [redlock](https://npm.io/package/redlock.md) ^5.0.0-beta.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 0.7.0 (latest) — 2023-09-04
- 0.6.1 — 2022-12-21
- 0.6.0 — 2022-09-09
- 0.5.0 — 2022-08-09
- 0.4.4 — 2022-06-02
- 0.4.3 — 2022-03-17

## README

# Lock

[![npm][npm-image]][npm-url]

[npm-image]: https://img.shields.io/npm/v/@s1seven/nestjs-tools-lock.svg?style=flat
[npm-url]: https://npmjs.org/package/@s1seven/nestjs-tools-lock

The Lock Service produce distributed locks to prevent duplicated/conflicted actions to be executed in a distributed environment.
It is based on [Redlock](https://www.npmjs.com/package/redlock) module.

## Installation

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

## Usage

### Inside a CronJob service

```ts
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);
  }
}
```

---
_Source: https://npm.io/package/@s1seven/nestjs-tools-lock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
