Skip to content

Commit 94c753c

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): limit the amount of CPUs used by workers
See: #16860 (comment)
1 parent 111e9cc commit 94c753c

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import { tags } from '@angular-devkit/core';
1313
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
1414
import { existsSync } from 'fs';
15-
import { cpus } from 'os';
1615
import * as path from 'path';
1716
import { RollupOptions } from 'rollup';
1817
import { ScriptTarget } from 'typescript';
@@ -28,8 +27,8 @@ import {
2827
debug,
2928
} from 'webpack';
3029
import { RawSource } from 'webpack-sources';
31-
import { AssetPatternClass, ExtraEntryPoint } from '../../../browser/schema';
32-
import { BuildBrowserFeatures } from '../../../utils';
30+
import { AssetPatternClass } from '../../../browser/schema';
31+
import { BuildBrowserFeatures, maxWorkers } from '../../../utils';
3332
import { findCachePath } from '../../../utils/cache-path';
3433
import {
3534
allowMangle,
@@ -428,16 +427,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
428427
mangle: allowMangle && buildOptions.platform !== 'server' && !differentialLoadingMode,
429428
};
430429

431-
// Use up to 7 CPUs for Terser workers, but no more.
432-
// Some environments, like CircleCI, report a large number of CPUs but trying to use them
433-
// Will cause `Error: Call retries were exceeded` errors.
434-
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
435-
const maxCpus = Math.min(cpus().length, 7);
436-
437430
extraMinimizers.push(
438431
new TerserPlugin({
439432
sourceMap: scriptsSourceMap,
440-
parallel: maxCpus,
433+
parallel: maxWorkers,
441434
cache: !cachingDisabled && findCachePath('terser-webpack'),
442435
extractComments: false,
443436
chunkFilter: (chunk: compilation.Chunk) =>
@@ -448,7 +441,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
448441
// They are shared between ES2015 & ES5 outputs so must support ES5.
449442
new TerserPlugin({
450443
sourceMap: scriptsSourceMap,
451-
parallel: maxCpus,
444+
parallel: maxWorkers,
452445
cache: !cachingDisabled && findCachePath('terser-webpack'),
453446
extractComments: false,
454447
chunkFilter: (chunk: compilation.Chunk) =>

packages/angular_devkit/build_angular/src/utils/action-executor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as v8 from 'v8';
1212
import { BundleActionCache } from './action-cache';
1313
import { I18nOptions } from './i18n-options';
1414
import { InlineOptions, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
15+
import { maxWorkers } from './workers';
1516

1617
const hasThreadSupport = (() => {
1718
try {
@@ -62,6 +63,7 @@ export class BundleActionExecutor {
6263
return (this.largeWorker = new JestWorker(workerFile, {
6364
exposedMethods: ['process', 'inlineLocales'],
6465
setupArgs: [[...serialize(this.workerOptions)]],
66+
numWorkers: maxWorkers,
6567
}));
6668
}
6769

packages/angular_devkit/build_angular/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export * from './normalize-source-maps';
1616
export * from './normalize-optimization';
1717
export * from './normalize-builder-schema';
1818
export * from './url';
19+
export * from './workers';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { cpus } from 'os';
10+
/**
11+
* Use CPU count -1 with limit to 7 for workers not to clog the system.
12+
* Some environments, like CircleCI which use Docker report a number of CPUs by the host and not the count of available.
13+
* This cause `Error: Call retries were exceeded` errors when trying to use them.
14+
*
15+
* See:
16+
*
17+
* https://github.com/nodejs/node/issues/28762
18+
*
19+
* https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
20+
*
21+
* https://github.com/angular/angular-cli/issues/16860#issuecomment-588828079
22+
*
23+
*/
24+
export const maxWorkers = Math.max(Math.min(cpus().length, 8) - 1, 1);

0 commit comments

Comments
 (0)