Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api/nuq): cap crawl backlog timeout at 48h instead of Infinity (#3450)

firecrawl-spring[bot]790de9571761
diff --git a/apps/api/src/lib/concurrency-limit.ts b/apps/api/src/lib/concurrency-limit.ts
index 3eb64510a..d1f997457 100644
--- a/apps/api/src/lib/concurrency-limit.ts
+++ b/apps/api/src/lib/concurrency-limit.ts
@@ -21,6 +21,13 @@ export function getTeamQueueLimit(concurrencyLimit: number): number {
return Math.min(Math.max(concurrencyLimit * 2000, 50_000), 2_000_000);
}
+// Upper bound for how long a job may sit in the concurrency-limit backlog.
+// This bounds both the Redis ZSET score and the Postgres `times_out_at`
+// column on `nuq.queue_scrape_backlog`, so the reaper can always evict
+// stale rows. A backlogged crawl job that outlives this window is
+// unrecoverable anyway — its StoredCrawl in Redis (24h TTL) is gone.
+export const MAX_BACKLOG_TIMEOUT_MS = 172800000; // 48h
+
const constructKey = (team_id: string) => "concurrency-limiter:" + team_id;
const constructQueueKey = (team_id: string) =>
"concurrency-limit-queue:" + team_id;
@@ -121,8 +128,8 @@ export async function pushConcurrencyLimitedJobs(
for (const { job, timeout } of jobs) {
const cappedTimeout = Number.isFinite(timeout)
- ? Math.min(timeout, 172800000)
- : 172800000; // cap at 48h, fallback for NaN/Infinity
+ ? Math.min(timeout, MAX_BACKLOG_TIMEOUT_MS)
+ : MAX_BACKLOG_TIMEOUT_MS;
pipeline.set(
constructJobKey(job.id),
JSON.stringify(job),
diff --git a/apps/api/src/lib/concurrency-queue-reconciler.ts b/apps/api/src/lib/concurrency-queue-reconciler.ts
index b6bb041ba..2bae7007b 100644
--- a/apps/api/src/lib/concurrency-queue-reconciler.ts
+++ b/apps/api/src/lib/concurrency-queue-reconciler.ts
@@ -7,6 +7,7 @@ import { RateLimiterMode, type ScrapeJobData } from "../types";
import {
getConcurrencyLimitActiveJobs,
getNextConcurrentJob,
+ MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
pushConcurrencyLimitedJob,
pushCrawlConcurrencyLimitActiveJob,
@@ -33,7 +34,7 @@ function isExtractJob(data: ScrapeJobData): boolean {
}
function getBacklogJobTimeout(jobData: ScrapeJobData): number {
- if (jobData.crawl_id) return Infinity;
+ if (jobData.crawl_id) return MAX_BACKLOG_TIMEOUT_MS;
if ("scrapeOptions" in jobData && jobData.scrapeOptions?.timeout)
return jobData.scrapeOptions.timeout;
@@ -243,7 +244,7 @@ async function drainQueue(
priority: nextJob.job.priority,
listenable: nextJob.job.listenable,
},
- nextJob.timeout === Infinity ? 172800000 : nextJob.timeout,
+ nextJob.timeout === Infinity ? MAX_BACKLOG_TIMEOUT_MS : nextJob.timeout,
);
typeBlocked++;
continue;
diff --git a/apps/api/src/services/queue-jobs.ts b/apps/api/src/services/queue-jobs.ts
index cfb20152d..3e0cf1fcb 100644
--- a/apps/api/src/services/queue-jobs.ts
+++ b/apps/api/src/services/queue-jobs.ts
@@ -6,6 +6,7 @@ import {
getConcurrencyQueueJobsCount,
getCrawlConcurrencyLimitActiveJobs,
getTeamQueueLimit,
+ MAX_BACKLOG_TIMEOUT_MS,
pushConcurrencyLimitActiveJob,
pushConcurrencyLimitedJob,
pushConcurrencyLimitedJobs,
@@ -59,12 +60,12 @@ async function _addScrapeJobToConcurrencyQueue(
ownerId: webScraperOptions.team_id ?? undefined,
groupId: webScraperOptions.crawl_id ?? undefined,
backlogged: true,
- backloggedTimesOutAt: webScraperOptions.crawl_id
- ? undefined
- : new Date(
- Date.now() +
- (webScraperOptions.scrapeOptions?.timeout ?? 60 * 1000),
- ),
+ backloggedTimesOutAt: new Date(
+ Date.now() +
+ (webScraperOptions.crawl_id
+ ? MAX_BACKLOG_TIMEOUT_MS
+ : (webScraperOptions.scrapeOptions?.timeout ?? 60 * 1000)),
+ ),
},
);
@@ -77,7 +78,7 @@ async function _addScrapeJobToConcurrencyQueue(
listenable,
},
webScraperOptions.crawl_id
- ? Infinity
+ ? MAX_BACKLOG_TIMEOUT_MS
: (webScraperOptions.scrapeOptions?.timeout ?? 60 * 1000),
);
}
@@ -100,11 +101,12 @@ async function _addScrapeJobsToConcurrencyQueue(
ownerId: job.data.team_id ?? undefined,
groupId: job.data.crawl_id ?? undefined,
backlogged: true,
- backloggedTimesOutAt: job.data.crawl_id
- ? undefined
- : new Date(
- Date.now() + (job.data.scrapeOptions?.timeout ?? 60 * 1000),
- ),
+ backloggedTimesOutAt: new Date(
+ Date.now() +
+ (job.data.crawl_id
+ ? MAX_BACKLOG_TIMEOUT_MS
+ : (job.data.scrapeOptions?.timeout ?? 60 * 1000)),
+ ),
},
})),
);
@@ -130,7 +132,7 @@ async function _addScrapeJobsToConcurrencyQueue(
listenable: job.listenable ?? false,
},
timeout: job.data.crawl_id
- ? Infinity
+ ? MAX_BACKLOG_TIMEOUT_MS
: (job.data.scrapeOptions?.timeout ?? 60 * 1000),
});
}