From aacc3f56c187d0fd1ad6cc508a6aa4b4a869a652 Mon Sep 17 00:00:00 2001 From: Steven Prybylynskyi Date: Wed, 16 Oct 2024 18:17:17 +0200 Subject: [PATCH] fix: worker process termination and logging This commit refactors the code in compileTypesAsync.ts to improve the termination of the worker process and enhance the logging. The worker process termination now includes sending an 'exit' message to the worker and setting the worker variable to null. Additionally, the logging messages now include the specific run number of the worker process. This change improves the overall reliability and clarity of the code. --- src/compileTypes/compileTypesAsync.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/compileTypes/compileTypesAsync.ts b/src/compileTypes/compileTypesAsync.ts index 0eb94e8..fe0e532 100644 --- a/src/compileTypes/compileTypesAsync.ts +++ b/src/compileTypes/compileTypesAsync.ts @@ -16,10 +16,11 @@ export function compileTypesAsync( ): Promise { const logger = getLogger(); workerIndex++; + const innerWorkerIndex = workerIndex; return new Promise((resolve, reject) => { if (worker) { - logger.log(`Terminating existing worker process #${workerIndex}`); + logger.log(`Terminating existing worker process #${innerWorkerIndex}`); worker.postMessage({ type: 'exit' }); } @@ -29,35 +30,35 @@ export function compileTypesAsync( worker.on('message', (result: CompileTypesWorkerResultMessage) => { switch (result.status) { case 'log': - logger[result.level](`[Worker #${workerIndex}]:`, result.message); + logger[result.level](`[Worker] run #${innerWorkerIndex}:`, result.message); return; case 'success': resolve(); break; case 'failure': logger.warn( - `[Worker #${workerIndex}]: Failed to compile types for exposed modules.`, + `[Worker] run #${innerWorkerIndex}: Failed to compile types for exposed modules.`, loggerHint, ); reject(new Error('Failed to compile types for exposed modules.')); break; case 'error': logger.warn( - `[Worker #${workerIndex}]: Error compiling types for exposed modules.`, + `[Worker] run #${innerWorkerIndex}: Error compiling types for exposed modules.`, loggerHint, ); reject(result.error); break; } - worker?.terminate(); + worker?.postMessage({ type: 'exit' }); worker = null; }); worker.on('error', error => { - logger.warn(`[Worker #${workerIndex}]: Unexpected error.`, loggerHint); + logger.warn(`[Worker] run #${innerWorkerIndex}: Unexpected error.`, loggerHint); logger.log(error); reject(error); - worker?.terminate(); + worker?.postMessage({ type: 'exit' }); worker = null; }); @@ -65,7 +66,7 @@ export function compileTypesAsync( if (code === null || code === 0) { resolve(); } else { - reject(new Error(`[Worker #${workerIndex}]: Process exited with code ${code}`)); + reject(new Error(`[Worker] run #${innerWorkerIndex}: Process exited with code ${code}`)); } worker = null; });