From 2920de90b6d2fec43b8c00fb2aca15ff78628c2a Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 19 Apr 2024 12:19:26 +0100 Subject: [PATCH] Fix news cron locks and fetching to work more than once per day. --- crons/news.ts | 4 ++-- lib/data/news.ts | 8 +++++--- lib/interfaces/locker.ts | 12 ++++++++++-- lib/utils/misc.ts | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/crons/news.ts b/crons/news.ts index 9828b06..84f2fcc 100644 --- a/crons/news.ts +++ b/crons/news.ts @@ -10,9 +10,9 @@ export async function fetchNewArticles(forceFetch = false) { try { const feedsToCrawl = await db.query( - sql`SELECT * FROM "bewcloud_news_feeds" WHERE "last_crawled_at" IS NULL OR "last_crawled_at" <= $1`, + sql`SELECT * FROM "bewcloud_news_feeds" WHERE "last_crawled_at" IS NULL OR "last_crawled_at" <= $1 ORDER BY "last_crawled_at" ASC`, [ - fourHoursAgo.toISOString().substring(0, 10), + fourHoursAgo.toISOString(), ], ); diff --git a/lib/data/news.ts b/lib/data/news.ts index 3844f48..a1d5215 100644 --- a/lib/data/news.ts +++ b/lib/data/news.ts @@ -199,8 +199,8 @@ async function fetchNewsArticles(newsFeed: NewsFeed): Promise setTimeout(() => resolve(true), milliseconds)); } -const expirationInMilliseconds = 15 * 1000; +const expirationInMilliseconds = 15_000; const locks: Map = 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); } } diff --git a/lib/utils/misc.ts b/lib/utils/misc.ts index 3bd7561..51b47c0 100644 --- a/lib/utils/misc.ts +++ b/lib/utils/misc.ts @@ -121,7 +121,7 @@ export async function concurrentPromises( return results; } -const MAX_RESPONSE_TIME_IN_MS = 10 * 1000; +const MAX_RESPONSE_TIME_IN_MS = 10_000; export async function fetchUrl(url: string) { const abortController = new AbortController();