MCPcopy Index your code
hub / github.com/liuup/claude-code-analysis / tryAcquireSchedulerLock

Function tryAcquireSchedulerLock

src/utils/cronTasksLock.ts:111–173  ·  view source on GitHub ↗
(
  opts?: SchedulerLockOptions,
)

Source from the content-addressed store, hash-verified

109 * If two sessions race to recover a stale lock, only one create succeeds.
110 */
111export 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)

Callers 1

enableFunction · 0.85

Calls 9

getSessionIdFunction · 0.85
logForDebuggingFunction · 0.85
jsonStringifyFunction · 0.85
unlinkFunction · 0.85
tryCreateExclusiveFunction · 0.70
registerLockCleanupFunction · 0.70
readLockFunction · 0.70
getLockPathFunction · 0.70
isProcessRunningFunction · 0.70

Tested by

no test coverage detected