Node.js lock module

An elasticsearch based lock mechanism for resources and ownership.
Introduction
This module provides abstraction methods to lock a resource with an owner. The owner is the name of the process requesting a lock. The resource would be the specific element/index/endpoint/etc.. that requires locking.
Installation
NPM: npm install enrise-lock --save
Yarn: yarn add enrise-lock
Initialization
Require and instantiate the locker.
const lock = new require('enrise-lock')(config: Object);
Where config
is an object with the following options. Type is optional, the rest is mandatory.
esClient: elasticsearch
: An elasticsearch client for the lock module to use. This can either be an elasticsearch or an enrise-esclient instance.index: String
: The elasticsearch index that will be used to store all lock-documents.owner: String
: The name of the process that is looking to place the lock.[type: String]
: The index-type that will be used to store all lock-documents. This will default to 'lockdocument'
.
Usage and API methods
lock.acquire(resource: String, callback: function)
Set a lock for resource: resource
. Callback will be called with parameters [err, success: boolean]
. The success paramater will be false if a lock already exists.
lock.release(resource: String, callback: function)
Release a lock for resource: resource
. Callback will be called with parameter [err]
.
lock.isLocked(resource: String, callback: function)
Check if a resource is locked. Callback will be called with parameters [err, isLocked: boolean]
.
lock.list(callback: function)
Retrieve a list of all current locks. Callback will be called with parameters [err, locks: Object]
. Where the key in locks represents the resource and the value an object with the owner.
lock.delete(callback: function)
Delete the entire lock-index. Callback will be called with parameter [err]
.
Example
const lock = new require('enrise-lock')({
esClient: new require('elasticsearch').Client(),
index: 'globallock',
type: 'lockdocument',
owner: 'api'
});
lock.acquire('my-resource', (err, success) => {
lock.release('my-resource', (err, success) => {
});
});