ttlfs

A file-system-based mutex, with a TTL.

ttlfs

Create a lock-file with a TTL.

what is it good for?

When you want to perform an operation only once in a given block of time, from a single process, on a single server, e.g., during an NRPE check.

what isn't it good for?

When you have multiple processes, in parallel, attempting to acquire a lock.

var ttlfs = require('ttlfs');
 
// check the ttl on a lock. 
ttlfs.ttl('/tmp/awesome.lock', function(errttl) {
  if (!ttl) {
    // the lock has expired, we can create 
    // a new lock. 
    ttlfs.lock('my/tmp/awesome.lock', function(err) {
      if (!err) {
        console.log('we got a lock!');
      }
    });
  }
});
 
// there is no need to check the ttl. 
// an error will occur if the lock can't 
// be fetched. 
ttlfs.lock('/tmp/awesome.lock', function(errttl) {
  if (!err) {
    // do something with the lock. 
  } else {
    // we could not get the lock. 
  }
});

ttlfs accepts the following configuraiton settings:

  • expiry: when, in ms, should the lock expire? default 60000.
var ttlfs = require('ttlfs').configure({
  expiry: 1800000
});