From 4f849bf768e8dc7a499717f7cf36501218f7db9a Mon Sep 17 00:00:00 2001 From: Ahuigo <1781999+ahuigo@users.noreply.github.com> Date: Sat, 30 Nov 2024 00:12:35 +0800 Subject: [PATCH] Abnormal lock file needs to be cleared For https://github.com/lino-levan/astral/issues/117 If the download of chrome is interrupted unexpectedly, lock file will be retained forever. We need to clean abnormal lock file --- src/cache.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/cache.ts b/src/cache.ts index 784b8b0..285ad72 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -301,6 +301,30 @@ class Lock { constructor({ cache = getDefaultCachePath() } = {}) { this.path = resolve(cache, ".lock"); + this.checkExpiredLockPath(); + } + + /** Clean expired lock path */ + checkExpiredLockPath() { + // if this.path's create time is older than cacheTTL, remove it + try { + const fileInfo = Deno.statSync(this.path); + const lockTTL = 3600 * 1000; + if ( + fileInfo.birthtime && + Date.now() - fileInfo.birthtime.getTime() < lockTTL + ) { + Deno.removeSync(this.path); + console.log( + `%c There is an old lock file (${this.path}), this is probably due to a failed download. It has been removed automatically.`, + "color: #ff0000", + ); + } + } catch (error) { + if (!(error instanceof Deno.errors.NotFound)) { + throw error; + } + } } /** Returns true if lock file exists */