包详细信息

lockfile

npm7.1mISC1.0.4

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.

lockfile, lock, file, fs

自述文件

lockfile

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.

Usage

var lockFile = require('lockfile')

// opts is optional, and defaults to {}
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 file was created,
  // and won't be deleted until we unlock it.

  // do my stuff, free of interruptions
  // then, some time later, do:
  lockFile.unlock('some-file.lock', function (er) {
    // er means that an error happened, and is probably bad.
  })
})

Methods

Sync methods return the value/throw the error, others don't. Standard node fs stuff.

All known locks are removed when the process exits. Of course, it's possible for certain types of failures to cause this to fail, but a best effort is made to not be a litterbug.

lockFile.lock(path, [opts], cb)

Acquire a file lock on the specified path

lockFile.lockSync(path, [opts])

Acquire a file lock on the specified path

lockFile.unlock(path, cb)

Close and unlink the lockfile.

lockFile.unlockSync(path)

Close and unlink the lockfile.

lockFile.check(path, [opts], cb)

Check if the lockfile is locked and not stale.

Callback is called with cb(error, isLocked).

lockFile.checkSync(path, [opts])

Check if the lockfile is locked and not stale.

Returns boolean.

Options

opts.wait

A number of milliseconds to wait for locks to expire before giving up. Only used by lockFile.lock. Poll for opts.wait ms. If the lock is not cleared by the time the wait expires, then it returns with the original error.

opts.pollPeriod

When using opts.wait, this is the period in ms in which it polls to check if the lock has expired. Defaults to 100.

opts.stale

A number of milliseconds before locks are considered to have expired.

opts.retries

Used by lock and lockSync. Retry n number of times before giving up.

opts.retryWait

Used by lock. Wait n milliseconds before retrying.

更新日志

Changes

1.0.4

  • test parallel
  • upgrade tap
  • upgrade node versions in travis.yml
  • Use signal-exit package to detect exit instead of process.on('exit')
  • added some debugging lines

v1.0.3

  • handle the case where callback is not passed by user

v1.0.2

  • git ignore coverage and node_modules
  • update tap to v7
  • build a changelog
  • package: fix repository link
  • pass tests on 0.8
  • before_script needs to be before_install
  • tap 1.2.0 and travis

v1.0.1

  • isc license
  • updated README.md

v1.0.0

  • Simulate staleness instead of waiting excessively
  • whitespace
  • manage 'retries' so it does not clash with 'wait' polling
  • manage 'wait' timer properly
  • Get rid of the excessive Object.create opts shadowing stuff
  • failing test for the time taken for retries + wait options
  • doc: add pollPeriod, correct opts.wait
  • Fixed #6: polling period should be configurable

v0.4.3

  • Implement race-resistant stale lock detection
  • set req id to 1 to start out

v0.4.2

  • stale option fix for windows file tunneling

v0.4.1

  • Fix version parsing

v0.4.0

  • Don't keep lockfiles open

v0.3.4

  • retry more aggressively

v0.3.3

  • Add debugging function

v0.3.2

  • remove console.error

v0.3.1

  • Support lack of subsecond fs precision
  • Fix error closure overwriting in notStale

v0.3.0

  • Use polling instead of watchers
  • Add more overhead buffer to contention test

v0.2.2

  • Fix wait calculation
  • fixup
  • Style: prefer early return to giant if/else
  • unlock: Close before unlinking
  • Don't get tripped up by locks named 'hasOwnProperty'
  • test: Pathological extreme lock contention
  • refactor license

0.2.1

  • Handle race conditions more thoroughly

0.2.0

  • Rename to 'lockfile'

0.0.2

  • Add retries
  • bsd

0.0.1

  • tests
  • package.json
  • the code
  • first