(lockDir: string, heartbeatPath: string, metaPath: string, staleMs: number)
| 125 | } |
| 126 | |
| 127 | async function stale(lockDir: string, heartbeatPath: string, metaPath: string, staleMs: number) { |
| 128 | // Stale detection allows automatic recovery after crashed owners. |
| 129 | const now = wall() |
| 130 | const heartbeat = await stats(heartbeatPath) |
| 131 | if (heartbeat) { |
| 132 | return now - heartbeat.mtimeMs > staleMs |
| 133 | } |
| 134 | |
| 135 | const meta = await stats(metaPath) |
| 136 | if (meta) { |
| 137 | return now - meta.mtimeMs > staleMs |
| 138 | } |
| 139 | |
| 140 | const dir = await stats(lockDir) |
| 141 | if (!dir) { |
| 142 | return false |
| 143 | } |
| 144 | |
| 145 | return now - dir.mtimeMs > staleMs |
| 146 | } |
| 147 | |
| 148 | async function tryAcquireLockDir(lockDir: string, opts: Opts): Promise<Owned | { acquired: false }> { |
| 149 | const token = randomUUID?.() ?? randomBytes(16).toString("hex") |
no test coverage detected