Skip to content

Commit c930aef

Browse files
refactor(logging): enhance error handling and logging in main worker
- Introduced `DefaultLogger` for consistent error logging in the main worker script. - Updated `handleRunError` to utilize the logger for error messages and added a delay before exiting the process. - Refactored the `run` function to simplify its return statement. - Enhanced unit tests to verify the new logging behavior and ensure proper process exit. These changes improve the clarity and reliability of error handling in the application, ensuring that errors are logged consistently and the process exits gracefully.
1 parent 31dfaff commit c930aef

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22
import { vi } from 'vitest';
33

4-
import { handleRunError, run } from '../index';
4+
import { handleRunError, logger, run } from '../index';
55

66
describe('run', () => {
77
it('should return true', async () => {
@@ -11,22 +11,29 @@ describe('run', () => {
1111

1212
describe('handleRunError', () => {
1313
it('should log error and exit process', () => {
14+
vi.useFakeTimers();
1415
const error = new Error('test error');
15-
const consoleErrorSpy = vi
16-
.spyOn(console, 'error')
16+
const loggerErrorSpy = vi
17+
.spyOn(logger, 'error')
1718
.mockImplementation(() => {});
1819
const processExitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
1920
throw new Error('exit');
2021
});
2122

22-
expect(() => handleRunError(error)).toThrow('exit');
23-
expect(consoleErrorSpy).toHaveBeenCalledWith(
24-
'Unhandled error in main:',
25-
error,
23+
expect(() => handleRunError(error)).toThrow(error);
24+
expect(loggerErrorSpy).toHaveBeenCalledWith(
25+
`Unhandled error in main: ${error.message}`,
2626
);
27+
// Таймер еще не сработал
28+
expect(processExitSpy).not.toHaveBeenCalled();
29+
// Прокручиваем таймеры
30+
expect(() => {
31+
vi.runAllTimers();
32+
}).toThrow('exit');
2733
expect(processExitSpy).toHaveBeenCalledWith(1);
2834

29-
consoleErrorSpy.mockRestore();
35+
loggerErrorSpy.mockRestore();
3036
processExitSpy.mockRestore();
37+
vi.useRealTimers();
3138
});
3239
});

workers/main/src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import { DefaultLogger } from '@temporalio/worker';
2+
3+
export const logger = new DefaultLogger('ERROR');
4+
15
/**
26
* Executes the main worker process.
37
* @returns {Promise<boolean>} Returns true when the worker completes successfully.
48
*/
5-
export async function run() {
6-
return await Promise.resolve(true);
9+
export async function run(): Promise<boolean> {
10+
return true;
711
}
812

9-
export function handleRunError(err: unknown) {
10-
console.error('Unhandled error in main:', err);
11-
process.exit(1);
13+
export function handleRunError(err: Error): never {
14+
logger.error(`Unhandled error in main: ${err.message}`);
15+
setTimeout(() => process.exit(1), 100);
16+
throw err;
1217
}
1318

1419
run().catch(handleRunError);

0 commit comments

Comments
 (0)