( opts?: SchedulerLockOptions, )
| 109 | * If two sessions race to recover a stale lock, only one create succeeds. |
| 110 | */ |
| 111 | export async function tryAcquireSchedulerLock( |
| 112 | opts?: SchedulerLockOptions, |
| 113 | ): Promise<boolean> { |
| 114 | const dir = opts?.dir |
| 115 | // "sessionId" in the lock file is really just a stable owner key. REPL |
| 116 | // uses getSessionId(); daemon callers supply their own UUID. PID remains |
| 117 | // the liveness signal regardless. |
| 118 | const sessionId = opts?.lockIdentity ?? getSessionId() |
| 119 | const lock: SchedulerLock = { |
| 120 | sessionId, |
| 121 | pid: process.pid, |
| 122 | acquiredAt: Date.now(), |
| 123 | } |
| 124 | |
| 125 | if (await tryCreateExclusive(lock, dir)) { |
| 126 | lastBlockedBy = undefined |
| 127 | registerLockCleanup(opts) |
| 128 | logForDebugging( |
| 129 | `[ScheduledTasks] acquired scheduler lock (PID ${process.pid})`, |
| 130 | ) |
| 131 | return true |
| 132 | } |
| 133 | |
| 134 | const existing = await readLock(dir) |
| 135 | |
| 136 | // Already ours (idempotent). After --resume the session ID is restored |
| 137 | // but the process has a new PID — update the lock file so other sessions |
| 138 | // see a live PID and don't steal it. |
| 139 | if (existing?.sessionId === sessionId) { |
| 140 | if (existing.pid !== process.pid) { |
| 141 | await writeFile(getLockPath(dir), jsonStringify(lock)) |
| 142 | registerLockCleanup(opts) |
| 143 | } |
| 144 | return true |
| 145 | } |
| 146 | |
| 147 | // Corrupt or unparseable — treat as stale. |
| 148 | // Another live session — blocked. |
| 149 | if (existing && isProcessRunning(existing.pid)) { |
| 150 | if (lastBlockedBy !== existing.sessionId) { |
| 151 | lastBlockedBy = existing.sessionId |
| 152 | logForDebugging( |
| 153 | `[ScheduledTasks] scheduler lock held by session ${existing.sessionId} (PID ${existing.pid})`, |
| 154 | ) |
| 155 | } |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | // Stale — unlink and retry the exclusive create once. |
| 160 | if (existing) { |
| 161 | logForDebugging( |
| 162 | `[ScheduledTasks] recovering stale scheduler lock from PID ${existing.pid}`, |
| 163 | ) |
| 164 | } |
| 165 | await unlink(getLockPath(dir)).catch(() => {}) |
| 166 | if (await tryCreateExclusive(lock, dir)) { |
| 167 | lastBlockedBy = undefined |
| 168 | registerLockCleanup(opts) |
no test coverage detected