refactor(ngcc): add debug messages to help with debugging in parallel mode (#34437)

PR Close #34437
This commit is contained in:
George Kalpakas 2019-12-05 21:02:57 +02:00 committed by Kara Erickson
parent 5cecd97493
commit cd8a837956
4 changed files with 35 additions and 11 deletions

View File

@ -39,7 +39,7 @@ export class ClusterExecutor implements Executor {
return master.run(); return master.run();
} else { } else {
// This process is a cluster worker. // This process is a cluster worker.
const worker = new ClusterWorker(createCompileFn); const worker = new ClusterWorker(this.logger, createCompileFn);
return worker.run(); return worker.run();
} }
} }

View File

@ -10,7 +10,9 @@
import * as cluster from 'cluster'; import * as cluster from 'cluster';
import {Logger} from '../../logging/logger';
import {CompileFn, CreateCompileFn} from '../api'; import {CompileFn, CreateCompileFn} from '../api';
import {stringifyTask} from '../utils';
import {MessageToWorker} from './api'; import {MessageToWorker} from './api';
import {sendMessageToMaster} from './utils'; import {sendMessageToMaster} from './utils';
@ -23,7 +25,7 @@ import {sendMessageToMaster} from './utils';
export class ClusterWorker { export class ClusterWorker {
private compile: CompileFn; private compile: CompileFn;
constructor(createCompileFn: CreateCompileFn) { constructor(private logger: Logger, createCompileFn: CreateCompileFn) {
if (cluster.isMaster) { if (cluster.isMaster) {
throw new Error('Tried to instantiate `ClusterWorker` on the master process.'); throw new Error('Tried to instantiate `ClusterWorker` on the master process.');
} }
@ -38,10 +40,12 @@ export class ClusterWorker {
try { try {
switch (msg.type) { switch (msg.type) {
case 'process-task': case 'process-task':
this.logger.debug(
`[Worker #${cluster.worker.id}] Processing task: ${stringifyTask(msg.task)}`);
return this.compile(msg.task); return this.compile(msg.task);
default: default:
throw new Error( 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) { } catch (err) {
sendMessageToMaster({ sendMessageToMaster({

View File

@ -274,6 +274,8 @@ export function mainNgcc(
`Failed to compile entry-point ${entryPoint.name} due to compilation errors:\n${errors}`); `Failed to compile entry-point ${entryPoint.name} due to compilation errors:\n${errors}`);
} }
logger.debug(` Successfully compiled ${entryPoint.name} : ${formatProperty}`);
onTaskCompleted(task, TaskProcessingOutcome.Processed); onTaskCompleted(task, TaskProcessingOutcome.Processed);
}; };
}; };

View File

@ -13,6 +13,7 @@ import {EventEmitter} from 'events';
import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/api'; import {Task, TaskCompletedCallback, TaskProcessingOutcome} from '../../../src/execution/api';
import {ClusterWorker} from '../../../src/execution/cluster/worker'; import {ClusterWorker} from '../../../src/execution/cluster/worker';
import {MockLogger} from '../../helpers/mock_logger';
import {mockProperty} from '../../helpers/spy_utils'; import {mockProperty} from '../../helpers/spy_utils';
@ -22,6 +23,7 @@ describe('ClusterWorker', () => {
let processSendSpy: jasmine.Spy; let processSendSpy: jasmine.Spy;
let compileFnSpy: jasmine.Spy; let compileFnSpy: jasmine.Spy;
let createCompileFnSpy: jasmine.Spy; let createCompileFnSpy: jasmine.Spy;
let mockLogger: MockLogger;
beforeEach(() => { beforeEach(() => {
compileFnSpy = jasmine.createSpy('compileFn'); compileFnSpy = jasmine.createSpy('compileFn');
@ -29,6 +31,8 @@ describe('ClusterWorker', () => {
processSendSpy = jasmine.createSpy('process.send'); processSendSpy = jasmine.createSpy('process.send');
mockProcessSend(processSendSpy); mockProcessSend(processSendSpy);
mockLogger = new MockLogger();
}); });
describe('constructor()', () => { describe('constructor()', () => {
@ -36,7 +40,7 @@ describe('ClusterWorker', () => {
beforeEach(() => runAsClusterMaster(true)); beforeEach(() => runAsClusterMaster(true));
it('should throw an error', () => { it('should throw an error', () => {
expect(() => new ClusterWorker(createCompileFnSpy)) expect(() => new ClusterWorker(mockLogger, createCompileFnSpy))
.toThrowError('Tried to instantiate `ClusterWorker` on the master process.'); .toThrowError('Tried to instantiate `ClusterWorker` on the master process.');
expect(createCompileFnSpy).not.toHaveBeenCalled(); expect(createCompileFnSpy).not.toHaveBeenCalled();
}); });
@ -46,12 +50,12 @@ describe('ClusterWorker', () => {
beforeEach(() => runAsClusterMaster(false)); beforeEach(() => runAsClusterMaster(false));
it('should create the `compileFn()`', () => { it('should create the `compileFn()`', () => {
new ClusterWorker(createCompileFnSpy); new ClusterWorker(mockLogger, createCompileFnSpy);
expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function)); expect(createCompileFnSpy).toHaveBeenCalledWith(jasmine.any(Function));
}); });
it('should set up `compileFn()` to send a `task-completed` message to master', () => { 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]; const onTaskCompleted: TaskCompletedCallback = createCompileFnSpy.calls.argsFor(0)[0];
onTaskCompleted(null as any, TaskProcessingOutcome.AlreadyProcessed); onTaskCompleted(null as any, TaskProcessingOutcome.AlreadyProcessed);
@ -86,7 +90,7 @@ describe('ClusterWorker', () => {
runAsClusterMaster(false); runAsClusterMaster(false);
mockClusterWorker(Object.assign(new EventEmitter(), {id: 42}) as cluster.Worker); 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 => { it('should return a promise (that is never resolved)', done => {
@ -104,27 +108,41 @@ describe('ClusterWorker', () => {
}); });
it('should handle `process-task` messages', () => { 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(); worker.run();
cluster.worker.emit('message', {type: 'process-task', task: mockTask}); cluster.worker.emit('message', {type: 'process-task', task: mockTask});
expect(compileFnSpy).toHaveBeenCalledWith(mockTask); expect(compileFnSpy).toHaveBeenCalledWith(mockTask);
expect(processSendSpy).not.toHaveBeenCalled(); 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', () => { 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; let err: string|Error;
compileFnSpy.and.callFake(() => { throw err; }); compileFnSpy.and.callFake(() => { throw err; });
worker.run(); worker.run();
err = 'Error string.'; 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}); expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err});
err = new Error('Error object.'); 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}); expect(processSendSpy).toHaveBeenCalledWith({type: 'error', error: err.stack});
}); });
@ -136,7 +154,7 @@ describe('ClusterWorker', () => {
expect(processSendSpy).toHaveBeenCalledWith({ expect(processSendSpy).toHaveBeenCalledWith({
type: 'error', type: 'error',
error: jasmine.stringMatching( error: jasmine.stringMatching(
'Error: Invalid message received on worker #42: {"type":"unknown","foo":"bar"}'), 'Error: \\[Worker #42\\] Invalid message received: {"type":"unknown","foo":"bar"}'),
}); });
}); });
}); });