Skip to content

Commit

Permalink
Abnormal lock file needs to be cleared
Browse files Browse the repository at this point in the history
For #117

If the download of chrome is interrupted unexpectedly, lock file will be retained forever.

We need to clean abnormal lock file
  • Loading branch information
ahuigo committed Dec 3, 2024
1 parent 4189e3e commit 4f849bf
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 4f849bf

Please sign in to comment.