Fix news cron locks and fetching to work more than once per day.

This commit is contained in:
Bruno Bernardino
2024-04-19 12:19:26 +01:00
parent a8076f6420
commit 2920de90b6
4 changed files with 18 additions and 8 deletions

View File

@@ -4,7 +4,7 @@ function wait(milliseconds = 100) {
return new Promise((resolve) => setTimeout(() => resolve(true), milliseconds));
}
const expirationInMilliseconds = 15 * 1000;
const expirationInMilliseconds = 15_000;
const locks: Map<string, { expiresAt: Date }> = new Map();
@@ -16,13 +16,20 @@ export default class Locker {
}
public async acquire() {
const currentLock = locks.get(this.lockName);
console.debug('Acquiring lock:', this.lockName);
let currentLock = locks.get(this.lockName);
while (currentLock) {
// Only wait if the lock hasn't expired
if (currentLock.expiresAt > new Date()) {
console.debug('Waiting for lock to expire:', this.lockName);
await wait();
} else {
// Release lock since it has expired
this.release();
}
currentLock = locks.get(this.lockName);
}
locks.set(this.lockName, {
@@ -31,6 +38,7 @@ export default class Locker {
}
public release() {
console.debug('Releasing lock:', this.lockName);
locks.delete(this.lockName);
}
}