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.

Initial commit

Ondra Urban2aeb81c0aea4
diff --git a/src/autoscaling/autoscaled_pool.js b/src/autoscaling/autoscaled_pool.js
index 22bb3156b..6ae330432 100644
--- a/src/autoscaling/autoscaled_pool.js
+++ b/src/autoscaling/autoscaled_pool.js
@@ -265,7 +265,12 @@ class AutoscaledPool {
try {
isTaskReady = await this.isTaskReadyFunction();
} catch (err) {
- log.exception(err, 'AutoscaledPool: isTaskReadyFunction failed');
+ // We might have already rejected this promise.
+ if (this.reject) {
+ // No need to log all concurrent errors.
+ log.exception(err, 'AutoscaledPool: isTaskReadyFunction failed');
+ this.reject(err);
+ }
} finally {
this.queryingIsTaskReady = false;
}
@@ -293,9 +298,9 @@ class AutoscaledPool {
} catch (err) {
// We might have already rejected this promise.
if (this.reject) {
- this.reject(err);
// No need to log all concurrent errors.
- log.exception(err, 'AutoscaledPool: runTaskFunction failed');
+ log.exception(err, 'AutoscaledPool: runTaskFunction failed.');
+ this.reject(err);
}
}
}
@@ -396,7 +401,11 @@ class AutoscaledPool {
const isFinished = await this.isFinishedFunction();
if (isFinished && this.resolve) this.resolve();
} catch (err) {
- log.exception(err, 'AutoscaledPool: isFinishedFunction failed');
+ if (this.reject) {
+ // No need to log all concurrent errors.
+ log.exception(err, 'AutoscaledPool: isFinishedFunction failed.');
+ this.reject(err);
+ }
} finally {
this.queryingIsFinished = false;
}
diff --git a/test/autoscaling/autoscaled_pool.js b/test/autoscaling/autoscaled_pool.js
index cb5c42cd2..c7dc6088f 100644
--- a/test/autoscaling/autoscaled_pool.js
+++ b/test/autoscaling/autoscaled_pool.js
@@ -246,6 +246,37 @@ describe('AutoscaledPool', () => {
await expect(pool.run()).to.be.rejectedWith('some-runtask-error');
});
+
+ it('when isFinishedFunction throws', async () => {
+ let count = 0;
+ const pool = new AutoscaledPool({
+ minConcurrency: 10,
+ maxConcurrency: 10,
+ runTaskFunction: async () => { count++; },
+ isFinishedFunction: async () => { throw new Error('some-finished-error'); },
+ isTaskReadyFunction: async () => count < 1,
+ });
+
+
+ await expect(pool.run()).to.be.rejectedWith('some-finished-error');
+ });
+
+ it('when isTaskReadyFunction throws', async () => {
+ let count = 0;
+ const pool = new AutoscaledPool({
+ minConcurrency: 10,
+ maxConcurrency: 10,
+ runTaskFunction: async () => { count++; },
+ isFinishedFunction: async () => false,
+ isTaskReadyFunction: async () => {
+ if (count > 1) throw new Error('some-ready-error');
+ else return true;
+ },
+ });
+
+
+ await expect(pool.run()).to.be.rejectedWith('some-ready-error');
+ });
});