lockfile
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -54,12 +54,4 @@ var fs = require('fs') | ||
// best-effort. unlocking an already-unlocked lock is a noop | ||
if (hasOwnProperty(locks, path)) | ||
fs.close(locks[path], unlink) | ||
else | ||
unlink() | ||
function unlink () { | ||
debug('unlink', path) | ||
delete locks[path] | ||
fs.unlink(path, function (unlinkEr) { cb() }) | ||
} | ||
delete locks[path] | ||
fs.unlink(path, function (unlinkEr) { cb() }) | ||
} | ||
@@ -70,3 +62,2 @@ | ||
// best-effort. unlocking an already-unlocked lock is a noop | ||
try { fs.closeSync(locks[path]) } catch (er) {} | ||
try { fs.unlinkSync(path) } catch (er) {} | ||
@@ -122,3 +113,3 @@ delete locks[path] | ||
if (!opts.stale) { | ||
fs.closeSync(fd) | ||
try { fs.closeSync(fd) } catch (er) {} | ||
return true | ||
@@ -166,3 +157,5 @@ } | ||
locks[path] = fd | ||
return cb(null, fd) | ||
return fs.close(fd, function () { | ||
return cb() | ||
}) | ||
} | ||
@@ -236,4 +229,5 @@ | ||
locks[path] = fd | ||
try { fs.closeSync(fd) } catch (er) {} | ||
debug('locked sync!', path, fd) | ||
return fd | ||
return | ||
} catch (er) { | ||
@@ -240,0 +234,0 @@ if (er.code !== 'EEXIST') return retryThrow(path, opts, er) |
{ | ||
"name": "lockfile", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"main": "lockfile.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -12,6 +12,6 @@ # lockfile | ||
// opts is optional, and defaults to {} | ||
lockFile.lock('some-file.lock', opts, function (er, fd) { | ||
lockFile.lock('some-file.lock', opts, function (er) { | ||
// if the er happens, then it failed to acquire a lock. | ||
// if there was not an error, then the fd is opened in | ||
// wx mode. If you want to write something to it, go ahead. | ||
// if there was not an error, then the file was created, | ||
// and won't be deleted until we unlock it. | ||
@@ -37,3 +37,3 @@ // do my stuff, free of interruptions | ||
Acquire a file lock on the specified path. Returns the FD. | ||
Acquire a file lock on the specified path | ||
@@ -40,0 +40,0 @@ ### lockFile.lockSync(path, [opts]) |
@@ -161,31 +161,2 @@ var test = require('tap').test | ||
test('watch test', function (t) { | ||
var opts = { wait: 100 } | ||
var fdx | ||
lockFile.lock('watch-lock', function (er, fd1) { | ||
if (er) throw er | ||
setTimeout(unlock, 10) | ||
function unlock () { | ||
console.error('unlocking it') | ||
lockFile.unlockSync('watch-lock') | ||
// open another file, so the fd gets reused | ||
// so we can know that it actually re-opened it fresh, | ||
// rather than just getting the same lock as before. | ||
fdx = fs.openSync('x', 'w') | ||
fdy = fs.openSync('x', 'w') | ||
} | ||
// should have gotten a new fd | ||
lockFile.lock('watch-lock', opts, function (er, fd2) { | ||
if (er) throw er | ||
t.notEqual(fd1, fd2) | ||
fs.closeSync(fdx) | ||
fs.closeSync(fdy) | ||
fs.unlinkSync('x') | ||
lockFile.unlockSync('watch-lock') | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
test('retries', function (t) { | ||
@@ -206,6 +177,5 @@ // next 5 opens will fail. | ||
lockFile.lock('retry-lock', { retries: opens }, function (er, fd) { | ||
lockFile.lock('retry-lock', { retries: opens }, function (er) { | ||
if (er) throw er | ||
t.equal(opens, 0) | ||
t.ok(fd) | ||
lockFile.unlockSync('retry-lock') | ||
@@ -232,6 +202,5 @@ t.end() | ||
var opts = { retries: opens, retryWait: 100 } | ||
lockFile.lock('retry-lock', opts, function (er, fd) { | ||
lockFile.lock('retry-lock', opts, function (er) { | ||
if (er) throw er | ||
t.equal(opens, 0) | ||
t.ok(fd) | ||
lockFile.unlockSync('retry-lock') | ||
@@ -238,0 +207,0 @@ t.end() |
18676
476