-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathlock.ts
More file actions
78 lines (68 loc) · 2.53 KB
/
lock.ts
File metadata and controls
78 lines (68 loc) · 2.53 KB
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
import fs from 'node:fs/promises';
import path from 'node:path';
interface LockFile {
pid: number;
at: string;
}
const isPidLive = (pid: number): boolean => {
try {
process.kill(pid, 0);
return true;
} catch (err: any) {
// ESRCH = no such process (stale).
// EPERM = exists but we can't signal — treat as live (some other user owns it).
return err.code !== 'ESRCH';
}
};
const readIfPresent = async (lockPath: string): Promise<LockFile | null> => {
let raw: string;
try {
raw = await fs.readFile(lockPath, 'utf8');
} catch (err: any) {
if (err.code === 'ENOENT') return null;
return null;
}
let parsed: unknown;
try { parsed = JSON.parse(raw); } catch { return null; }
if (!parsed || typeof parsed !== 'object') return null;
const p = parsed as Record<string, unknown>;
if (typeof p.pid !== 'number' || typeof p.at !== 'string') return null;
return {pid: p.pid, at: p.at};
};
/**
* Atomic acquire via O_CREAT|O_EXCL. If the file already exists, the holder's
* PID is checked; when dead we reap it and retry once. Returns false on a live
* conflict — the caller is expected to surface "lock-held" to the admin.
*/
export const acquireLock = async (lockPath: string): Promise<boolean> => {
await fs.mkdir(path.dirname(lockPath), {recursive: true});
const payload = JSON.stringify({pid: process.pid, at: new Date().toISOString()});
const tryCreate = async (): Promise<boolean> => {
try {
const fh = await fs.open(lockPath, 'wx');
try { await fh.writeFile(payload); } finally { await fh.close(); }
return true;
} catch (err: any) {
if (err.code === 'EEXIST') return false;
throw err;
}
};
if (await tryCreate()) return true;
const existing = await readIfPresent(lockPath);
if (existing && isPidLive(existing.pid)) return false;
// Stale or unparseable — reap and retry once. A concurrent reaper may beat us,
// in which case the second tryCreate also returns false (correctly: someone
// else holds it now).
try { await fs.unlink(lockPath); }
catch (err: any) { if (err.code !== 'ENOENT') throw err; }
return tryCreate();
};
export const releaseLock = async (lockPath: string): Promise<void> => {
try { await fs.unlink(lockPath); }
catch (err: any) { if (err.code !== 'ENOENT') throw err; }
};
/** True iff the lock file exists *and* the recorded PID is live. Stale locks read as not-held. */
export const isHeld = async (lockPath: string): Promise<boolean> => {
const f = await readIfPresent(lockPath);
return !!f && isPidLive(f.pid);
};