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.

Renamed abortInstanceAfterRequestCount to retireInstanceAfterRequestC… (#64)

Jan Čurnd1b8eda926e8
diff --git a/README.md b/README.md
index de88e3ed1..d159bc8e7 100644
--- a/README.md
+++ b/README.md
@@ -5,9 +5,10 @@
[![Build Status](https://travis-ci.org/apifytech/apify-js.svg)](https://travis-ci.org/apifytech/apify-js)
<div id="include-readme-1">
- The `apify` NPM package enables development of web scrapers, crawlers and web automation projects
+ The <code>apify</code> NPM package enables development of web scrapers, crawlers and web automation projects
either locally or running on <a href="https://www.apify.com/docs/actor" target="_blank">Apify Actor</a> -
a serverless computing platform that enables execution of arbitrary code in the cloud.
+
The package provides helper functions to launch web browsers with proxies, access the storage etc. Note that the usage of the package is optional, you can create acts at Apify platform without it.
For more information about the Apify Actor platform, please see https://www.apify.com/docs/actor
diff --git a/src/puppeteer_crawler.js b/src/puppeteer_crawler.js
index 2b762f07c..27fca7412 100644
--- a/src/puppeteer_crawler.js
+++ b/src/puppeteer_crawler.js
@@ -75,14 +75,15 @@ const DEFAULT_OPTIONS = {
* See `isFinishedFunction` parameter of `AutoscaledPool`.
* @param {Number} [options.maxOpenPagesPerInstance=100] Maximum number of opened tabs per browser. If this limit is reached then a new
* browser instance is started. See `maxOpenPagesPerInstance` parameter of `PuppeteerPool`.
- * @param {Number} [options.abortInstanceAfterRequestCount=150] Maximum number of requests that can be processed by a single browser instance.
- * After this limit is reached the browser is restarted.
- * See `abortInstanceAfterRequestCount` parameter of `PuppeteerPool`.
+ * @param {Number} [options.retireInstanceAfterRequestCount=150] Maximum number of requests that can be processed by a single browser instance.
+ * After the limit is reached the browser will be retired and new requests will
+ * be handled by a new browser instance.
+ * See `retireInstanceAfterRequestCount` parameter of `PuppeteerPool`.
* @param {Function} [options.launchPuppeteerFunction] Overrides how new Puppeteer instance gets launched. See `launchPuppeteerFunction` parameter of
* `PuppeteerPool`.
* @param {Number} [options.instanceKillerIntervalMillis=60000] How often the launched Puppeteer instances are checked whether they can be
* closed. See `instanceKillerIntervalMillis` parameter of `PuppeteerPool`.
- * @param {Number} [options.killInstanceAfterMillis=300000] If Puppeteer instance reaches the `options.abortInstanceAfterRequestCount` limit then
+ * @param {Number} [options.killInstanceAfterMillis=300000] If Puppeteer instance reaches the `options.retireInstanceAfterRequestCount` limit then
* it is considered retired and no more tabs will be opened. After the last tab is closed
* the whole browser is closed too. This parameter defines a time limit for inactivity
* after which the browser is closed even if there are pending tabs. See
@@ -94,6 +95,11 @@ const DEFAULT_OPTIONS = {
*/
export default class PuppeteerCrawler {
constructor(opts) {
+ // For backwards compatibility, in the future we can remove this...
+ if (!opts.retireInstanceAfterRequestCount && opts.abortInstanceAfterRequestCount) {
+ opts.retireInstanceAfterRequestCount = opts.abortInstanceAfterRequestCount;
+ }
+
const {
handlePageFunction,
gotoFunction,
@@ -114,7 +120,7 @@ export default class PuppeteerCrawler {
// Puppeteer Pool options
maxOpenPagesPerInstance,
- abortInstanceAfterRequestCount,
+ retireInstanceAfterRequestCount,
launchPuppeteerFunction,
instanceKillerIntervalMillis,
killInstanceAfterMillis,
@@ -137,7 +143,7 @@ export default class PuppeteerCrawler {
this.puppeteerPool = new PuppeteerPool({
maxOpenPagesPerInstance,
- abortInstanceAfterRequestCount,
+ retireInstanceAfterRequestCount,
launchPuppeteerFunction,
instanceKillerIntervalMillis,
killInstanceAfterMillis,
diff --git a/src/puppeteer_pool.js b/src/puppeteer_pool.js
index a1ebf508a..f76106a67 100644
--- a/src/puppeteer_pool.js
+++ b/src/puppeteer_pool.js
@@ -16,8 +16,10 @@ const DEFAULT_PUPPETEER_CONFIG = {
const PROCESS_KILL_TIMEOUT_MILLIS = 5000;
const DEFAULT_OPTIONS = {
- maxOpenPagesPerInstance: 100,
- abortInstanceAfterRequestCount: 150,
+ // Don't make these too large, otherwise Puppeteer might start crashing weirdly,
+ // and the default settings should just work
+ maxOpenPagesPerInstance: 50,
+ retireInstanceAfterRequestCount: 100,
// These can't be constants because we need it for unit tests.
instanceKillerIntervalMillis: 60 * 1000,
@@ -31,7 +33,8 @@ const DEFAULT_OPTIONS = {
const config = Object.assign({}, DEFAULT_PUPPETEER_CONFIG, puppeteerConfig);
- // TODO: is this needed at all? It might be confusing because the feature has the same name
+ // TODO: this should be in Apify.launchPuppeteer(). But is this needed at all?
+ // It might be confusing because the feature has the same name
// as Chrome command line flag, so people will assume it's doing just that.
// For simplicity I'd just remove it...
if (config.disableWebSecurity) {
@@ -87,13 +90,13 @@ class PuppeteerInstance {
*
* @param {Number} [options.maxOpenPagesPerInstance=100] Maximum number of open tabs per browser. If this limit is reached then a new
* browser will be started.
- * @param {Number} [options.abortInstanceAfterRequestCount=150] Maximum number of requests processed by a single browser.
- * After the limit is reach the browser
- * will be restarted.
+ * @param {Number} [options.retireInstanceAfterRequestCount=150] Maximum number of requests that can be processed by a single browser instance.
+ * After the limit is reached the browser will be retired and new requests will
+ * be handled by a new browser instance.
* @param {Function} [options.launchPuppeteerFunction] Overrides the default function to launch a new Puppeteer instance.
* @param {Number} [options.instanceKillerIntervalMillis=60000] How often opened Puppeteer instances are checked wheter they can be
* closed.
- * @param {Number} [options.killInstanceAfterMillis=300000] If Puppeteer instance reaches the `options.abortInstanceAfterRequestCount` limit then
+ * @param {Number} [options.killInstanceAfterMillis=300000] If Puppeteer instance reaches the `options.retireInstanceAfterRequestCount` limit then
* it is considered retired and no more tabs will be opened. After the last tab is closed the
* whole browser is closed too. This parameter defines a time limit for inactivity after
* which the browser is closed even if there are pending open tabs.
@@ -106,23 +109,28 @@ export default class PuppeteerPool {
constructor(opts = {}) {
checkParamOrThrow(opts, 'opts', 'Object');
+ // For backwards compatibility, in the future we can remove this...
+ if (!opts.retireInstanceAfterRequestCount && opts.abortInstanceAfterRequestCount) {
+ opts.retireInstanceAfterRequestCount = opts.abortInstanceAfterRequestCount;
+ }
+
const {
maxOpenPagesPerInstance,
- abortInstanceAfterRequestCount,
+ retireInstanceAfterRequestCount,
launchPuppeteerFunction,
instanceKillerIntervalMillis,
killInstanceAfterMillis,
} = _.defaults(opts, DEFAULT_OPTIONS);
checkParamOrThrow(maxOpenPagesPerInstance, 'opts.maxOpenPagesPerInstance', 'Number');
- checkParamOrThrow(abortInstanceAfterRequestCount, 'opts.abortInstanceAfterRequestCount', 'Number');
+ checkParamOrThrow(retireInstanceAfterRequestCount, 'opts.retireInstanceAfterRequestCount', 'Number');
checkParamOrThrow(launchPuppeteerFunction, 'opts.launchPuppeteerFunction', 'Function');
checkParamOrThrow(instanceKillerIntervalMillis, 'opts.instanceKillerIntervalMillis', 'Number');
checkParamOrThrow(killInstanceAfterMillis, 'opts.killInstanceAfterMillis', 'Number');
// Config.
this.maxOpenPagesPerInstance = maxOpenPagesPerInstance;
- this.abortInstanceAfterRequestCount = abortInstanceAfterRequestCount;
+ this.retireInstanceAfterRequestCount = retireInstanceAfterRequestCount;
this.killInstanceAfterMillis = killInstanceAfterMillis;
this.launchPuppeteerFunction = () => launchPuppeteerFunction(opts);
@@ -266,7 +274,7 @@ export default class PuppeteerPool {
instance.totalPages++;
instance.activePages++;
- if (instance.totalPages >= this.abortInstanceAfterRequestCount) this._retireInstance(instance);
+ if (instance.totalPages >= this.retireInstanceAfterRequestCount) this._retireInstance(instance);
return instance.browserPromise
.then(browser => browser.newPage())