• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# lockfile
2
3A very polite lock file utility, which endeavors to not litter, and to
4wait patiently for others.
5
6## Usage
7
8```javascript
9var lockFile = require('lockfile')
10
11// opts is optional, and defaults to {}
12lockFile.lock('some-file.lock', opts, function (er) {
13  // if the er happens, then it failed to acquire a lock.
14  // if there was not an error, then the file was created,
15  // and won't be deleted until we unlock it.
16
17  // do my stuff, free of interruptions
18  // then, some time later, do:
19  lockFile.unlock('some-file.lock', function (er) {
20    // er means that an error happened, and is probably bad.
21  })
22})
23```
24
25## Methods
26
27Sync methods return the value/throw the error, others don't.  Standard
28node fs stuff.
29
30All known locks are removed when the process exits.  Of course, it's
31possible for certain types of failures to cause this to fail, but a best
32effort is made to not be a litterbug.
33
34### lockFile.lock(path, [opts], cb)
35
36Acquire a file lock on the specified path
37
38### lockFile.lockSync(path, [opts])
39
40Acquire a file lock on the specified path
41
42### lockFile.unlock(path, cb)
43
44Close and unlink the lockfile.
45
46### lockFile.unlockSync(path)
47
48Close and unlink the lockfile.
49
50### lockFile.check(path, [opts], cb)
51
52Check if the lockfile is locked and not stale.
53
54Callback is called with `cb(error, isLocked)`.
55
56### lockFile.checkSync(path, [opts])
57
58Check if the lockfile is locked and not stale.
59
60Returns boolean.
61
62## Options
63
64### opts.wait
65
66A number of milliseconds to wait for locks to expire before giving up.
67Only used by lockFile.lock.  Poll for `opts.wait` ms.  If the lock is
68not cleared by the time the wait expires, then it returns with the
69original error.
70
71### opts.pollPeriod
72
73When using `opts.wait`, this is the period in ms in which it polls to
74check if the lock has expired.  Defaults to `100`.
75
76### opts.stale
77
78A number of milliseconds before locks are considered to have expired.
79
80### opts.retries
81
82Used by lock and lockSync.  Retry `n` number of times before giving up.
83
84### opts.retryWait
85
86Used by lock.  Wait `n` milliseconds before retrying.
87