Skip to content

Commit

Permalink
feat: stopping the crawlers gracefully with BasicCrawler.stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
barjin committed Jan 6, 2025
1 parent f912b8b commit 284274b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/basic-crawler/src/internals/basic-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,18 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
return stats;
}

/**
* Gracefully stops the current run of the crawler.
*
* This method will wait for all running tasks to finish. Only once all tasks are finished, the method will resolve.
*
* **WARNING:** If this method is called (and awaited) from a task (e.g. in the `requestHandler`), it will wait indefinitely, as the task will never finish.
*/
async stop() {
await this.autoscaledPool?.pause(); // Gracefully starve the this.autoscaledPool, so it doesn't start new tasks. Resolves once the pool is cleared.
await this.autoscaledPool?.abort(); // Resolves the `autoscaledPool.run()` promise in the `BasicCrawler.run()` method. Since the pool is already paused, it resolves immediately and doesn't kill any tasks.
}

async getRequestQueue() {
if (!this.requestQueue && this.requestList) {
this.log.warningOnce(
Expand Down

0 comments on commit 284274b

Please sign in to comment.