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();