Skip to content

Commit

Permalink
fix: RQ request count is consistent after migration (#2116)
Browse files Browse the repository at this point in the history
Until now, the `assumedTotalCount` got reset on a migration (it was a
purely local counter). This causes #1855, which is not too bad, but
manifests at a very prominent place in the console.

I'm not *way too confident* about the async iife in the constructor, but
I think it should work just fine, as the new `totalCount` property is
purely approximative (and is marked as such it the comment).
Closes #1855
  • Loading branch information
barjin authored Oct 5, 2023
1 parent 6fb1c55 commit 9ab8c18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/basic-crawler/src/internals/basic-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/storages/request_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 9ab8c18

Please sign in to comment.