(opts: {
lockName: LockName;
lockDir: string;
})
| 223 | } |
| 224 | |
| 225 | export function readProcessLock(opts: { |
| 226 | lockName: LockName; |
| 227 | lockDir: string; |
| 228 | }): ProcessLockMetadata | null { |
| 229 | const { lockName, lockDir } = opts; |
| 230 | const lockPath = lockFilePath(lockDir, lockName); |
| 231 | if (!existsSync(lockPath)) return null; |
| 232 | |
| 233 | let existing: ProcessLockMetadata; |
| 234 | try { |
| 235 | const parsed = JSON.parse(readFileSync(lockPath, 'utf-8')); |
| 236 | if (!parsed || typeof parsed !== 'object' || !isValidLockPid((parsed as { pid?: unknown }).pid)) |
| 237 | return null; |
| 238 | existing = parsed as ProcessLockMetadata; |
| 239 | } catch { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | if (existing.hostname !== hostname()) return null; |
| 244 | if (!isProcessAlive(existing.pid)) { |
| 245 | try { |
| 246 | unlinkSync(lockPath); |
| 247 | } catch {} |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | return existing; |
| 252 | } |
| 253 | |
| 254 | export type ReadProcessLockResult = |
| 255 | | { status: 'absent' } |
no test coverage detected