Git Product home page Git Product logo

Comments (5)

ajay2611 avatar ajay2611 commented on July 26, 2024

I basically want this function to be executed by one server only. So, I'm trying that whenever first server executes this cron, puts a lock on this function, so that 2nd server cannot execute this. Am I doing it right way?

from node-redlock.

mike-marcacci avatar mike-marcacci commented on July 26, 2024

Hi @ajay2611, it looks like you are returning too soon here, and your lock variable isn't in the scope of the second function; I'd definitely suggest reading up on how promises work, but here's what your function should look like:

var resource = 'redisKey';

function getData(){
    return redlock.lock(resource, ttl)
    .then(function(lock) {
        return Promise.resolve(logicToRun())
        .then(
            function(result) {
                // we can fire this off in the background here,
                // because we don't really care if it succeeds
                lock.unlock()
                .catch(function(errofFromUnlocking) {
                    console.log(errofFromUnlocking);
                });

                // let's make sure to return the result  
                return result;
            },
            function(errorFromLogic) {
                // we can fire this off in the background here,
                // because we don't really care if it succeeds
                lock.unlock()
                .catch(function(errofFromUnlocking) {
                    console.log(errofFromUnlocking);
                });

                // let's make sure to propagate the error
                throw errorFromLogic;
            }
        );
    });
});


function logicToRun() {
    // logic of function; it can return a promise OR a
    // resolved value, because it's called from inside
    // Promise.resolve()
}

// using your function:
getData()

// if you want something to run AFTER it's finished, chain with `then`:
.then(function(result) {
    // do something...
});

from node-redlock.

ajay2611 avatar ajay2611 commented on July 26, 2024

Thanks for a prompt reply. Implementing and testing it this way.
By the way, is this a correct usage case of redlock algo as I've only one redis client?

from node-redlock.

mike-marcacci avatar mike-marcacci commented on July 26, 2024

Yep! It can use multiple redis instances (which provides high availability since one of 3 could go down, etc) but it's perfectly normal to have just one.

from node-redlock.

ajay2611 avatar ajay2611 commented on July 26, 2024

Thanks for help @mike-marcacci . Closing this issue.

from node-redlock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.