( lock: SchedulerLock, dir?: string, )
| 62 | } |
| 63 | |
| 64 | async function tryCreateExclusive( |
| 65 | lock: SchedulerLock, |
| 66 | dir?: string, |
| 67 | ): Promise<boolean> { |
| 68 | const path = getLockPath(dir) |
| 69 | const body = jsonStringify(lock) |
| 70 | try { |
| 71 | await writeFile(path, body, { flag: 'wx' }) |
| 72 | return true |
| 73 | } catch (e: unknown) { |
| 74 | const code = getErrnoCode(e) |
| 75 | if (code === 'EEXIST') return false |
| 76 | if (code === 'ENOENT') { |
| 77 | // .claude/ doesn't exist yet — create it and retry once. In steady |
| 78 | // state the dir already exists (scheduled_tasks.json lives there), |
| 79 | // so this path is hit at most once. |
| 80 | await mkdir(dirname(path), { recursive: true }) |
| 81 | try { |
| 82 | await writeFile(path, body, { flag: 'wx' }) |
| 83 | return true |
| 84 | } catch (retryErr: unknown) { |
| 85 | if (getErrnoCode(retryErr) === 'EEXIST') return false |
| 86 | throw retryErr |
| 87 | } |
| 88 | } |
| 89 | throw e |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | function registerLockCleanup(opts?: SchedulerLockOptions): void { |
| 94 | unregisterCleanup?.() |
no test coverage detected