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.

Clean up code

Ondra Urban8c93330e859f
diff --git a/src/autoscaled_pool.js b/src/autoscaled_pool.js
index 97e7f7f62..2f088c61d 100644
--- a/src/autoscaled_pool.js
+++ b/src/autoscaled_pool.js
@@ -221,10 +221,11 @@ export default class AutoscaledPool {
}
/**
- * Stops the auto-scaled pool, discarding all currently running tasks.
+ * Stops the auto-scaled pool, discards all currently running tasks and destroys it.
*/
stop() {
if (this.resolve) this.resolve();
+ // _destroy gets called 10 lines above so no need to do it here
}
/**
diff --git a/src/puppeteer_crawler.js b/src/puppeteer_crawler.js
index ce4337c8d..9241bfda5 100644
--- a/src/puppeteer_crawler.js
+++ b/src/puppeteer_crawler.js
@@ -207,10 +207,10 @@ export default class PuppeteerCrawler {
*/
run() {
if (this.isStopped) this.puppeteerPool = new PuppeteerPool(this.puppeteerPoolOptions);
+ this.isStopped = false;
this.isRunningPromise = new Promise((resolve, reject) => {
this.isRunningReject = reject;
});
- this.isStopped = false;
return this.basicCrawler.run()
.finally(() => this.puppeteerPool.destroy());
}
@@ -237,7 +237,7 @@ export default class PuppeteerCrawler {
const handlePagePromise = this.puppeteerPool
.newPage()
.then((newPage) => { page = newPage; })
- .then(() => this._gotoFunction({ page, request, puppeteerPool: this.puppeteerPool }))
+ .then(() => this.gotoFunction({ page, request, puppeteerPool: this.puppeteerPool }))
.then((response) => {
const promise = this.handlePageFunction({
page,
@@ -254,17 +254,12 @@ export default class PuppeteerCrawler {
const finalPromise = handlePagePromise
.timeout(this.pageOpsTimeoutMillis, 'PuppeteerCrawler: handlePageFunction timed out.')
.finally(() => {
- if (this.isStopped) return; // Pool will clean up itself.
+ if (this.isStopped) return; // Pool will be destroyed.
return Promise
.try(() => page.close())
.timeout(PAGE_CLOSE_TIMEOUT_MILLIS, 'Operation timed out.')
.catch(err => log.debug('PuppeteerCrawler: Page.close() failed.', { reason: err ? err.message : err }));
});
- return Promise.race([finalPromise, this.isRunningPromise]);
- }
-
- _gotoFunction(...args) {
- if (this.isStopped) return Promise.reject(new Error('PuppeteerCrawler is stopped.'));
- return this.gotoFunction(...args);
+ return Promise.race([finalPromise, this.isRunningPromise]); // Reject all requests immediately after stopping the crawler (to be reclaimed).
}
}
diff --git a/src/puppeteer_pool.js b/src/puppeteer_pool.js
index 6e9b1f155..97650cbdd 100644
--- a/src/puppeteer_pool.js
+++ b/src/puppeteer_pool.js
@@ -110,7 +110,6 @@ export default class PuppeteerPool {
checkParamOrThrow(launchPuppeteerOptions, 'opts.launchPuppeteerOptions', 'Maybe Object');
// Config.
- this.isDestroyed = false;
this.maxOpenPagesPerInstance = maxOpenPagesPerInstance;
this.retireInstanceAfterRequestCount = retireInstanceAfterRequestCount;
this.killInstanceAfterMillis = killInstanceAfterMillis;
@@ -291,7 +290,6 @@ export default class PuppeteerPool {
return page;
})
.catch((err) => {
- if (this.isDestroyed) return;
log.exception(err, 'PuppeteerPool: browser.newPage() failed', { id: instance.id });
this._retireInstance(instance);
@@ -304,7 +302,6 @@ export default class PuppeteerPool {
* Closes all the browsers.
*/
destroy() {
- this.isDestroyed = true;
clearInterval(this.instanceKillerInterval);
process.removeListener('SIGINT', this.sigintListener);
diff --git a/test/puppeteer_crawler.js b/test/puppeteer_crawler.js
index 57b5dc45f..e9be0011f 100644
--- a/test/puppeteer_crawler.js
+++ b/test/puppeteer_crawler.js
@@ -104,6 +104,7 @@ describe('PuppeteerCrawler', () => {
expect(request.userData.title).to.be.eql('Example Domain');
});
+ await Apify.utils.sleep(10); // Wait for event loop to unwind.
await puppeteerCrawler.run();
expect(processed).to.have.lengthOf(30);