Skip to content

Commit

Permalink
Merge pull request #48 from cloudbeds/fix/worker-process-termination
Browse files Browse the repository at this point in the history
fix: worker process termination and logging
  • Loading branch information
steven-pribilinskiy authored Oct 16, 2024
2 parents 7b25408 + aacc3f5 commit 6c7927d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/compileTypes/compileTypesAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export function compileTypesAsync(
): Promise<void> {
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' });
}

Expand All @@ -29,43 +30,43 @@ 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;
});

worker.on('exit', code => {
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;
});
Expand Down

0 comments on commit 6c7927d

Please sign in to comment.