Skip to content

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

fix(queue-worker): improve stalled job cleaner (ENG-2907) (#1839)

Gergő Móricz5506dd981b22
diff --git a/apps/api/src/services/queue-worker.ts b/apps/api/src/services/queue-worker.ts
index 15045a86a..a52b3df09 100644
--- a/apps/api/src/services/queue-worker.ts
+++ b/apps/api/src/services/queue-worker.ts
@@ -1631,22 +1631,36 @@ app.listen(workerPort, () => {
(async () => {
async function failedListener(args: { jobId: string; failedReason: string; prev?: string | undefined; }) {
if (args.failedReason === "job stalled more than allowable limit") {
- let logger = _logger.child({ jobId: args.jobId, scrapeId: args.jobId, module: "queue-worker", method: "failedListener" });
+ const set = await redisEvictConnection.set("stalled-job-cleaner:" + args.jobId, "1", "EX", 60 * 60 * 24, "NX");
+ if (!set) {
+ return;
+ }
+
const job = await getScrapeQueue().getJob(args.jobId);
+ let logger = _logger.child({ jobId: args.jobId, scrapeId: args.jobId, module: "queue-worker", method: "failedListener", zeroDataRetention: job?.data.zeroDataRetention });
if (job && job.data.crawl_id) {
logger = logger.child({ crawlId: job.data.crawl_id });
logger.warn("Job stalled more than allowable limit");
const sc = (await getCrawl(job.data.crawl_id)) as StoredCrawl;
+
+ if (job.mode === "kickoff") {
+ await finishCrawlKickoff(job.data.crawl_id);
+ if (sc) {
+ await finishCrawlIfNeeded(job, sc);
+ }
+ } else {
+ const sc = (await getCrawl(job.data.crawl_id)) as StoredCrawl;
- logger.debug("Declaring job as done...");
- await addCrawlJobDone(job.data.crawl_id, job.id, false, logger);
- await redisEvictConnection.srem(
- "crawl:" + job.data.crawl_id + ":visited_unique",
- normalizeURL(job.data.url, sc),
- );
-
- await finishCrawlIfNeeded(job, sc);
+ logger.debug("Declaring job as done...");
+ await addCrawlJobDone(job.data.crawl_id, job.id, false, logger);
+ await redisEvictConnection.srem(
+ "crawl:" + job.data.crawl_id + ":visited_unique",
+ normalizeURL(job.data.url, sc),
+ );
+
+ await finishCrawlIfNeeded(job, sc);
+ }
} else {
logger.warn("Job stalled more than allowable limit");
}