( options: TryAcquireFsLockSyncOptions, )
| 157 | } |
| 158 | |
| 159 | export function tryAcquireFsLockSync( |
| 160 | options: TryAcquireFsLockSyncOptions, |
| 161 | ): AcquiredFsLockSync | null { |
| 162 | const now = options.now ?? Date.now(); |
| 163 | const owner: FsLockOwner = { |
| 164 | token: randomUUID(), |
| 165 | pid: options.pid ?? process.pid, |
| 166 | purpose: options.purpose, |
| 167 | acquiredAtMs: now, |
| 168 | expiresAtMs: now + options.leaseMs, |
| 169 | }; |
| 170 | |
| 171 | try { |
| 172 | mkdirSync(dirname(options.lockDir), { recursive: true, mode: 0o700 }); |
| 173 | const guard = tryAcquireGuard(options.lockDir, options.purpose, options.leaseMs, now); |
| 174 | if (!guard) { |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | try { |
| 179 | for (let attempt = 0; attempt < 2; attempt += 1) { |
| 180 | try { |
| 181 | return createLock(options.lockDir, owner); |
| 182 | } catch (error) { |
| 183 | if ((error as NodeJS.ErrnoException).code !== 'EEXIST') { |
| 184 | return null; |
| 185 | } |
| 186 | if (!tryRecoverExpiredLockDir(options.lockDir, options.purpose, now, options.leaseMs)) { |
| 187 | return null; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } finally { |
| 192 | guard.release(); |
| 193 | } |
| 194 | } catch { |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | return null; |
| 199 | } |
no test coverage detected