Stéphane Bnn
Stéphane Bnn

Reputation: 75

Redlock error in node js

I am trying to use redlock module but I am facing an error that I can't get through.

Here is what my redlock file looks like :

var redis = require('redis');
var Redlock = require('redlock');
var logger = require('./logger.js');

var client;

module.exports.redisConnection = function(port,host){
    client = redis.createClient(port,host);

    client.on('connect',function(){
        logger.info("Redis default connection open to "+host+":"+port);
    });

    client.on('error',function(err){
        logger.info("Redis default connection error "+err);
        logger.info("Redis Path : "+host+":"+port);
    });

    process.on('SIGINT', function() {  
        client.quit();      
        logger.info("Redis default connection disconnected");
        process.exit(0);
    }); 
};

var redlock = new Redlock(
    [client],
    {
        driftFactor : 0.01,
        retryCount : 15,
        retryDelay : 200
    }
);

redlock.on('clientError', function(err) {
    logger.info("A Redis Error Has Occurred : "+err);
});

module.exports.lockRessource = function(ressource_id,callback){
    redlock.lock(ressource_id,2000,function(err,lock){
        if(err){
            callback(err,null);
        }
        else{
            callback(null,lock);
        }
    });
};

module.exports.unlockLock = function(lock,callback){

    lock.unlock(function(err){
        if(err){
            callback(true,null);
        }
        else{
            callback(null,true);
        }
    }); 
};

When I try calling the lockRessource function from another module, I get an error :

2016-04-14T16:28:55.020 - info: TypeError: Cannot read property 'set' of undefined at request (/usr/app/node_modules/redlock/redlock.js:260:18) at /usr/app/node_modules/redlock/redlock.js:314:12 at Array.forEach (native) at attempt (/usr/app/node_modules/redlock/redlock.js:313:24) at /usr/app/node_modules/redlock/redlock.js:318:10 at Promise._execute (/usr/app/node_modules/redlock/node_modules/bluebird/js/release/debuggability.js:272:9) at Promise._resolveFromExecutor (/usr/app/node_modules/redlock/node_modules/bluebird/js/release/promise.js:474:18) at new Promise (/usr/app/node_modules/redlock/node_modules/bluebird/js/release/promise.js:77:14) at Redlock._lock (/usr/app/node_modules/redlock/redlock.js:249:9) at Redlock.lock (/usr/app/node_modules/redlock/redlock.js:111:14) at Object.module.exports.lockRessource (/usr/app/redisdata.js:42:10)

Do you have any idea where this could come from ? It seems like I am not doing anything crazy with that module.

Thanks a lot !

Upvotes: 2

Views: 6441

Answers (1)

sel-fish
sel-fish

Reputation: 4476

Init redlock after client was assigned.

Your redlock file will look like:

var redis = require('redis');
var Redlock = require('redlock');
var logger = require('./logger.js');

var client;
var redlock;

module.exports.redisConnection = function(port,host){
    client = redis.createClient(port,host);
    redlock = new Redlock(
        [client],
        {
            driftFactor : 0.01,
            retryCount : 15,
            retryDelay : 200
        }
    );

    client.on('connect',function(){
        logger.info("Redis default connection open to "+host+":"+port);
    });

    client.on('error',function(err){
        logger.info("Redis default connection error "+err);
        logger.info("Redis Path : "+host+":"+port);
    });

    redlock.on('clientError', function(err) {
        logger.info("A Redis Error Has Occurred : "+err);
    });

    process.on('SIGINT', function() {
        client.quit();
        logger.info("Redis default connection disconnected");
        process.exit(0);
    });
};

module.exports.lockRessource = function(ressource_id,callback){
    redlock.lock(ressource_id,2000,function(err,lock){
        if(err){
            callback(err,null);
        }
        else{
            callback(null,lock);
        }
    });
};

module.exports.unlockLock = function(lock,callback){

    lock.unlock(function(err){
        if(err){
            callback(true,null);
        }
        else{
            callback(null,true);
        }
    });
};

When you want to use that module, call redisConnection first.
Assume the name of the file above is "redlock.js".

resource = require('./redlock.js')

resource.redisConnection(6379, "127.0.0.1")
resource.lockRessource("your resourceId", function(err, lock) { })

Upvotes: 1

Related Questions