# process-locker

> Base on redis, same resource should only be processed once. Requests about the resource will be pending until process is complete and then the result will be returned.

Latest version **0.3.2** (published 2019-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install process-locker
pnpm add process-locker
yarn add process-locker
bun add process-locker
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2019-02-18 |
| First published | 2016-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Orange Mi |
| Maintainers | orangemi |

## Links

- npm: https://www.npmjs.com/package/process-locker
- Repository: https://github.com/orangemi/process-locker
- Issues: https://github.com/orangemi/process-locker/issues
- npm.io page: https://npm.io/package/process-locker

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^3.1.0
- [thunk-redis](https://npm.io/package/thunk-redis.md) ^2.1.6

## Recent versions

- 0.3.2 (latest) — 2019-02-18
- 0.3.1 — 2019-01-21
- 0.3.0 — 2018-01-26
- 0.2.5 — 2016-10-18
- 0.2.4 — 2016-03-17
- 0.2.3 — 2016-03-17
- 0.2.2 — 2016-03-08
- 0.2.1 — 2016-03-07
- 0.2.0 — 2016-03-07
- 0.1.2 — 2016-01-19
- 0.1.1 — 2016-01-18
- 0.1.0 — 2016-01-15

## README

Process-Locker
==============
Base on redis, one resource only be processed once. Requests about the resource will be pending until process is complete and then the result will be returned.
基于redis，同一个资源仅会被处理一次，如果一个资源正在处理，当前请求都会被挂起，待处理完成后返回结果。

## Usage:
```
var Locker = require('process-locker')
var locker = Locker()
var key = 'resource-key'
locker.request(key)(function (err, resp) {
  var result
	if (resp.status === Locker.LOCKED) {
    // do the process
    locker.publish(key, result)()
  } else if (resp.status === Locker.DONE) {
    result = resp.result
  }
  // process result
})
```

## API:
### Locker(options)
return a locker instance
options:
- **redis** *thunk-redis-client* or *string* use [thunk-redis](https://github.com/thunks/thunk-redis) client to lock and publish events. Default use: `localhost:6379`
- **subRedis** *thunk-redis-client* or *string* use [thunk-redis](https://github.com/thunks/thunk-redis) client to subscribe events. Default use: `localhost:6379`
- **redisPrefix** *String* Default: `locker`
- **channel** *String* channel name for redis subscribe Default: `channel`
- **resultTimeout** *Number* milliseconds to cache the process result Default: `30 * 60 * 1000`
- **lockTimeout** *Number* milliseconds to lock the process result Default: `60 * 60 * 1000`

### Locker.LOCKED
locker locked the key name and you have the only permission to process the resource. So you are the real processor.
### Locker.DONE
locker received the real processor signal and notice all requests.

### locker.request (key)
Request status for process named `key`. If status is `Locker.status.LOCKED`, means you become the real processor. Return a thunk function (use [thunks](https://github.com/thunks/thunks)) `callback(err, resp)`
- **key** *String* the name of the process.
- **resp** *Object*
	- **status** [Locker.LOCKED](#Locker.LOCKED) or [Locker.DONE](#Locker.DONE)
	- **result** *Object* the result published by the real processor

### locker.publish (key, result, timeout)
return a thunk function (use [thunks](https://github.com/thunks/thunks)) `callback(err)`
- **key** *String* name of the process.
- **result** *Object* result used to notice all requests
- **timeout** *Number* milliseconds to cache the process result that will publish for other request

---
_Source: https://npm.io/package/process-locker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
