2019-08-29 18:47:54 +03: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';
|
|
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
import {parseCommandLineOptions} from '../../command_line_options';
|
|
|
|
|
import {ConsoleLogger} from '../../logging/console_logger';
|
|
|
|
|
import {Logger, LogLevel} from '../../logging/logger';
|
2020-04-16 15:54:40 +01:00
|
|
|
import {getSharedSetup} from '../../ngcc_options';
|
2020-04-15 16:35:09 +01:00
|
|
|
import {CreateCompileFn} from '../api';
|
|
|
|
|
import {getCreateCompileFn} from '../create_compile_function';
|
2020-03-14 13:38:27 +00:00
|
|
|
import {stringifyTask} from '../tasks/utils';
|
2019-08-29 18:47:54 +03:00
|
|
|
|
|
|
|
|
import {MessageToWorker} from './api';
|
2020-04-16 10:37:24 +01:00
|
|
|
import {ClusterWorkerPackageJsonUpdater} from './package_json_updater';
|
2019-08-29 18:47:54 +03:00
|
|
|
import {sendMessageToMaster} from './utils';
|
|
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
// Cluster worker entry point
|
|
|
|
|
if (require.main === module) {
|
|
|
|
|
(async () => {
|
2020-04-16 15:54:40 +01:00
|
|
|
process.title = 'ngcc (worker)';
|
2020-04-15 16:35:09 +01:00
|
|
|
|
2020-04-16 15:54:40 +01:00
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
createNewEntryPointFormats = false,
|
|
|
|
|
logger = new ConsoleLogger(LogLevel.info),
|
|
|
|
|
pathMappings,
|
|
|
|
|
errorOnFailedEntryPoint = false,
|
|
|
|
|
enableI18nLegacyMessageIdFormat = true,
|
|
|
|
|
fileSystem,
|
|
|
|
|
tsConfig
|
|
|
|
|
} = getSharedSetup(parseCommandLineOptions(process.argv.slice(2)));
|
2020-04-15 16:35:09 +01:00
|
|
|
|
2020-04-16 10:37:24 +01:00
|
|
|
// 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();
|
2019-08-29 18:47:54 +03:00
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
// The function for creating the `compile()` function.
|
|
|
|
|
const createCompileFn = getCreateCompileFn(
|
|
|
|
|
fileSystem, logger, pkgJsonUpdater, createNewEntryPointFormats, errorOnFailedEntryPoint,
|
|
|
|
|
enableI18nLegacyMessageIdFormat, tsConfig, pathMappings);
|
|
|
|
|
|
|
|
|
|
await startWorker(logger, createCompileFn);
|
|
|
|
|
process.exitCode = 0;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e.stack || e.message);
|
2020-04-16 15:54:40 +01:00
|
|
|
process.exit(1);
|
2019-08-29 18:47:54 +03:00
|
|
|
}
|
2020-04-15 16:35:09 +01:00
|
|
|
})();
|
|
|
|
|
}
|
2019-08-29 18:47:54 +03:00
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
export async function startWorker(logger: Logger, createCompileFn: CreateCompileFn): Promise<void> {
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
throw new Error('Tried to run cluster worker on the master process.');
|
2019-08-29 18:47:54 +03:00
|
|
|
}
|
|
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
const compile = createCompileFn(
|
2020-04-29 21:28:11 +03:00
|
|
|
() => {},
|
2020-04-15 16:35:09 +01:00
|
|
|
(_task, outcome, message) => sendMessageToMaster({type: 'task-completed', outcome, message}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Listen for `ProcessTaskMessage`s and process tasks.
|
2020-04-29 21:28:11 +03:00
|
|
|
cluster.worker.on('message', async (msg: MessageToWorker) => {
|
2020-04-15 16:35:09 +01:00
|
|
|
try {
|
|
|
|
|
switch (msg.type) {
|
|
|
|
|
case 'process-task':
|
|
|
|
|
logger.debug(
|
|
|
|
|
`[Worker #${cluster.worker.id}] Processing task: ${stringifyTask(msg.task)}`);
|
2020-04-29 21:28:11 +03:00
|
|
|
return await compile(msg.task);
|
2020-04-15 16:35:09 +01:00
|
|
|
default:
|
|
|
|
|
throw new Error(
|
|
|
|
|
`[Worker #${cluster.worker.id}] Invalid message received: ${JSON.stringify(msg)}`);
|
2019-08-29 18:47:54 +03:00
|
|
|
}
|
2020-04-15 16:35:09 +01:00
|
|
|
} catch (err) {
|
|
|
|
|
sendMessageToMaster({
|
|
|
|
|
type: 'error',
|
|
|
|
|
error: (err instanceof Error) ? (err.stack || err.message) : err,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-08-29 18:47:54 +03:00
|
|
|
|
2020-04-15 16:35:09 +01:00
|
|
|
// Return a promise that is never resolved.
|
|
|
|
|
return new Promise(() => undefined);
|
|
|
|
|
}
|