Skip to content

Commit e22f60b

Browse files
refactor(ngcc): simplify cluster PackageJsonUpdater
1 parent 24d53ef commit e22f60b

File tree

4 files changed

+149
-189
lines changed

4 files changed

+149
-189
lines changed

packages/compiler-cli/ngcc/src/execution/cluster/package_json_updater.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ import {sendMessageToMaster} from './utils';
1818

1919

2020
/**
21-
* A `PackageJsonUpdater` that can safely handle update operations on multiple processes.
21+
* A `PackageJsonUpdater` for cluster workers that will send update changes to the master process so
22+
* that it can safely handle update operations on multiple processes.
2223
*/
23-
export class ClusterPackageJsonUpdater implements PackageJsonUpdater {
24-
constructor(private delegate: PackageJsonUpdater) {}
24+
export class ClusterWorkerPackageJsonUpdater implements PackageJsonUpdater {
25+
constructor() {
26+
if (cluster.isMaster) {
27+
throw new Error('Tried to create cluster worker PackageJsonUpdater on the master process.');
28+
}
29+
}
2530

2631
createUpdate(): PackageJsonUpdate {
2732
return new PackageJsonUpdate((...args) => this.writeChanges(...args));
2833
}
2934

35+
/**
36+
* Apply the changes in-memory (if necessary) and send a message to the master process.
37+
*/
3038
writeChanges(
3139
changes: PackageJsonChange[], packageJsonPath: AbsoluteFsPath,
3240
preExistingParsedJson?: JsonObject): void {
33-
if (cluster.isMaster) {
34-
// This is the master process:
35-
// Actually apply the changes to the file on disk.
36-
return this.delegate.writeChanges(changes, packageJsonPath, preExistingParsedJson);
37-
}
38-
39-
// This is a worker process:
40-
// Apply the changes in-memory (if necessary) and send a message to the master process.
4141
if (preExistingParsedJson) {
4242
for (const [propPath, value] of changes) {
4343
if (propPath.length === 0) {

packages/compiler-cli/ngcc/src/execution/cluster/worker.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {getCreateCompileFn} from '../create_compile_function';
2121
import {stringifyTask} from '../tasks/utils';
2222

2323
import {MessageToWorker} from './api';
24-
import {ClusterPackageJsonUpdater} from './package_json_updater';
24+
import {ClusterWorkerPackageJsonUpdater} from './package_json_updater';
2525
import {sendMessageToMaster} from './utils';
2626

2727
// Cluster worker entry point
@@ -55,8 +55,7 @@ if (require.main === module) {
5555
pathMappings = getPathMappingsFromTsConfig(tsConfig, projectPath);
5656
}
5757

58-
const pkgJsonUpdater =
59-
new ClusterPackageJsonUpdater(new DirectPackageJsonUpdater(fileSystem));
58+
const pkgJsonUpdater = new ClusterWorkerPackageJsonUpdater();
6059

6160
// The function for creating the `compile()` function.
6261
const createCompileFn = getCreateCompileFn(

packages/compiler-cli/ngcc/src/main.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {TargetedEntryPointFinder} from './entry_point_finder/targeted_entry_poin
2626
import {getAnalyzeEntryPointsFn} from './execution/analyze_entry_points';
2727
import {Executor} from './execution/api';
2828
import {ClusterExecutor} from './execution/cluster/executor';
29-
import {ClusterPackageJsonUpdater} from './execution/cluster/package_json_updater';
3029
import {getCreateCompileFn} from './execution/create_compile_function';
3130
import {SingleProcessExecutorAsync, SingleProcessExecutorSync} from './execution/single_process_executor';
3231
import {CreateTaskCompletedCallback, TaskProcessingOutcome} from './execution/tasks/api';
@@ -105,11 +104,7 @@ export function mainNgcc({
105104
return;
106105
}
107106

108-
// NOTE: To avoid file corruption, ensure that each `ngcc` invocation only creates _one_ instance
109-
// of `PackageJsonUpdater` that actually writes to disk (across all processes).
110-
// This is hard to enforce automatically, when running on multiple processes, so needs to be
111-
// enforced manually.
112-
const pkgJsonUpdater = getPackageJsonUpdater(inParallel, fileSystem);
107+
const pkgJsonUpdater = new DirectPackageJsonUpdater(fileSystem);
113108

114109
const analyzeEntryPoints = getAnalyzeEntryPointsFn(
115110
logger, finder, fileSystem, supportedPropertiesToConsider, compileAllFormats,
@@ -151,11 +146,6 @@ function ensureSupportedProperties(properties: string[]): EntryPointJsonProperty
151146
return supportedProperties;
152147
}
153148

154-
function getPackageJsonUpdater(inParallel: boolean, fs: FileSystem): PackageJsonUpdater {
155-
const directPkgJsonUpdater = new DirectPackageJsonUpdater(fs);
156-
return inParallel ? new ClusterPackageJsonUpdater(directPkgJsonUpdater) : directPkgJsonUpdater;
157-
}
158-
159149
function getCreateTaskCompletedCallback(
160150
pkgJsonUpdater: PackageJsonUpdater, errorOnFailedEntryPoint: boolean, logger: Logger,
161151
fileSystem: FileSystem): CreateTaskCompletedCallback {

0 commit comments

Comments
 (0)