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.

Fix/autoscale pool (#101)

Jan Čurn659fef971614
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c76ff584a..25f84d8b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,7 @@
xxx
===================
+- Added `loggingIntervalMillis` options to `AutoscaledPool`
+- Bugfix: `utils.isProduction` function was incorrect
- Added `RequestList.length()` function
0.5.32 / 2018-05-14
diff --git a/src/autoscaled_pool.js b/src/autoscaled_pool.js
index b8144537e..a436be25d 100644
--- a/src/autoscaled_pool.js
+++ b/src/autoscaled_pool.js
@@ -6,7 +6,7 @@ import { getMemoryInfo, isPromise, avg, isAtHome } from './utils';
import events from './events';
import { ACTOR_EVENT_NAMES } from './constants';
-const MEM_CHECK_INTERVAL_MILLIS = 200; // This is low to have at least.
+const AUTOSCALE_INTERVAL_MILLIS = 200; // This is low to have at least.
const MIN_FREE_MEMORY_RATIO = 0.1; // Minimum amount of memory that we keep free.
const DEFAULT_OPTIONS = {
maxConcurrency: 1000,
@@ -15,13 +15,13 @@ const DEFAULT_OPTIONS = {
maybeRunIntervalMillis: 500,
finishWhenEmpty: true,
ignoreMainProcess: false,
+ loggingIntervalMillis: 60000,
};
-// These constants defines that in Nth execution memCheckInterval we do:
+// These constants defines that in Nth execution of autoscaleInterval we do:
export const SCALE_UP_INTERVAL = 50;
export const SCALE_UP_MAX_STEP = 10;
export const SCALE_DOWN_INTERVAL = 5;
-export const LOG_INFO_INTERVAL = 6 * SCALE_UP_INTERVAL; // This must be multiple of SCALE_UP_INTERVAL
/**
* Helper function that coverts bytes into human readable MBs.
@@ -31,11 +31,11 @@ export const LOG_INFO_INTERVAL = 6 * SCALE_UP_INTERVAL; // This must be multiple
const humanReadable = bytes => `${Math.round(bytes / 1024 / 1024)} MB`;
/**
- * This class manages a pool of asynchronous resource-intensive tasks that are executed in parallel.
- * The pool only starts new tasks if there is enough free memory and CPU capacity.
+ * Manages a pool of asynchronous resource-intensive tasks that are executed in parallel.
+ * The pool only starts new tasks if there is enough free CPU and memory available.
* The information about the CPU and memory usage is obtained
* either from the local system or from the Apify cloud infrastructure in case the process
- * is running on the Apify Actor platform.
+ * is running on the Apify platform.
*
* The auto-scaled pool is started by calling the `run()` function
* and it finishes when the last running task gets resolved and the next call to
@@ -59,28 +59,38 @@ const humanReadable = bytes => `${Math.round(bytes / 1024 / 1024)} MB`;
* ```
*
* @param {Object} options
- * @param {Function} options.runTaskFunction A function that performs an asynchronous resource-intensive task.
- * The function must either return a
- * promise or `null` if no task is currently available.
- * @param {Function} [opts.isFinishedFunction] A function that is called every time there are no tasks being processed.
- * If it resolves to `true` then the pool's run finishes.
- * If `isFinishedFunction` is not provided then the pool
- * is finished whenever there are no running tasks.
- * @param {Function} [opts.isTaskReadyFunction] A function that indicates if `runTaskFunction` should be called.
- * By default, this function is called every time there is a free capacity for new task.
- * But by overriding this you can throttle
- * number of calls to `runTaskFunction` or to prevent calls to `runTaskFunction` when you know
- * that it would return null.
- * @param {Number} [options.maxConcurrency=1000] Maximum concurrency.
- * @param {Number} [options.minConcurrency=1] Minimum concurrency.
- * @param {Number} [options.maxMemoryMbytes] Maximum memory available in the system. By default the pool
- * uses the `totalMemory` value provided by `Apify.getMemoryInfo()`.
- * @param {Number} [options.minFreeMemoryRatio=0.2] Minimum ratio of free memory kept in the system.
- * @param {number} [options.maybeRunIntervalMillis=500] Indicates how often should the pool try to call
- * `opts.runTaskFunction` to start a new task.
- * @param {Boolean} [options.ignoreMainProcess=false] If set to `true` then autoscaling doesn't consider memory consumption
- * of the main NodeJS process when autoscaling the pool up/down. This is mainly useful when
- * tasks are running as separate processes (for example web browser).
+ * @param {Function} [options.runTaskFunction]
+ * A function that performs an asynchronous resource-intensive task.
+ * The function must either return a promise or `null` if no task is currently available.
+ * @param {Function} [options.isFinishedFunction]
+ * A function that is called every time there are no tasks being processed.
+ * If it resolves to `true` then the pool's run finishes.
+ * If `isFinishedFunction` is not provided then the pool
+ * is finished whenever there are no running tasks.
+ * @param {Function} [options.isTaskReadyFunction]
+ * A function that indicates if `runTaskFunction` should be called.
+ * By default, this function is called every time there is a free capacity for new task.
+ * But by overriding this you can throttle
+ * number of calls to `runTaskFunction` or to prevent calls to `runTaskFunction` when you know
+ * that it would return null.
+ * @param {Number} [options.minConcurrency=1]
+ * Minimum number of tasks running in parallel.
+ * @param {Number} [options.maxConcurrency=1000]
+ * Maximum number of tasks running in parallel.
+ * @param {Number} [options.maxMemoryMbytes]
+ * Maximum memory available in the system. By default the pool
+ * uses the `totalMemory` value provided by `Apify.getMemoryInfo()`.
+ * @param {Number} [options.minFreeMemoryRatio=0.2]
+ * Minimum ratio of free memory kept in the system.
+ * @param {Number} [options.maybeRunIntervalMillis=500]
+ * Indicates how often should the pool try to call `opts.runTaskFunction` to start a new task.
+ * @param {Boolean} [options.ignoreMainProcess=false]
+ * If set to `true` then the auto-scaling manager does not consider memory consumption
+ * of the main Node.js process when scaling the pool up or down.
+ * This is mainly useful when tasks are running as separate processes (e.g. web browsers).
+ * @param {Number} [options.loggingIntervalMillis=60000]
+ * Specifies a period in which the instance logs it state, in milliseconds.
+ * Set to `null` to disable periodic logging.
*/
export default class AutoscaledPool {
constructor(opts) {
@@ -114,6 +124,7 @@ export default class AutoscaledPool {
isFinishedFunction,
isTaskReadyFunction,
ignoreMainProcess,
+ loggingIntervalMillis,
} = _.defaults(opts, DEFAULT_OPTIONS);
checkParamOrThrow(maxConcurrency, 'opts.maxConcurrency', 'Number');
@@ -125,6 +136,7 @@ export default class AutoscaledPool {
checkParamOrThrow(isFinishedFunction, 'opts.isFinishedFunction', 'Maybe Function');
checkParamOrThrow(isTaskReadyFunction, 'opts.isTaskReadyFunction', 'Maybe Function');
checkParamOrThrow(ignoreMainProcess, 'opts.ignoreMainProcess', 'Boolean');
+ checkParamOrThrow(loggingIntervalMillis, 'opts.loggingIntervalMillis', 'Maybe Number');
// Configuration.
this.maxMemoryMbytes = maxMemoryMbytes;
@@ -136,9 +148,9 @@ export default class AutoscaledPool {
this.isFinishedFunction = isFinishedFunction;
this.isTaskReadyFunction = isTaskReadyFunction;
this.ignoreMainProcess = ignoreMainProcess;
+ this.loggingIntervalMillis = loggingIntervalMillis;
// State.
- this.promiseCounter = 0;
this.intervalCounter = 0;
this.concurrency = minConcurrency;
this.runningCount = 0;
@@ -146,9 +158,10 @@ export default class AutoscaledPool {
this.isCpuOverloadedSnapshots = [false];
this.queryingIsTaskReady = false;
this.queryingIsFinished = false;
+ this.lastLoggingTime = 0;
// Intervals.
- this.memCheckInterval = null;
+ this.autoscaleInterval = null;
this.maybeRunTaskInterval = null;
// This is resolve function of Promise returned by this.run()
@@ -189,11 +202,11 @@ export default class AutoscaledPool {
// This interval checks memory and in each call saves current memory stats and in every
// SCALE_UP_INTERVAL-th/SCALE_DOWN_INTERVAL-th call it may scale up/down based on memory.
if (isAtHome()) {
- this.memCheckInterval = setInterval(() => this._autoscale(), MEM_CHECK_INTERVAL_MILLIS);
+ this.autoscaleInterval = setInterval(() => this._autoscale(), AUTOSCALE_INTERVAL_MILLIS);
} else {
- log.warning('Autoscaling feature is currently available only when running on Apify platform!');
- log.warning('Use `minConcurrency` parameter if you need to test multiple requests in parallel.');
- log.warning('This feature will be enabled soon!');
+ log.warning('Auto-scaling is currently available only when running on Apify platform! '
+ + 'Use `minConcurrency` parameter if you need to test multiple requests in parallel. '
+ + 'This feature will be enabled soon.');
}
return this.poolPromise
@@ -208,7 +221,7 @@ export default class AutoscaledPool {
}
/**
- * Gets called every MEM_CHECK_INTERVAL_MILLIS and saves number of free bytes in this.freeBytesSnapshots.
+ * Gets called every AUTOSCALE_INTERVAL_MILLIS and saves number of free bytes in this.freeBytesSnapshots.
*
* Every:
* - SCALE_DOWN_INTERVAL-th call checks memory and possibly scales DOWN by 1.
@@ -234,8 +247,25 @@ export default class AutoscaledPool {
this.intervalCounter++;
this.freeBytesSnapshots = this.freeBytesSnapshots.concat(freeBytes).slice(-SCALE_UP_INTERVAL);
- const scaledDown = this._maybeScaleDown(totalBytes);
- if (!scaledDown) this._maybeScaleUp(totalBytes);
+ // On periodic intervals, print comprehensive log information
+ let logData = null;
+ if (this.loggingIntervalMillis > 0) {
+ const now = Date.now();
+ if (now > this.lastLoggingTime + this.loggingIntervalMillis) {
+ this.lastLoggingTime = now;
+ logData = {
+ concurrency: this.concurrency,
+ runningCount: this.runningCount,
+ };
+ }
+ }
+
+ const scaledDown = this._maybeScaleDown(totalBytes, logData);
+ if (!scaledDown) this._maybeScaleUp(totalBytes, logData);
+
+ if (logData) {
+ log.info('AutoscaledPool state', logData);
+ }
})
.catch(err => log.exception(err, 'AutoscaledPool._autoscale() function failed'));
}
@@ -246,15 +276,24 @@ export default class AutoscaledPool {
* @return true if concurrency was changed
* @ignore
*/
- _maybeScaleDown(totalBytes) {
- if (this.intervalCounter % SCALE_DOWN_INTERVAL !== 0 || this.concurrency <= this.minConcurrency) return false;
-
+ _maybeScaleDown(totalBytes, logData) {
const snapshots = this.freeBytesSnapshots.slice(-SCALE_DOWN_INTERVAL);
const averageFreeBytes = avg(snapshots);
const isMemoryOverloaded = averageFreeBytes / totalBytes < this.minFreeMemoryRatio;
const isCpuOverloaded = _.all(this.isCpuOverloadedSnapshots);
- if (!isCpuOverloaded && !isMemoryOverloaded) return false;
+ // Update logData even if not scaling down
+ if (logData) {
+ Object.assign(logData, {
+ concurrency: this.concurrency,
+ isMemoryOverloaded,
+ isCpuOverloaded,
+ });
+ }
+
+ if (this.intervalCounter % SCALE_DOWN_INTERVAL !== 0
+ || this.concurrency <= this.minConcurrency
+ || (!isCpuOverloaded && !isMemoryOverloaded)) return false;
this.concurrency--;
log.debug('AutoscaledPool: scaling down', {
@@ -273,12 +312,13 @@ export default class AutoscaledPool {
* @return true if concurrency was changed
* @ignore
*/
- _maybeScaleUp(totalBytes) {
- if (this.intervalCounter % SCALE_UP_INTERVAL !== 0 || this.concurrency >= this.maxConcurrency) return false;
-
- const spaceForInstances = this._computeSpaceForInstances(totalBytes, this.intervalCounter % LOG_INFO_INTERVAL);
+ _maybeScaleUp(totalBytes, logData) {
+ // Update logData even if not scaling down
+ const spaceForInstances = this._computeSpaceForInstances(totalBytes, logData);
- if (spaceForInstances <= 0) return false;
+ if (this.intervalCounter % SCALE_UP_INTERVAL !== 0
+ || this.concurrency >= this.maxConcurrency
+ || spaceForInstances <= 0) return false;
const increaseBy = Math.min(spaceForInstances, SCALE_UP_MAX_STEP);
const oldConcurrency = this.concurrency;
@@ -300,17 +340,15 @@ export default class AutoscaledPool {
*
* @ignore
*/
- _computeSpaceForInstances(totalBytes, logInfo) {
+ _computeSpaceForInstances(totalBytes, logData) {
const minFreeBytes = Math.min(...this.freeBytesSnapshots);
const minFreeRatio = minFreeBytes / totalBytes;
const maxTakenBytes = totalBytes - minFreeBytes;
const perInstanceRatio = (maxTakenBytes / totalBytes) / this.runningCount;
- const hasSpaceForInstances = (minFreeRatio - MIN_FREE_MEMORY_RATIO) / perInstanceRatio;
+ const hasSpaceForInstances = Math.floor((minFreeRatio - MIN_FREE_MEMORY_RATIO) / perInstanceRatio);
- if (logInfo) {
- log.info('AutoscaledPool: info', {
- concurency: this.concurrency,
- runningCount: this.runningCount,
+ if (logData) {
+ Object.assign(logData, {
freeBytesSnapshots: humanReadable(_.last(this.freeBytesSnapshots)),
totalBytes: humanReadable(totalBytes),
minFreeBytes: humanReadable(minFreeBytes),
@@ -321,7 +359,7 @@ export default class AutoscaledPool {
});
}
- return Math.floor(hasSpaceForInstances);
+ return hasSpaceForInstances;
}
/**
* If number of running task is lower than allowed concurrency and this.isTaskReadyFunction()
@@ -345,7 +383,7 @@ export default class AutoscaledPool {
// It's not null so it must be a promise!
if (!isPromise(isTaskReadyPromise)) throw new Error('User provided isTaskReadyFunction must return a Promise.');
- // We don't want to chain this so no return here!
+ // We don't want to chain this so don't return here!
isTaskReadyPromise
.catch((err) => {
this.queryingIsTaskReady = false;
@@ -378,7 +416,7 @@ export default class AutoscaledPool {
.catch((err) => {
log.exception(err, 'AutoscaledPool: runTaskFunction failed');
this.runningCount--;
- // If is here because we might already rejected this promise.
+ // This is here because we might have already rejected this promise.
if (this.reject) this.reject(err);
});
}
@@ -423,7 +461,7 @@ export default class AutoscaledPool {
events.removeListener(ACTOR_EVENT_NAMES.CPU_INFO, this.cpuInfoListener);
- clearInterval(this.memCheckInterval);
+ clearInterval(this.autoscaleInterval);
clearInterval(this.maybeRunTaskInterval);
}
}
diff --git a/src/basic_crawler.js b/src/basic_crawler.js
index 0714fa216..3f3d49d46 100644
--- a/src/basic_crawler.js
+++ b/src/basic_crawler.js
@@ -90,9 +90,9 @@ const DEFAULT_OPTIONS = {
* If it resolves to `true` then the crawler's run finishes.
* See `AutoscaledPool` for details.
* @param {Boolean} [options.ignoreMainProcess=false]
- * If set to `true` then autoscaling does not consider memory consumption
- * of the main Node.js process when autoscaling the pool up/down. This is mainly useful when
- * tasks are running as separate processes (e.g web browsers).
+ * If set to `true` then the auto-scaling manager does not consider memory consumption
+ * of the main Node.js process when scaling the pool up or down.
+ * This is mainly useful when tasks are running as separate processes (e.g. web browsers).
* See `AutoscaledPool` for details.
*/
export default class BasicCrawler {
@@ -178,9 +178,9 @@ export default class BasicCrawler {
this.requestList.markRequestHandled(request),
])
.then(results => results[0]);
- // If requestQueue.addRequest() fails here then we must reclaim it back to
- // the RequestList because probably it's not yet in the queue!
}, (err) => {
+ // If requestQueue.addRequest() fails here then we must reclaim it back to
+ // the RequestList because probably it's not yet in the queue!
log.exception(err, 'RequestQueue.addRequest() failed, reclaiming request back to queue', { request });
// Return null so that we finish immediately.
@@ -211,7 +211,7 @@ export default class BasicCrawler {
.then(() => source.markRequestHandled(request))
.catch((error) => {
if (request.ignoreErrors) {
- log.exception(error, 'BasicCrawler: handleRequestFunction failed, request.ignoreErrors=true so marking request handled', {
+ log.exception(error, 'BasicCrawler: handleRequestFunction failed, request.ignoreErrors=true so marking the request as handled', { // eslint-disable-line max-len
url: request.url,
retryCount: request.retryCount,
});
@@ -224,7 +224,7 @@ export default class BasicCrawler {
// Retry request.
if (request.retryCount < this.maxRequestRetries) {
request.retryCount++;
- log.exception(error, 'BasicCrawler: handleRequestFunction failed, reclaiming failed request back to list/queue', {
+ log.exception(error, 'BasicCrawler: handleRequestFunction failed, reclaiming failed request back to the list or queue', {
url: request.url,
retryCount: request.retryCount,
});
@@ -232,7 +232,7 @@ export default class BasicCrawler {
return source.reclaimRequest(request);
}
- log.exception(error, 'BasicCrawler: handleRequestFunction failed, marking failed request handled', {
+ log.exception(error, 'BasicCrawler: handleRequestFunction failed, marking failed request as handled', {
url: request.url,
retryCount: request.retryCount,
});
diff --git a/src/puppeteer_pool.js b/src/puppeteer_pool.js
index 10c182b83..c99a9cedc 100644
--- a/src/puppeteer_pool.js
+++ b/src/puppeteer_pool.js
@@ -217,9 +217,9 @@ export default class PuppeteerPool {
// Ensure that Chrome process will be really killed.
setTimeout(() => {
- // This if is here because users reported that it happened
+ // This is here because users reported that it happened
// that error `TypeError: Cannot read property 'kill' of null` was thrown.
- // Likely Chrome process wasn't started for some error ...
+ // Likely Chrome process wasn't started due to some error ...
if (childProcess) childProcess.kill('SIGKILL');
}, PROCESS_KILL_TIMEOUT_MILLIS);
diff --git a/src/utils.js b/src/utils.js
index c12390d04..4c42f9786 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -246,7 +246,7 @@ export const isPromise = (maybePromise) => {
*
* @ignore
*/
-export const isProduction = () => process.env.NODE_ENV !== 'production';
+export const isProduction = () => process.env.NODE_ENV === 'production';
/**
* Helper function used for local implementations. Creates dir.
diff --git a/test/autoscaled_pool.js b/test/autoscaled_pool.js
index 44f19762b..de0398698 100644
--- a/test/autoscaled_pool.js
+++ b/test/autoscaled_pool.js
@@ -6,7 +6,7 @@ import sinon from 'sinon';
import { delayPromise } from 'apify-shared/utilities';
import * as Apify from '../build/index';
import { ACTOR_EVENT_NAMES, ENV_VARS } from '../build/constants';
-import { LOG_INFO_INTERVAL, SCALE_UP_MAX_STEP, SCALE_DOWN_INTERVAL } from '../build/autoscaled_pool';
+import { SCALE_UP_MAX_STEP, SCALE_UP_INTERVAL, SCALE_DOWN_INTERVAL } from '../build/autoscaled_pool';
import * as utils from '../build/utils';
chai.use(chaiAsPromised);
@@ -104,7 +104,7 @@ describe('AutoscaledPool', () => {
toBytes(50),
toBytes(40),
];
- const hasSpaceForInstances = pool._computeSpaceForInstances(toBytes(100), true);
+ const hasSpaceForInstances = pool._computeSpaceForInstances(toBytes(100), null);
expect(hasSpaceForInstances).to.be.eql(5);
pool.concurrency = 20;
@@ -115,7 +115,7 @@ describe('AutoscaledPool', () => {
toBytes(5),
toBytes(5),
];
- const hasSpaceForInstances2 = pool._computeSpaceForInstances(toBytes(100), true);
+ const hasSpaceForInstances2 = pool._computeSpaceForInstances(toBytes(100), null);
expect(hasSpaceForInstances2).to.be.eql(-2);
});
@@ -133,7 +133,7 @@ describe('AutoscaledPool', () => {
// Should scale up.
pool.concurrency = 1;
pool.runningCount = 1;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
@@ -143,7 +143,7 @@ describe('AutoscaledPool', () => {
// Should not do anything when there is right amount of memory used.
pool.concurrency = 10;
pool.runningCount = 10;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(1), totalBytes: toBytes(10) }));
@@ -159,7 +159,7 @@ describe('AutoscaledPool', () => {
let promise = Promise.resolve();
for (let i = 0; i < 3; i++) {
promise = promise.then(() => {
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
return pool._autoscale();
});
}
@@ -172,7 +172,7 @@ describe('AutoscaledPool', () => {
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(0.9), totalBytes: toBytes(10) }));
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
await pool._autoscale();
expect(pool.concurrency).to.be.eql(9);
@@ -195,7 +195,7 @@ describe('AutoscaledPool', () => {
// Should not scale up.
pool.concurrency = 1;
pool.runningCount = 1;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({
@@ -209,7 +209,7 @@ describe('AutoscaledPool', () => {
// Should scale up.
pool.concurrency = 1;
pool.runningCount = 1;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({
@@ -224,6 +224,37 @@ describe('AutoscaledPool', () => {
mock.restore();
});
+ it('should work with loggingIntervalMillis = null', async () => {
+ const pool = new Apify.AutoscaledPool({
+ ignoreMainProcess: true,
+ minConcurrency: 1,
+ maxConcurrency: 100,
+ minFreeMemoryRatio: 0.1,
+ runTaskFunction: () => Promise.resolve(),
+ isFinishedFunction: () => Promise.resolve(false),
+ isTaskReadyFunction: () => Promise.resolve(true),
+ loggingIntervalMillis: null,
+ });
+ const mock = sinon.mock(utils);
+
+ // Should not scale up.
+ pool.concurrency = 1;
+ pool.runningCount = 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
+ mock.expects('getMemoryInfo')
+ .once()
+ .returns(Promise.resolve({
+ freeBytes: toBytes(5),
+ totalBytes: toBytes(10),
+ mainProcessBytes: 0,
+ }));
+ await pool._autoscale();
+ expect(pool.concurrency).to.be.eql(1);
+
+ mock.verify();
+ mock.restore();
+ });
+
it('should throw when some of the promises throws', async () => {
let counter = 0;
const runTaskFunction = () => {
@@ -284,7 +315,7 @@ describe('AutoscaledPool', () => {
}
// Should scale up.
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
pool.runningCount = pool.concurrency;
mock.expects('getMemoryInfo')
.once()
@@ -293,7 +324,7 @@ describe('AutoscaledPool', () => {
expect(pool.concurrency).to.be.eql(1 + SCALE_UP_MAX_STEP);
// Should scale up.
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
pool.runningCount = pool.concurrency;
mock.expects('getMemoryInfo')
.once()
@@ -307,7 +338,7 @@ describe('AutoscaledPool', () => {
// Should scale up but because of CPU overloaded event it always scales down.
pool.runningCount = pool.concurrency;
for (let i = 1; i <= 5; i++) {
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
@@ -320,7 +351,7 @@ describe('AutoscaledPool', () => {
// Should scale up again.
pool.runningCount = pool.concurrency;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
@@ -502,7 +533,7 @@ describe('AutoscaledPool', () => {
toBytes(50),
toBytes(40),
];
- const hasSpaceForInstances = pool._computeSpaceForInstances(toBytes(100), true);
+ const hasSpaceForInstances = pool._computeSpaceForInstances(toBytes(100), null);
expect(hasSpaceForInstances).to.be.eql(5);
pool.concurrency = 20;
@@ -513,7 +544,7 @@ describe('AutoscaledPool', () => {
toBytes(5),
toBytes(5),
];
- const hasSpaceForInstances2 = pool._computeSpaceForInstances(toBytes(100), true);
+ const hasSpaceForInstances2 = pool._computeSpaceForInstances(toBytes(100), null);
expect(hasSpaceForInstances2).to.be.eql(-2);
});
@@ -529,7 +560,7 @@ describe('AutoscaledPool', () => {
// Should scale up.
pool.concurrency = 1;
pool.runningCount = 1;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
@@ -539,7 +570,7 @@ describe('AutoscaledPool', () => {
// Should not do anything when there is right amount of memory used.
pool.concurrency = 10;
pool.runningCount = 10;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(1), totalBytes: toBytes(10) }));
@@ -555,7 +586,7 @@ describe('AutoscaledPool', () => {
let promise = Promise.resolve();
for (let i = 0; i < 3; i++) {
promise = promise.then(() => {
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
return pool._autoscale();
});
}
@@ -568,7 +599,7 @@ describe('AutoscaledPool', () => {
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(0.9), totalBytes: toBytes(10) }));
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
await pool._autoscale();
expect(pool.concurrency).to.be.eql(9);
@@ -616,7 +647,7 @@ describe('AutoscaledPool', () => {
}
// Should scale up.
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
pool.runningCount = pool.concurrency;
mock.expects('getMemoryInfo')
.once()
@@ -625,7 +656,7 @@ describe('AutoscaledPool', () => {
expect(pool.concurrency).to.be.eql(1 + SCALE_UP_MAX_STEP);
// Should scale up.
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
pool.runningCount = pool.concurrency;
mock.expects('getMemoryInfo')
.once()
@@ -639,7 +670,7 @@ describe('AutoscaledPool', () => {
// Should scale up but because of CPU overloaded event it always scales down.
pool.runningCount = pool.concurrency;
for (let i = 1; i <= 5; i++) {
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
@@ -652,7 +683,7 @@ describe('AutoscaledPool', () => {
// Should scale up again.
pool.runningCount = pool.concurrency;
- pool.intervalCounter = LOG_INFO_INTERVAL - 1;
+ pool.intervalCounter = SCALE_UP_INTERVAL - 1;
mock.expects('getMemoryInfo')
.once()
.returns(Promise.resolve({ freeBytes: toBytes(9.99), totalBytes: toBytes(10) }));
diff --git a/test/utils.js b/test/utils.js
index 970c3769f..a7f4caf28 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -53,6 +53,21 @@ describe('utils.addCharsetToContentType()', () => {
});
});
+describe('utils.isProduction()', () => {
+ it('works', () => {
+ const prev = process.env.NODE_ENV;
+ try {
+ process.env.NODE_ENV = 'production';
+ expect(utils.isProduction()).to.eql(true);
+
+ process.env.NODE_ENV = 'debug';
+ expect(utils.isProduction()).to.eql(false);
+ } finally {
+ process.env.NODE_ENV = prev;
+ }
+ });
+});
+
describe('utils.isDocker()', () => {
it('works for dockerenv && cgroup', () => {
sinon.stub(fs, 'stat').callsFake((path, callback) => callback(null));