2019-08-29 15:47:54 +00:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/// <reference types="node" />
|
|
|
|
|
|
|
|
|
|
import * as cluster from 'cluster';
|
|
|
|
|
|
|
|
|
|
import {Logger} from '../../logging/logger';
|
|
|
|
|
import {PackageJsonUpdater} from '../../writing/package_json_updater';
|
|
|
|
|
import {AnalyzeEntryPointsFn, CreateCompileFn, Executor} from '../api';
|
2020-03-04 12:35:52 +00:00
|
|
|
import {AsyncLocker} from '../lock_file';
|
2019-08-29 15:47:54 +00:00
|
|
|
|
|
|
|
|
import {ClusterMaster} from './master';
|
|
|
|
|
import {ClusterWorker} from './worker';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An `Executor` that processes tasks in parallel (on multiple processes) and completes
|
|
|
|
|
* asynchronously.
|
|
|
|
|
*/
|
|
|
|
|
export class ClusterExecutor implements Executor {
|
|
|
|
|
constructor(
|
|
|
|
|
private workerCount: number, private logger: Logger,
|
2020-03-04 12:35:52 +00:00
|
|
|
private pkgJsonUpdater: PackageJsonUpdater, private lockFile: AsyncLocker) {}
|
2019-08-29 15:47:54 +00:00
|
|
|
|
|
|
|
|
async execute(analyzeEntryPoints: AnalyzeEntryPointsFn, createCompileFn: CreateCompileFn):
|
|
|
|
|
Promise<void> {
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
// This process is the cluster master.
|
2020-01-10 09:54:58 +00:00
|
|
|
return this.lockFile.lock(() => {
|
|
|
|
|
this.logger.debug(
|
|
|
|
|
`Running ngcc on ${this.constructor.name} (using ${this.workerCount} worker processes).`);
|
|
|
|
|
const master = new ClusterMaster(
|
|
|
|
|
this.workerCount, this.logger, this.pkgJsonUpdater, analyzeEntryPoints);
|
|
|
|
|
return master.run();
|
|
|
|
|
});
|
2019-08-29 15:47:54 +00:00
|
|
|
} else {
|
|
|
|
|
// This process is a cluster worker.
|
2019-12-05 19:02:57 +00:00
|
|
|
const worker = new ClusterWorker(this.logger, createCompileFn);
|
2019-08-29 15:47:54 +00:00
|
|
|
return worker.run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|