Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: RQ request count is consistent after migration (#2116)

Jindřich Bär9ab8c1874f52
diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.ts
index 6e0e27979..6bac7cbb9 100644
--- a/packages/basic-crawler/src/internals/basic-crawler.ts
+++ b/packages/basic-crawler/src/internals/basic-crawler.ts
@@ -783,7 +783,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
// eslint-disable-next-line max-len
message = `Experiencing problems, ${this.stats.state.requestsFailed - previousState.requestsFailed || this.stats.state.requestsFailed} failed requests in the past ${this.statusMessageLoggingInterval} seconds.`;
} else {
- const total = this.requestQueue?.assumedTotalCount || this.requestList?.length();
+ const total = this.requestQueue?.getTotalCount() || this.requestList?.length();
message = `Crawled ${this.stats.state.requestsFinished}${total ? `/${total}` : ''} pages, ${this.stats.state.requestsFailed} failed requests.`;
}
diff --git a/packages/core/src/storages/request_provider.ts b/packages/core/src/storages/request_provider.ts
index 209131898..b53202c4d 100644
--- a/packages/core/src/storages/request_provider.ts
+++ b/packages/core/src/storages/request_provider.ts
@@ -41,6 +41,8 @@ export abstract class RequestProvider implements IStorage {
assumedTotalCount = 0;
assumedHandledCount = 0;
+ private initialCount = 0;
+
protected queueHeadIds = new ListDictionary<string>();
protected requestCache: LruCache<RequestLruItem>;
/** @internal */
@@ -77,6 +79,15 @@ export abstract class RequestProvider implements IStorage {
return this.inProgress.size;
}
+ /**
+ * Returns an offline approximation of the total number of requests in the queue (i.e. pending + handled).
+ *
+ * Survives restarts and actor migrations.
+ */
+ getTotalCount() {
+ return this.assumedTotalCount + this.initialCount;
+ }
+
/**
* Adds a request to the queue.
*
@@ -631,6 +642,9 @@ export abstract class RequestProvider implements IStorage {
const queue = await manager.openStorage(queueIdOrName, options.storageClient);
queue.proxyConfiguration = options.proxyConfiguration;
+ // eslint-disable-next-line dot-notation
+ queue['initialCount'] = (await queue.client.get())?.totalRequestCount ?? 0;
+
return queue;
}
}