-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlock.ts
101 lines (84 loc) · 2.98 KB
/
lock.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Delay, Exception, promisify } from '@azure-tools/tasks';
import * as fs from 'fs';
import { isFile } from './file-io';
/* eslint-disable */ // special case
const { lock, check, } = require('proper-lockfile');
const openInternal: (path: string | Buffer, flags: string | number) => Promise<number> = promisify(fs.open);
export class UnableToReadLockException extends Exception {
constructor(path: string, public exitCode: number = 1) {
super(`Unable to create read lock on '${path}'.`, exitCode);
Object.setPrototypeOf(this, UnableToReadLockException.prototype);
}
}
export interface UnlockOptions {
realpath?: boolean;
delay?: number;
}
export interface CheckOptions extends UnlockOptions {
stale?: number;
}
export interface LockOptions extends CheckOptions {
update?: number;
retries?: number;
}
export type release = () => Promise<void>;
export type _release = () => void;
export class Lock {
private static _exclusive: (path: string, options?: LockOptions) => Promise<_release> = promisify(lock);
public static check: (path: string, options?: CheckOptions) => Promise<boolean> = promisify(check);
public static async exclusive(path: string, options?: LockOptions): Promise<release> {
return promisify(await this._exclusive(path, options));
}
public static async waitForExclusive(path: string, timeout: number = 5 * 60 * 1000): Promise<release | null> {
let result: release | null = null;
const expire = Date.now() + timeout;
do {
try {
result = await this.exclusive(path);
} catch (e) {
// no worries. Just wait a few seconds and see if we can get it.
await Delay(3000);
}
} while (result == null && expire > Date.now());
return result;
}
public static async read(path: string, options?: LockOptions): Promise<release> {
// first try to create the file
// it's ok if it fails
options = options || {};
options.delay = options.delay || 2000;
options.retries = options.retries || 4;
const p = `${path}.lock`;
try {
fs.writeFileSync(p, 'lockfile');
} catch (e) {
// no worries.
}
// try to open the file for read
try {
if (await isFile(p)) {
const fd = await openInternal(p, 'r');
return async () => {
fs.close(fd, (err) => { });
try {
fs.unlinkSync(p);
} catch {
// ignore errors
}
};
}
} catch {
// ignore errors
}
if (options.retries) {
await Delay(options.delay);
options.retries--;
return this.read(path, options);
}
throw new UnableToReadLockException(path);
}
}