diff options
author | isaacs <i@izs.me> | 2013-06-08 01:50:51 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-06-08 01:50:51 +0400 |
commit | 7523c3848302bff846a1c23a0ee92865a759f4ab (patch) | |
tree | fbc22fa678d11c1daa8791eae6912a88b73c93c6 /node_modules/lockfile | |
parent | 81740ce1c10105ead2b6f5a17aea15bcb88eaf25 (diff) |
lockfile@0.3.3
Diffstat (limited to 'node_modules/lockfile')
-rw-r--r-- | node_modules/lockfile/lockfile.js | 63 | ||||
-rw-r--r-- | node_modules/lockfile/package.json | 10 |
2 files changed, 54 insertions, 19 deletions
diff --git a/node_modules/lockfile/lockfile.js b/node_modules/lockfile/lockfile.js index 6752a0362..a88940808 100644 --- a/node_modules/lockfile/lockfile.js +++ b/node_modules/lockfile/lockfile.js @@ -1,11 +1,23 @@ var fs = require('fs') var wx = 'wx' -if (process.version.match(/^v0.[456]/)) { +if (process.version.match(/^v0\.[0-6]/)) { var c = require('constants') wx = c.O_TRUNC | c.O_CREAT | c.O_WRONLY | c.O_EXCL } +var debug +var util = require('util') +if (util.debuglog) + debug = util.debuglog('LOCKFILE') +else if (/\blockfile\b/i.test(process.env.NODE_DEBUG)) + debug = function() { + var msg = util.format.apply(util, arguments) + console.error('LOCKFILE %d %s', process.pid, msg) + } +else + debug = function() {} + var locks = {} function hasOwnProperty (obj, prop) { @@ -13,25 +25,31 @@ function hasOwnProperty (obj, prop) { } process.on('exit', function () { + debug('exit listener') // cleanup Object.keys(locks).forEach(exports.unlockSync) }) // XXX https://github.com/joyent/node/issues/3555 // Remove when node 0.8 is deprecated. -process.on('uncaughtException', function H (er) { - var l = process.listeners('uncaughtException').filter(function (h) { - return h !== H +if (/^v0\.[0-8]/.test(process.version)) { + debug('uncaughtException, version = %s', process.version) + process.on('uncaughtException', function H (er) { + debug('uncaughtException') + var l = process.listeners('uncaughtException').filter(function (h) { + return h !== H + }) + if (!l.length) { + // cleanup + try { Object.keys(locks).forEach(exports.unlockSync) } catch (e) {} + process.removeListener('uncaughtException', H) + throw er + } }) - if (!l.length) { - // cleanup - try { Object.keys(locks).forEach(exports.unlockSync) } catch (e) {} - process.removeListener('uncaughtException', H) - throw er - } -}) +} exports.unlock = function (path, cb) { + debug('unlock', path) // best-effort. unlocking an already-unlocked lock is a noop if (hasOwnProperty(locks, path)) fs.close(locks[path], unlink) @@ -39,12 +57,14 @@ exports.unlock = function (path, cb) { unlink() function unlink () { + debug('unlink', path) delete locks[path] fs.unlink(path, function (unlinkEr) { cb() }) } } exports.unlockSync = function (path) { + debug('unlockSync', path) // best-effort. unlocking an already-unlocked lock is a noop try { fs.closeSync(locks[path]) } catch (er) {} try { fs.unlinkSync(path) } catch (er) {} @@ -56,6 +76,7 @@ exports.unlockSync = function (path) { // if the error is something other than ENOENT, then it's not. exports.check = function (path, opts, cb) { if (typeof opts === 'function') cb = opts, opts = {} + debug('check', path, opts) fs.open(path, 'r', function (er, fd) { if (er) { if (er.code !== 'ENOENT') return cb(er) @@ -83,6 +104,7 @@ exports.check = function (path, opts, cb) { exports.checkSync = function (path, opts) { opts = opts || {} + debug('checkSync', path, opts) if (opts.wait) { throw new Error('opts.wait not supported sync for obvious reasons') } @@ -113,14 +135,18 @@ exports.checkSync = function (path, opts) { +var req = 0 exports.lock = function (path, opts, cb) { if (typeof opts === 'function') cb = opts, opts = {} + opts.req = opts.req || req++ + debug('lock', path, opts) if (typeof opts.retries === 'number' && opts.retries > 0) { cb = (function (orig) { return function (er, fd) { if (!er) return orig(er, fd) var newRT = opts.retries - 1 opts_ = Object.create(opts, { retries: { value: newRT }}) + debug('lock retry', path, newRT) if (opts.retryWait) setTimeout(function() { exports.lock(path, opts_, orig) }, opts.retryWait) @@ -132,6 +158,7 @@ exports.lock = function (path, opts, cb) { // if this succeeds, then we're in business. fs.open(path, wx, function (er, fd) { if (!er) { + debug('locked', path, fd) locks[path] = fd return cb(null, fd) } @@ -146,6 +173,7 @@ exports.lock = function (path, opts, cb) { if (statEr.code === 'ENOENT') { // expired already! var opts_ = Object.create(opts, { stale: { value: false }}) + debug('lock stale enoent retry', path, opts_) exports.lock(path, opts_, cb) return } @@ -154,9 +182,11 @@ exports.lock = function (path, opts, cb) { var age = Date.now() - st.ctime.getTime() if (age > opts.stale) { + debug('lock stale', path, opts_) exports.unlock(path, function (er) { if (er) return cb(er) var opts_ = Object.create(opts, { stale: { value: false }}) + debug('lock stale retry', path, opts_) exports.lock(path, opts_, cb) }) } else notStale(er, path, opts, cb) @@ -166,6 +196,8 @@ exports.lock = function (path, opts, cb) { } function notStale (er, path, opts, cb) { + debug('notStale', path, opts) + // if we can't wait, then just call it a failure if (typeof opts.wait !== 'number' || opts.wait <= 0) return cb(er) @@ -176,17 +208,20 @@ function notStale (er, path, opts, cb) { var end = start + opts.wait function retry () { + debug('notStale retry', path, opts) var now = Date.now() var newWait = end - now var newOpts = Object.create(opts, { wait: { value: newWait }}) exports.lock(path, newOpts, cb) } - var timer = setTimeout(retry, 10) + var timer = setTimeout(retry, 1000) } exports.lockSync = function (path, opts) { opts = opts || {} + opts.req = opts.req || req++ + debug('lockSync', path, opts) if (opts.wait || opts.retryWait) { throw new Error('opts.wait not supported sync for obvious reasons') } @@ -194,6 +229,7 @@ exports.lockSync = function (path, opts) { try { var fd = fs.openSync(path, wx) locks[path] = fd + debug('locked sync!', path, fd) return fd } catch (er) { if (er.code !== 'EEXIST') return retryThrow(path, opts, er) @@ -212,12 +248,14 @@ exports.lockSync = function (path, opts) { } var age = Date.now() - ct if (age > opts.stale) { + debug('lockSync stale', path, opts, age) exports.unlockSync(path) return exports.lockSync(path, opts) } } // failed to lock! + debug('failed to lock', path, opts, er) return retryThrow(path, opts, er) } } @@ -225,6 +263,7 @@ exports.lockSync = function (path, opts) { function retryThrow (path, opts, er) { if (typeof opts.retries === 'number' && opts.retries > 0) { var newRT = opts.retries - 1 + debug('retryThrow', path, opts, newRT) var opts_ = Object.create(opts, { retries: { value: newRT }}) return exports.lockSync(path, opts_) } diff --git a/node_modules/lockfile/package.json b/node_modules/lockfile/package.json index 50cb72eec..6a5ee1fee 100644 --- a/node_modules/lockfile/package.json +++ b/node_modules/lockfile/package.json @@ -1,6 +1,6 @@ { "name": "lockfile", - "version": "0.3.2", + "version": "0.3.3", "main": "lockfile.js", "directories": { "test": "test" @@ -36,10 +36,6 @@ "bugs": { "url": "https://github.com/isaacs/lockfile/issues" }, - "_id": "lockfile@0.3.2", - "dist": { - "shasum": "542261743479f9f907be30441f5d6b95e8b95722" - }, - "_from": "lockfile@0.3.2", - "_resolved": "https://registry.npmjs.org/lockfile/-/lockfile-0.3.2.tgz" + "_id": "lockfile@0.3.3", + "_from": "lockfile@~0.3.2" } |