refactor(ngcc): add debug messages to help with debugging in parallel mode (#34437)
PR Close #34437
This commit is contained in:
parent
5cecd97493
commit
cd8a837956
|
@ -39,7 +39,7 @@ export class ClusterExecutor implements Executor {
|
|||
return master.run();
|
||||
} else {
|
||||
// This process is a cluster worker.
|
||||
const worker = new ClusterWorker(createCompileFn);
|
||||
const worker = new ClusterWorker(this.logger, createCompileFn);
|
||||
return worker.run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,9 @@
|
|||
|
||||
import * as cluster from 'cluster';
|
||||
|
||||
import {Logger} from '../../logging/logger';
|
||||
import {CompileFn, CreateCompileFn} from '../api';
|
||||
import {stringifyTask} from '../utils';
|
||||
|
||||
import {MessageToWorker} from './api';
|
||||
import {sendMessageToMaster} from './utils';
|
||||
|
@ -23,7 +25,7 @@ import {sendMessageToMaster} from './utils';
|
|||
export class ClusterWorker {
|
||||
private compile: CompileFn;
|
||||
|
||||
constructor(createCompileFn: CreateCompileFn) {
|
||||
constructor(private logger: Logger, createCompileFn: CreateCompileFn) {
|
||||
if (cluster.isMaster) {
|
||||
throw new Error('Tried to instantiate `ClusterWorker` on the master process.');
|
||||
}
|
||||
|
@ -38,10 +40,12 @@ export class ClusterWorker {
|
|||
try {
|
||||
switch (msg.type) {
|
||||
case 'process-task':
|
||||
this.logger.debug(
|
||||
`[Worker #${cluster.worker.id}] Processing task: ${stringifyTask(msg.task)}`);
|
||||
return this.compile(msg.task);
|
||||
default:
|
||||
throw new Error(
|
||||
`Invalid message received on worker #${cluster.worker.id}: ${JSON.stringify(msg)}`);
|
||||
`[Worker #${cluster.worker.id}] Invalid message received: ${JSON.stringify(msg)}`);
|
||||
}
|
||||
} catch (err) {
|
||||
sendMessageToMaster({
|
||||
|
|
|
@ -274,6 +274,8 @@ export function mainNgcc(
|
|||
`Failed to compile entry-point ${entryPoint.name} due to compilation errors:\n${errors}`);
|
||||
}
|
||||
|
||||
logger.debug(` Successfully compiled ${entryPoint.name} : ${formatProperty}`);
|
||||
|
||||
onTaskCompleted(task, TaskProcessingOutcome.Processed);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -13,6 +13,7 @@ import {EventEmitter} from 'events';
|
|||
|
||||
import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/api';
|
||||
import {ClusterWorker} from '../../../src/execution/cluster/worker';
|
||||
import {MockLogger} from '../../helpers/mock_logger';
|
||||
import {mockProperty} from '../../helpers/spy_utils';
|
||||
|
||||
|
||||
|
@ -22,6 +23,7 @@ describe('ClusterWorker', () => {
|
|||
let processSendSpy: jasmine.Spy;
|
||||
let compileFnSpy: jasmine.Spy;
|
||||
let createCompileFnSpy: jasmine.Spy;
|
||||
let mockLogger: MockLogger;
|
||||
|
||||
beforeEach(() => {
|
||||
compileFnSpy = jasmine.createSpy('compileFn');
|
||||
|
@ -29,6 +31,8 @@ describe('ClusterWorker', () => {
|
|||
|
||||
processSendSpy = jasmine.createSpy('process.send');
|
||||
mockProcessSend(processSendSpy);
|
||||
|
||||
mockLogger = new MockLogger();
|
||||
});
|
||||
|
||||
describe('constructor()', () => {
|
||||
|
@ -36,7 +40,7 @@ describe('ClusterWorker', () => {
|
|||
beforeEach(() => runAsClusterMaster(true));
|
||||
|
||||
it('should throw an error', () => {
|
||||
expect(() => new ClusterWorker(createCompileFnSpy))
|
||||
expect(() => new ClusterWorker(mockLogger, createCompileFnSpy))
|
||||
.toThrowError('Tried to instantiate `ClusterWorker` on the master process.');
|
||||
expect(createCompileFnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
@ -46,12 +50,12 @@ describe('ClusterWorker', () => {
|
|||
beforeEach(() => runAsClusterMaster(false));
|
||||
|
||||
it('should create the `compileFn()`', () => {
|
||||
new ClusterWorker(createCompileFnSpy);
|
||||
new ClusterWorker(mockLogger, createCompileFnSpy);
|
||||
expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('should set up `compileFn()` to send a `task-completed` message to master', () => {
|
||||
new ClusterWorker(createCompileFnSpy);
|
||||
new ClusterWorker(mockLogger, createCompileFnSpy);
|
||||
const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[0];
|
||||
|
||||
onTaskCompleted(null as any, TaskProcessingOutcome.AlreadyProcessed);
|
||||
|
@ -86,7 +90,7 @@ describe('ClusterWorker', () => {
|
|||
runAsClusterMaster(false);
|
||||
mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker);
|
||||
|
||||
worker = new ClusterWorker(createCompileFnSpy);
|
||||
worker = new ClusterWorker(mockLogger, createCompileFnSpy);
|
||||
});
|
||||
|
||||
it('should return a promise (that is never resolved)', done => {
|
||||
|
@ -104,27 +108,41 @@ describe('ClusterWorker', () => {
|
|||
});
|
||||
|
||||
it('should handle `process-task` messages', () => {
|
||||
const mockTask = { foo: 'bar' } as unknown as Task;
|
||||
const mockTask = {
|
||||
entryPoint: {name: 'foo'},
|
||||
formatProperty: 'es2015',
|
||||
processDts: true,
|
||||
} as unknown as Task;
|
||||
|
||||
worker.run();
|
||||
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
||||
|
||||
expect(compileFnSpy).toHaveBeenCalledWith(mockTask);
|
||||
expect(processSendSpy).not.toHaveBeenCalled();
|
||||
|
||||
expect(mockLogger.logs.debug[0]).toEqual([
|
||||
'[Worker #42] Processing task: {entryPoint: foo, formatProperty: es2015, processDts: true}',
|
||||
]);
|
||||
});
|
||||
|
||||
it('should send errors during task processing back to the master process', () => {
|
||||
const mockTask = {
|
||||
entryPoint: {name: 'foo'},
|
||||
formatProperty: 'es2015',
|
||||
processDts: true,
|
||||
} as unknown as Task;
|
||||
|
||||
let err: string|Error;
|
||||
compileFnSpy.and.callFake(() => { throw err; });
|
||||
|
||||
worker.run();
|
||||
|
||||
err = 'Error string.';
|
||||
cluster.worker.emit('message', {type: 'process-task', task: {} as Task});
|
||||
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
||||
expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err});
|
||||
|
||||
err = new Error('Error object.');
|
||||
cluster.worker.emit('message', {type: 'process-task', task: {} as Task});
|
||||
cluster.worker.emit('message', {type: 'process-task', task: mockTask});
|
||||
expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err.stack});
|
||||
});
|
||||
|
||||
|
@ -136,7 +154,7 @@ describe('ClusterWorker', () => {
|
|||
expect(processSendSpy).toHaveBeenCalledWith({
|
||||
type: 'error',
|
||||
error: jasmine.stringMatching(
|
||||
'Error: Invalid message received on worker #42: {"type":"unknown","foo":"bar"}'),
|
||||
'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue