var spinlock = require('mongodb-spinlock');
var lock = spinlock(collection, { ttl: 1, id: 'default' });
lock.acquire(function(release, renew) {
doSomethingAsync(function(err) {
if (err) {
release(err);
return;
}
// extend the expiry time
renew(function(err) {
doSomethingElseAsync(release);
});
});
}, function done(err) {
// either your operation is done and the lock is released or an error happened, either because an err was passed to release,
// or an internal error e.g. database problem updating the lock
});