The node.js implementation of dotlockfile with some differences
npm i dotlockfile
// ES6
{ create, check, touch } from 'dotlockfile';
async stuff() {
try {
const remove = await create(somePath, { pid: process.pid });
//do stuff
await remove();
} catch (e) {
errorHandler(e);
}
}
//ES5
const dtf = require('dotlockfile');
dtf.create(somePath, { pid: process.pid }).then(remove => {
//do stuff
return remove()
})
.catch(errorHandler);
Compatible node v4+ only
AFAIK:
- retries (
-r
) defaults to 0 - retries (
-r
) === -1 just won't work, no infinite loop. - no
-m
option (mail)
all path parameters defaults to `${process.pid}.lock`
Create a lockfile.
options defaults to:
{
retries: 0,
pid: null,
}
Resolves to a function to remove your lock if you acquired the lock.
Rejects to an error with reason in the message if it couldn't create the lock.
Remove lockfile.
Resolve if you removed the lock (or lock isn't there).
Rejects if it couldn't remove the lock.
Check lockfile validity.
Resolves to true if lockfile is valid.
Rejects to an error with invalid lock message if lockfile is invalid (does not automatically remve the lock).
Update utime of lockfile
I looked for an obvious dotlockfile equivalent and I didn't find it, or I find some stuff but it didn't have obvious tests to show that it's actually working ...
MIT