lockup

Lockup uses a certain lock file to queue and deal with inter-process asynchronous resources.

Lockup

Lockup uses a certain lock file to queue and deal with inter-process asynchronous resources.

npm install lockup --save
var lockup = require('lockup');

Create a lock if the lock is not existed, and run the callback function.

If the lock already exists, callback will put in a queue which will be executed and cleaned up once the lock is released.

Notice that, lockup.lock could work with multiple processes.

The path of the lock file.

Optional, default to {};

The callback function.

Error instance or error message.

Release a lock.

var file = 'foo.lock';
var counter = 0;
 
function foo(delay){
    var c = counter ++;
 
    lockup.lock(file, function (err) {
        setTimeout(function () {
            console.log('counter', c);
            
            // release the lock. 
            lockup.unlock(file);
        }, delay);
    });
}
 
foo(1000);
foo(0); // without `lockup.lock`, "counter 1" will be printed first. 
 
// Then print: 
// counter 0 
// counter 1