2.0.1 • Published 8 years ago

redis-mutex v2.0.1

Weekly downloads
178
License
MIT
Repository
github
Last release
8 years ago

REDIS-MUTEX

Nodejs mutex implementation using the redis' set nx/xx/px commands.

USAGE

var redis = require("redis");
var client = redis.createClient();

var redisMutex = require("redis-mutex");
var runWithMutex = redisMutex.runWithMutex;

runWithMutex({
    intervalTime: 2000,
    renewTime: 100,
    maxTime: 10000,
    key: "PROCESS_1",
    redisClient: client
}, function(cb) {
    // this is the code of our function
    setTimeout(cb, 1000);
})
.then(function() {
    return "OK";
}, function() {
    return "REJECTED";
});

API

runWithMutex()

Arguments

nametypedescription
inervalTimenumberThe granularity at which the mutex updates itself in milliseconds.
renewTimenumberHow many ms before intervalTime a renew request is sent to redis.
maxTimenumberThe maximum time the function is allowed to run before it is killed.
keystringA string label of the mutex. Only one function with the same key is allowed to run.
redisClientobjectInstance of redisClient from redis, providing the redis connection.
fnfunctionThe actual processing function.

Return value

The function returns a promise. The promise is rejected when the mutex is locked or fn calls the callback with an error. The promise is resolved once fn calls the callback without an error.

Error states

In case the redis record can't be updated the whole process is terminated since something had to go wrong. When the maxTime is exceeded the process is also terminated since there is no way in nodejs to terminate the function.

2.0.1

8 years ago

2.0.0

8 years ago

0.0.1

9 years ago