* Check if a lock is currently held * @param lockName - Name of the lock * @returns Promise - Whether the lock is currently held
(lockName: string)
| 270 | * @returns Promise<boolean> - Whether the lock is currently held |
| 271 | */ |
| 272 | async isLocked(lockName: string): Promise<boolean> { |
| 273 | try { |
| 274 | const cacheManager = await getCacheManager(); |
| 275 | const lockKey = `${this.options.prefix}:${lockName}`; |
| 276 | const existingLock = await cacheManager.get(lockKey); |
| 277 | |
| 278 | if (!existingLock) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | const parsedLock = JSON.parse(String(existingLock)); |
| 283 | const lockAge = Date.now() - parsedLock.acquiredAt; |
| 284 | |
| 285 | // Check if lock has expired |
| 286 | if (lockAge > this.options.timeout) { |
| 287 | // Clean up expired lock |
| 288 | await cacheManager.delete(lockKey); |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | return true; |
| 293 | } catch (error) { |
| 294 | logger.error( |
| 295 | `[DistributedLock] Error checking lock status ${lockName}:`, |
| 296 | error |
| 297 | ); |
| 298 | return false; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get information about a lock |