refactor(ngcc): simplify cluster PackageJsonUpdater (#36637)
PR Close #36637
This commit is contained in:

committed by
Matias Niemelä

parent
443f5eee85
commit
bb944eecd6
@ -18,26 +18,26 @@ import {sendMessageToMaster} from './utils';
|
||||
|
||||
|
||||
/**
|
||||
* A `PackageJsonUpdater` that can safely handle update operations on multiple processes.
|
||||
* A `PackageJsonUpdater` for cluster workers that will send update changes to the master process so
|
||||
* that it can safely handle update operations on multiple processes.
|
||||
*/
|
||||
export class ClusterPackageJsonUpdater implements PackageJsonUpdater {
|
||||
constructor(private delegate: PackageJsonUpdater) {}
|
||||
export class ClusterWorkerPackageJsonUpdater implements PackageJsonUpdater {
|
||||
constructor() {
|
||||
if (cluster.isMaster) {
|
||||
throw new Error('Tried to create cluster worker PackageJsonUpdater on the master process.');
|
||||
}
|
||||
}
|
||||
|
||||
createUpdate(): PackageJsonUpdate {
|
||||
return new PackageJsonUpdate((...args) => this.writeChanges(...args));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the changes in-memory (if necessary) and send a message to the master process.
|
||||
*/
|
||||
writeChanges(
|
||||
changes: PackageJsonChange[], packageJsonPath: AbsoluteFsPath,
|
||||
preExistingParsedJson?: JsonObject): void {
|
||||
if (cluster.isMaster) {
|
||||
// This is the master process:
|
||||
// Actually apply the changes to the file on disk.
|
||||
return this.delegate.writeChanges(changes, packageJsonPath, preExistingParsedJson);
|
||||
}
|
||||
|
||||
// This is a worker process:
|
||||
// Apply the changes in-memory (if necessary) and send a message to the master process.
|
||||
if (preExistingParsedJson) {
|
||||
for (const [propPath, value] of changes) {
|
||||
if (propPath.length === 0) {
|
||||
|
@ -15,13 +15,12 @@ import {parseCommandLineOptions} from '../../command_line_options';
|
||||
import {ConsoleLogger} from '../../logging/console_logger';
|
||||
import {Logger, LogLevel} from '../../logging/logger';
|
||||
import {getPathMappingsFromTsConfig} from '../../utils';
|
||||
import {DirectPackageJsonUpdater} from '../../writing/package_json_updater';
|
||||
import {CreateCompileFn} from '../api';
|
||||
import {getCreateCompileFn} from '../create_compile_function';
|
||||
import {stringifyTask} from '../tasks/utils';
|
||||
|
||||
import {MessageToWorker} from './api';
|
||||
import {ClusterPackageJsonUpdater} from './package_json_updater';
|
||||
import {ClusterWorkerPackageJsonUpdater} from './package_json_updater';
|
||||
import {sendMessageToMaster} from './utils';
|
||||
|
||||
// Cluster worker entry point
|
||||
@ -55,8 +54,10 @@ if (require.main === module) {
|
||||
pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath);
|
||||
}
|
||||
|
||||
const pkgJsonUpdater =
|
||||
new ClusterPackageJsonUpdater(new DirectPackageJsonUpdater(fileSystem));
|
||||
// NOTE: To avoid file corruption, `ngcc` invocation only creates _one_ instance of
|
||||
// `PackageJsonUpdater` that actually writes to disk (across all processes).
|
||||
// In cluster workers we use a `PackageJsonUpdater` that delegates to the cluster master.
|
||||
const pkgJsonUpdater = new ClusterWorkerPackageJsonUpdater();
|
||||
|
||||
// The function for creating the `compile()` function.
|
||||
const createCompileFn = getCreateCompileFn(
|
||||
|
@ -26,7 +26,6 @@ import {TargetedEntryPointFinder} from './entry_point_finder/targeted_entry_poin
|
||||
import {getAnalyzeEntryPointsFn} from './execution/analyze_entry_points';
|
||||
import {Executor} from './execution/api';
|
||||
import {ClusterExecutor} from './execution/cluster/executor';
|
||||
import {ClusterPackageJsonUpdater} from './execution/cluster/package_json_updater';
|
||||
import {getCreateCompileFn} from './execution/create_compile_function';
|
||||
import {SingleProcessExecutorAsync, SingleProcessExecutorSync} from './execution/single_process_executor';
|
||||
import {CreateTaskCompletedCallback, TaskProcessingOutcome} from './execution/tasks/api';
|
||||
@ -105,11 +104,7 @@ export function mainNgcc({
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE: To avoid file corruption, ensure that each `ngcc` invocation only creates _one_ instance
|
||||
// of `PackageJsonUpdater` that actually writes to disk (across all processes).
|
||||
// This is hard to enforce automatically, when running on multiple processes, so needs to be
|
||||
// enforced manually.
|
||||
const pkgJsonUpdater = getPackageJsonUpdater(inParallel, fileSystem);
|
||||
const pkgJsonUpdater = new DirectPackageJsonUpdater(fileSystem);
|
||||
|
||||
const analyzeEntryPoints = getAnalyzeEntryPointsFn(
|
||||
logger, finder, fileSystem, supportedPropertiesToConsider, compileAllFormats,
|
||||
@ -151,11 +146,6 @@ function ensureSupportedProperties(properties: string[]): EntryPointJsonProperty
|
||||
return supportedProperties;
|
||||
}
|
||||
|
||||
function getPackageJsonUpdater(inParallel: boolean, fs: FileSystem): PackageJsonUpdater {
|
||||
const directPkgJsonUpdater = new DirectPackageJsonUpdater(fs);
|
||||
return inParallel ? new ClusterPackageJsonUpdater(directPkgJsonUpdater) : directPkgJsonUpdater;
|
||||
}
|
||||
|
||||
function getCreateTaskCompletedCallback(
|
||||
pkgJsonUpdater: PackageJsonUpdater, errorOnFailedEntryPoint: boolean, logger: Logger,
|
||||
fileSystem: FileSystem): CreateTaskCompletedCallback {
|
||||
|
Reference in New Issue
Block a user