forked from moxystudio/node-proper-lockfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
380 lines (302 loc) · 10.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
'use strict';
const fs = require('graceful-fs');
const path = require('path');
const retry = require('retry');
const syncFs = require('./lib/syncFs');
const locks = {};
function getLockFile(file) {
return `${file}.lock`;
}
function canonicalPath(file, options, callback) {
if (!options.realpath) {
return callback(null, path.resolve(file));
}
// Use realpath to resolve symlinks
// It also resolves relative paths
options.fs.realpath(file, callback);
}
function acquireLock(file, options, callback) {
// Use mkdir to create the lockfile (atomic operation)
options.fs.mkdir(getLockFile(file), (err) => {
// If successful, we are done
if (!err) {
return callback();
}
// If error is not EEXIST then some other error occurred while locking
if (err.code !== 'EEXIST') {
return callback(err);
}
// Otherwise, check if lock is stale by analyzing the file mtime
if (options.stale <= 0) {
return callback(Object.assign(new Error('Lock file is already being hold'), { code: 'ELOCKED', file }));
}
options.fs.stat(getLockFile(file), (err, stat) => {
if (err) {
// Retry if the lockfile has been removed (meanwhile)
// Skip stale check to avoid recursiveness
if (err.code === 'ENOENT') {
return acquireLock(file, Object.assign({}, options, { stale: 0 }), callback);
}
return callback(err);
}
if (!isLockStale(stat, options)) {
return callback(Object.assign(new Error('Lock file is already being hold'), { code: 'ELOCKED', file }));
}
// If it's stale, remove it and try again!
// Skip stale check to avoid recursiveness
removeLock(file, options, (err) => {
if (err) {
return callback(err);
}
acquireLock(file, Object.assign({}, options, { stale: 0 }), callback);
});
});
});
}
function isLockStale(stat, options) {
return stat.mtime.getTime() < Date.now() - options.stale;
}
function removeLock(file, options, callback) {
// Remove lockfile, ignoring ENOENT errors
options.fs.rmdir(getLockFile(file), (err) => {
if (err && err.code !== 'ENOENT') {
return callback(err);
}
callback();
});
}
function updateLock(file, options) {
const lock = locks[file];
/* istanbul ignore next */
if (lock.updateTimeout) {
return;
}
lock.updateDelay = lock.updateDelay || options.update;
lock.updateTimeout = setTimeout(() => {
const mtime = Date.now() / 1000;
lock.updateTimeout = null;
options.fs.utimes(getLockFile(file), mtime, mtime, (err) => {
// Ignore if the lock was released
if (lock.released) {
return;
}
// Verify if we are within the stale threshold
if (lock.lastUpdate <= Date.now() - options.stale &&
lock.lastUpdate > Date.now() - options.stale * 2) {
return compromisedLock(file, lock, Object.assign(new Error(lock.updateError || 'Unable to update lock within the stale \
threshold'), { code: 'ECOMPROMISED' }));
}
// If the file is older than (stale * 2), we assume the clock is moved manually,
// which we consider a valid case
// If it failed to update the lockfile, keep trying unless
// the lockfile was deleted!
if (err) {
if (err.code === 'ENOENT') {
return compromisedLock(file, lock, Object.assign(err, { code: 'ECOMPROMISED' }));
}
lock.updateError = err;
lock.updateDelay = 1000;
return updateLock(file, options);
}
// All ok, keep updating..
lock.lastUpdate = Date.now();
lock.updateError = null;
lock.updateDelay = null;
updateLock(file, options);
});
}, lock.updateDelay);
// Unref the timer so that the nodejs process can exit freely
// This is safe because all acquired locks will be automatically released
// on process exit
// We first check that `lock.updateTimeout.unref` exists because some users
// may be using this module outside of NodeJS (e.g., in an electron app),
// and in those cases `setTimeout` return an integer.
if (lock.updateTimeout.unref) {
lock.updateTimeout.unref();
}
}
function compromisedLock(file, lock, err) {
lock.released = true; // Signal the lock has been released
/* istanbul ignore next */
lock.updateTimeout && clearTimeout(lock.updateTimeout); // Cancel lock mtime update
if (locks[file] === lock) {
delete locks[file];
}
lock.compromised(err);
}
// -----------------------------------------
function lock(file, options, compromised, callback) {
if (typeof options === 'function') {
callback = compromised;
compromised = options;
options = null;
}
if (!callback) {
callback = compromised;
compromised = null;
}
options = Object.assign({
stale: 10000,
update: null,
realpath: true,
retries: 0,
fs,
}, options);
options.retries = options.retries || 0;
options.retries = typeof options.retries === 'number' ? { retries: options.retries } : options.retries;
options.stale = Math.max(options.stale || 0, 2000);
options.update = options.update == null ? options.stale / 2 : options.update || 0;
options.update = Math.max(Math.min(options.update, options.stale / 2), 1000);
compromised = compromised || function (err) { throw err; };
// Resolve to a canonical file path
canonicalPath(file, options, (err, file) => {
if (err) {
return callback(err);
}
// Attempt to acquire the lock
const operation = retry.operation(options.retries);
operation.attempt(() => {
acquireLock(file, options, (err) => {
if (operation.retry(err)) {
return;
}
if (err) {
return callback(operation.mainError());
}
// We now own the lock
const lock = locks[file] = {
options,
compromised,
lastUpdate: Date.now(),
};
// We must keep the lock fresh to avoid staleness
updateLock(file, options);
callback(null, (releasedCallback) => {
if (lock.released) {
return releasedCallback &&
releasedCallback(Object.assign(new Error('Lock is already released'), { code: 'ERELEASED' }));
}
// Not necessary to use realpath twice when unlocking
unlock(file, Object.assign({}, options, { realpath: false }), releasedCallback);
});
});
});
});
}
function unlock(file, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = Object.assign({
fs,
realpath: true,
}, options);
callback = callback || function () {};
// Resolve to a canonical file path
canonicalPath(file, options, (err, file) => {
if (err) {
return callback(err);
}
// Skip if the lock is not acquired
const lock = locks[file];
if (!lock) {
return callback(Object.assign(new Error('Lock is not acquired/owned by you'), { code: 'ENOTACQUIRED' }));
}
lock.updateTimeout && clearTimeout(lock.updateTimeout); // Cancel lock mtime update
lock.released = true; // Signal the lock has been released
delete locks[file]; // Delete from locks
removeLock(file, options, callback);
});
}
function lockSync(file, options, compromised) {
if (typeof options === 'function') {
compromised = options;
options = null;
}
options = options || {};
options.fs = syncFs(options.fs || fs);
options.retries = options.retries || 0;
options.retries = typeof options.retries === 'number' ? { retries: options.retries } : options.retries;
// Retries are not allowed because it requires the flow to be sync
if (options.retries.retries) {
throw Object.assign(new Error('Cannot use retries with the sync api'), { code: 'ESYNC' });
}
let err;
let release;
lock(file, options, compromised, (_err, _release) => {
err = _err;
release = _release;
});
if (err) {
throw err;
}
return release;
}
function unlockSync(file, options) {
options = options || {};
options.fs = syncFs(options.fs || fs);
let err;
unlock(file, options, (_err) => {
err = _err;
});
if (err) {
throw err;
}
}
function check(file, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = Object.assign({
stale: 10000,
realpath: true,
fs,
}, options);
options.stale = Math.max(options.stale || 0, 2000);
// Resolve to a canonical file path
canonicalPath(file, options, (err, file) => {
if (err) {
return callback(err);
}
// Check if lockfile exists
options.fs.stat(getLockFile(file), (err, stat) => {
if (err) {
// if does not exist, file is not locked. Otherwise, callback with error
return (err.code === 'ENOENT') ? callback(null, false) : callback(err);
}
if (options.stale <= 0) { return callback(null, true); }
// Otherwise, check if lock is stale by analyzing the file mtime
return callback(null, !isLockStale(stat, options));
});
});
}
function checkSync(file, options) {
options = options || {};
options.fs = syncFs(options.fs || fs);
let err;
let locked;
check(file, options, (_err, _locked) => {
err = _err;
locked = _locked;
});
if (err) {
throw err;
}
return locked;
}
// Remove acquired locks on exit
/* istanbul ignore next */
process.on('exit', () => {
Object.keys(locks).forEach((file) => {
try { locks[file].options.fs.rmdirSync(getLockFile(file)); } catch (e) { /* empty */ }
});
});
module.exports = lock;
module.exports.lock = lock;
module.exports.unlock = unlock;
module.exports.lockSync = lockSync;
module.exports.unlockSync = unlockSync;
module.exports.check = check;
module.exports.checkSync = checkSync;