PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/proper-lockfile/README.md

https://github.com/mci/mpatlas
Markdown | 203 lines | 126 code | 77 blank | 0 comment | 0 complexity | ec56c294a80c42fefbb3e0da1b13c472 MD5 | raw file
  1. # proper-lockfile
  2. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]
  3. [npm-url]:https://npmjs.org/package/proper-lockfile
  4. [downloads-image]:http://img.shields.io/npm/dm/proper-lockfile.svg
  5. [npm-image]:http://img.shields.io/npm/v/proper-lockfile.svg
  6. [travis-url]:https://travis-ci.org/IndigoUnited/node-proper-lockfile
  7. [travis-image]:http://img.shields.io/travis/IndigoUnited/node-proper-lockfile/master.svg
  8. [coveralls-url]:https://coveralls.io/r/IndigoUnited/node-proper-lockfile
  9. [coveralls-image]:https://img.shields.io/coveralls/IndigoUnited/node-proper-lockfile/master.svg
  10. [david-dm-url]:https://david-dm.org/IndigoUnited/node-proper-lockfile
  11. [david-dm-image]:https://img.shields.io/david/IndigoUnited/node-proper-lockfile.svg
  12. [david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-proper-lockfile#info=devDependencies
  13. [david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-proper-lockfile.svg
  14. A inter-process and inter-machine lockfile utility that works on a local or network file system.
  15. ## Installation
  16. `$ npm install proper-lockfile`
  17. ## Design
  18. There are various ways to achieve [file locking](http://en.wikipedia.org/wiki/File_locking).
  19. This library utilizes the `mkdir` strategy which works atomically on any kind of file system, even network based ones.
  20. The lockfile path is based on the file path you are trying to lock by suffixing it with `.lock`.
  21. When a lock is successfully acquired, the lockfile's `mtime` (modified time) is periodically updated to prevent staleness. This allows to effectively check if a lock is stale by checking its `mtime` against a stale threshold. If the update of the mtime fails several times, the lock might be compromised. The `mtime` is [supported](http://en.wikipedia.org/wiki/Comparison_of_file_systems) in almost every `filesystem`.
  22. ### Comparison
  23. This library is similar to [lockfile](https://github.com/isaacs/lockfile) but the later has some drawbacks:
  24. - It relies on `open` with `O_EXCL` flag which has problems in network file systems. `proper-lockfile` uses `mkdir` which doesn't have this issue.
  25. > O_EXCL is broken on NFS file systems; programs which rely on it for performing locking tasks will contain a race condition.
  26. - The lockfile staleness check is done via `ctime` (creation time) which is unsuitable for long running processes. `proper-lockfile` constantly updates lockfiles `mtime` to do proper staleness check.
  27. - It does not check if the lockfile was compromised which can led to undesirable situations. `proper-lockfile` checks the lockfile when updating the `mtime`.
  28. ### Compromised
  29. `proper-lockfile` does not detect cases in which:
  30. - A `lockfile` is manually removed and someone else acquires the lock right after
  31. - Different `stale`/`update` values are being used for the same file, possibly causing two locks to be acquired on the same file
  32. `proper-lockfile` detects cases in which:
  33. - Updates to the `lockfile` fail
  34. - Updates take longer than expected, possibly causing the lock to became stale for a certain amount of time
  35. As you see, the first two are a consequence of bad usage. Technically, it was possible to detect the first two but it would introduce complexity and eventual race conditions.
  36. ## Usage
  37. ### .lock(file, [options], [compromised], callback)
  38. Tries to acquire a lock on `file`.
  39. If the lock succeeds, a `release` function is provided that should be called when you want to release the lock.
  40. If the lock gets compromised, the `compromised` function will be called. The default `compromised` function is a simple `throw err` which will probably cause the process to die. Specify it to handle the way you desire.
  41. Available options:
  42. - `stale`: Duration in milliseconds in which the lock is considered stale, defaults to `10000` (minimum value is `5000`)
  43. - `update`: The interval in milliseconds in which the lockfile's `mtime` will be updated, defaults to `stale/2` (minimum value is `1000`, maximum value is `stale/2`)
  44. - `retries`: The number of retries or a [retry](https://www.npmjs.org/package/retry) options object, defaults to `0`
  45. - `realpath`: Resolve symlinks using realpath, defaults to `true` (note that if `true`, the `file` must exist previously)
  46. - `fs`: A custom fs to use, defaults to `graceful-fs`
  47. ```js
  48. var lockfile = require('proper-lockfile');
  49. lockfile.lock('some/file', function (err, release) {
  50. if (err) throw err; // Lock failed
  51. // Do something while the file is locked
  52. // Call the provided release function when you're done
  53. release();
  54. // Note that you can optionally handle release errors
  55. // Though it's not mandatory since it will eventually stale
  56. /*release(function (err) {
  57. // At this point the lock was effectively released or an error
  58. // ocurred while removing it
  59. if (err) throw err;
  60. });*/
  61. });
  62. ```
  63. ### .unlock(file, [options], [callback])
  64. Releases a previously acquired lock on `file`.
  65. Whenever possible you should use the `release` function instead (as exemplified above). Still there are cases in which its hard to keep a reference to it around code. In those cases `unlock()` might be handy.
  66. The `callback` is optional because even if the removal of the lock failed, the lockfile's `mtime` will no longer be updated causing it to eventually stale.
  67. Available options:
  68. - `realpath`: Resolve symlinks using realpath, defaults to `true` (note that if `true`, the `file` must exist previously)
  69. - `fs`: A custom fs to use, defaults to `graceful-fs`
  70. ```js
  71. var lockfile = require('proper-lockfile');
  72. lockfile.lock('some/file', function (err) {
  73. if (err) throw err;
  74. // Later..
  75. lockfile.unlock('some/file');
  76. // or..
  77. /*lockfile.unlock('some/file', function (err) {
  78. // At this point the lock was effectively released or an error
  79. // ocurred while removing it
  80. if (err) throw err;
  81. });*/
  82. });
  83. ```
  84. ### .check(file, [options], callback)
  85. Check if the file is locked and its lockfile is not stale. Callback is called with callback(error, isLocked).
  86. Available options:
  87. - `stale`: Duration in milliseconds in which the lock is considered stale, defaults to `10000` (minimum value is `5000`)
  88. - `realpath`: Resolve symlinks using realpath, defaults to `true` (note that if `true`, the `file` must exist previously)
  89. - `fs`: A custom fs to use, defaults to `graceful-fs`
  90. ```js
  91. var lockfile = require('proper-lockfile');
  92. lockfile.check('some/file', function (err, isLocked) {
  93. if (err) throw err;
  94. // isLocked will be true if 'some/file' is locked, otherwise will be false if not locked
  95. });
  96. ```
  97. ### .lockSync(file, [options], [compromised])
  98. Sync version of `.lock()`.
  99. Returns the `release` function or throws on error.
  100. ### .unlockSync(file, [options])
  101. Sync version of `.unlock()`.
  102. Throws on error.
  103. ### .checkSync(file, [options])
  104. Sync version of `.check()`.
  105. Returns a boolean or throws on error.
  106. ## Graceful exit
  107. `proper-lockfile` automatically remove locks if the process exists. Though, `SIGINT` and `SIGTERM` signals
  108. are handled differently by `nodejs` in the sense that they do not fire a `exit` event on the `process`.
  109. To avoid this common issue that `CLI` developers have, please do the following:
  110. ```js
  111. // Map SIGINT & SIGTERM to process exit
  112. // so that lockfile removes the lockfile automatically
  113. process
  114. .once('SIGINT', function () {
  115. process.exit(1);
  116. })
  117. .once('SIGTERM', function () {
  118. process.exit(1);
  119. });
  120. ```
  121. ## Tests
  122. `$ npm test`
  123. `$ npm test-cov` to get coverage report
  124. The test suite is very extensive. There's even a stress test to guarantee exclusiveness of locks.
  125. ## License
  126. Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).