Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): limit the amount of CPUs used by …
Browse files Browse the repository at this point in the history
…workers

See: #16860 (comment)
(cherry picked from commit 94c753c)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Feb 25, 2020
1 parent 9907542 commit 7f6703c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { tags } from '@angular-devkit/core';
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
import { existsSync } from 'fs';
import { cpus } from 'os';
import * as path from 'path';
import { RollupOptions } from 'rollup';
import { ScriptTarget } from 'typescript';
Expand All @@ -28,8 +27,8 @@ import {
debug,
} from 'webpack';
import { RawSource } from 'webpack-sources';
import { AssetPatternClass, ExtraEntryPoint } from '../../../browser/schema';
import { BuildBrowserFeatures } from '../../../utils';
import { AssetPatternClass } from '../../../browser/schema';
import { BuildBrowserFeatures, maxWorkers } from '../../../utils';
import { findCachePath } from '../../../utils/cache-path';
import {
allowMangle,
Expand Down Expand Up @@ -428,16 +427,10 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
mangle: allowMangle && buildOptions.platform !== 'server' && !differentialLoadingMode,
};

// Use up to 7 CPUs for Terser workers, but no more.
// Some environments, like CircleCI, report a large number of CPUs but trying to use them
// Will cause `Error: Call retries were exceeded` errors.
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
const maxCpus = Math.min(cpus().length, 7);

extraMinimizers.push(
new TerserPlugin({
sourceMap: scriptsSourceMap,
parallel: maxCpus,
parallel: maxWorkers,
cache: !cachingDisabled && findCachePath('terser-webpack'),
extractComments: false,
chunkFilter: (chunk: compilation.Chunk) =>
Expand All @@ -448,7 +441,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
// They are shared between ES2015 & ES5 outputs so must support ES5.
new TerserPlugin({
sourceMap: scriptsSourceMap,
parallel: maxCpus,
parallel: maxWorkers,
cache: !cachingDisabled && findCachePath('terser-webpack'),
extractComments: false,
chunkFilter: (chunk: compilation.Chunk) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as v8 from 'v8';
import { BundleActionCache } from './action-cache';
import { I18nOptions } from './i18n-options';
import { InlineOptions, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
import { maxWorkers } from './workers';

const hasThreadSupport = (() => {
try {
Expand Down Expand Up @@ -62,6 +63,7 @@ export class BundleActionExecutor {
return (this.largeWorker = new JestWorker(workerFile, {
exposedMethods: ['process', 'inlineLocales'],
setupArgs: [[...serialize(this.workerOptions)]],
numWorkers: maxWorkers,
}));
}

Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './normalize-source-maps';
export * from './normalize-optimization';
export * from './normalize-builder-schema';
export * from './url';
export * from './workers';
24 changes: 24 additions & 0 deletions packages/angular_devkit/build_angular/src/utils/workers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { cpus } from 'os';
/**
* Use CPU count -1 with limit to 7 for workers not to clog the system.
* Some environments, like CircleCI which use Docker report a number of CPUs by the host and not the count of available.
* This cause `Error: Call retries were exceeded` errors when trying to use them.
*
* See:
*
* https://github.com/nodejs/node/issues/28762
*
* https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
*
* https://github.com/angular/angular-cli/issues/16860#issuecomment-588828079
*
*/
export const maxWorkers = Math.max(Math.min(cpus().length, 8) - 1, 1);

0 comments on commit 7f6703c

Please sign in to comment.