perf(ngcc): spawn workers lazily (#35719)

With this change we spawn workers lazily based on the amount of work that needs to be done.

Before this change we spawned the maximum of workers possible. However, in some cases there are less tasks than the max number of workers which resulted in created unnecessary workers

Reference: #35717

PR Close #35719
This commit is contained in:
Alan Agius 2020-02-28 07:54:16 +01:00 committed by atscott
parent 0a5a841994
commit dc40a93317
1 changed files with 15 additions and 8 deletions

View File

@ -41,6 +41,10 @@ export class ClusterMaster {
}
run(): Promise<void> {
if (this.taskQueue.allTasksCompleted) {
return Promise.resolve();
}
// Set up listeners for worker events (emitted on `cluster`).
cluster.on('online', this.wrapEventHandler(worker => this.onWorkerOnline(worker.id)));
@ -51,10 +55,8 @@ export class ClusterMaster {
'exit',
this.wrapEventHandler((worker, code, signal) => this.onWorkerExit(worker, code, signal)));
// Start the workers.
for (let i = 0; i < this.workerCount; i++) {
cluster.fork();
}
// Since we have pending tasks at the very minimum we need a single worker.
cluster.fork();
return this.finishedDeferred.promise.then(() => this.stopWorkers(), err => {
this.stopWorkers();
@ -98,11 +100,16 @@ export class ClusterMaster {
isWorkerAvailable = false;
}
// If there are no available workers or no available tasks, log (for debugging purposes).
if (!isWorkerAvailable) {
this.logger.debug(
`All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` +
'work.');
if (this.taskAssignments.size < this.workerCount) {
this.logger.debug('Spawning another worker process as there is more work to be done.');
cluster.fork();
} else {
// If there are no available workers or no available tasks, log (for debugging purposes).
this.logger.debug(
`All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` +
'work.');
}
} else {
const busyWorkers = Array.from(this.taskAssignments)
.filter(([_workerId, task]) => task !== null)