-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: handle multiple BasicCrawler.stop() calls correctly
#3324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
dca1ffd
8496c2d
1195555
e9d152c
b55a8eb
4b5cd3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -561,6 +561,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| protected retryOnBlocked: boolean; | ||
| protected respectRobotsTxtFile: boolean | { userAgent?: string }; | ||
| protected onSkippedRequest?: SkippedRequestCallback; | ||
| protected stoppingPromise?: Promise<void>; | ||
| private _closeEvents?: boolean; | ||
| private shouldLogMaxProcessedRequestsExceeded = true; | ||
| private shouldLogMaxEnqueuedRequestsExceeded = true; | ||
|
|
@@ -978,6 +979,7 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| } | ||
|
|
||
| this.running = true; | ||
| this.stoppingPromise = undefined as any; | ||
barjin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.shouldLogMaxProcessedRequestsExceeded = true; | ||
| this.shouldLogMaxEnqueuedRequestsExceeded = true; | ||
|
|
||
|
|
@@ -1016,6 +1018,9 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| try { | ||
| await this.autoscaledPool!.run(); | ||
| } finally { | ||
| if (this?.stoppingPromise) { | ||
| await this.stoppingPromise; | ||
| } | ||
barjin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await this.teardown(); | ||
| await this.stats.stopCapturing(); | ||
|
|
||
|
|
@@ -1080,15 +1085,17 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext | |
| * To stop the crawler immediately, use {@apilink BasicCrawler.teardown|`crawler.teardown()`} instead. | ||
| */ | ||
| stop(message = 'The crawler has been gracefully stopped.'): void { | ||
| // Gracefully starve the this.autoscaledPool, so it doesn't start new tasks. Resolves once the pool is cleared. | ||
| this.autoscaledPool | ||
| ?.pause() | ||
| // 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. | ||
| .then(async () => this.autoscaledPool?.abort()) | ||
| .then(() => this.log.info(message)) | ||
| .catch((err) => { | ||
| this.log.error('An error occurred when stopping the crawler:', err); | ||
| }); | ||
| if (!this.stoppingPromise) { | ||
|
||
| // Gracefully starve the this.autoscaledPool, so it doesn't start new tasks. Resolves once the pool is cleared. | ||
| this.stoppingPromise = this.autoscaledPool | ||
| ?.pause() | ||
| // 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. | ||
| .then(async () => this.autoscaledPool?.abort()) | ||
| .then(() => this.log.info(message)) | ||
| .catch((err) => { | ||
| this.log.error('An error occurred when stopping the crawler:', err); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async getRequestQueue(): Promise<RequestProvider> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.