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.

Added AutoscaledPool.setDesiredConcurrency() function, improved docs

Jan Curn54ecf8e0b721
diff --git a/CHANGELOG.md b/CHANGELOG.md
index abf0d1465..20515455c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ xxx
- Added `loadedUrl` property to `Request` that contains the final URL of the loaded page after all redirects.
- Added memory overload warning log message.
- Added `keyValueStore.getPublicUrl` function.
+- Added `AutoscaledPool.setDesiredConcurrency()` function, improved docs
0.11.8 / 2019-02-05
===================
diff --git a/src/autoscaling/autoscaled_pool.js b/src/autoscaling/autoscaled_pool.js
index 620513cdb..b8c46c2ce 100644
--- a/src/autoscaling/autoscaled_pool.js
+++ b/src/autoscaling/autoscaled_pool.js
@@ -83,12 +83,12 @@ const DEFAULT_OPTIONS = {
* To abort a run, use the [`abort()`](#AutoscaledPool+abort) method.
*
* @param {Number} [options.minConcurrency=1]
- * Minimum number of tasks running in parallel.
+ * The minimum number of tasks running in parallel.
*
* *WARNING:* If you set this value too high with respect to the available system memory and CPU, your code might run extremely slow or crash.
* If you're not sure, just keep the default value and the concurrency will scale up automatically.
* @param {Number} [options.maxConcurrency=1000]
- * Maximum number of tasks running in parallel.
+ * The maximum number of tasks running in parallel.
* @param {Number} [options.desiredConcurrencyRatio=0.95]
* Minimum level of desired concurrency to reach before more scaling up is allowed.
* @param {Number} [options.scaleUpStepRatio=0.05]
@@ -114,6 +114,18 @@ const DEFAULT_OPTIONS = {
* Options to be passed down to the {@link SystemStatus} constructor. This is useful for fine-tuning
* the system status reports. If a custom snapshotter is set in the options, it will be used
* by the pool.
+ *
+ * @property {Number} minConcurrency
+ * The minimum number of tasks running in parallel.
+ * To change this property safely, please use the {@link setMinConcurrency} function.
+ * @property {Number} maxConcurrency
+ * The maximum number of tasks running in parallel.
+ * To change this property safely, please use the {@link setMaxConcurrency} function.
+ * @property {Number} desiredConcurrency
+ * The estimated number of parallel tasks that the system can currently support.
+ * To change this property safely, please use the {@link setDesiredConcurrency} function.
+ * @property {Number} currentConcurrency
+ * The number of tasks currently running in parallel.
*/
class AutoscaledPool {
constructor(options = {}) {
@@ -197,6 +209,18 @@ class AutoscaledPool {
this.minConcurrency = minConcurrency;
}
+ /**
+ * Sets current desired concurrency for the pool.
+ *
+ * @param {Number} maxConcurrency
+ */
+ setDesiredConcurrency(desiredConcurrency) {
+ if (!(this.minConcurrency <= desiredConcurrency && desiredConcurrency <= this.maxConcurrency)) {
+ throw new Error(`The desired concurrency must be an integer from ${this.minConcurrency} to ${this.maxConcurrency}`);
+ }
+ this.desiredConcurrency = desiredConcurrency;
+ }
+
/**
* Runs the auto-scaled pool. Returns a promise that gets resolved or rejected once
* all the tasks are finished or one of them fails.
diff --git a/test/autoscaling/autoscaled_pool.js b/test/autoscaling/autoscaled_pool.js
index ba3caf4aa..b8dd28a15 100644
--- a/test/autoscaling/autoscaled_pool.js
+++ b/test/autoscaling/autoscaled_pool.js
@@ -81,6 +81,15 @@ describe('AutoscaledPool', () => {
const startedAt = Date.now();
await pool.run();
+ // Test setDesiredConcurrency()
+ pool.setDesiredConcurrency(10);
+ expect(() => {
+ pool.setDesiredConcurrency(100);
+ }).to.throw(Error);
+ expect(() => {
+ pool.setDesiredConcurrency(-100);
+ }).to.throw(Error);
+
expect(result).to.be.eql(_.range(0, 100));
expect(Date.now() - startedAt).to.be.within(50, 200);
});