@@ -8,7 +8,8 @@ import process from 'node:process';
88import readline from 'node:readline' ;
99import { parseArgs } from 'node:util' ;
1010
11- import { logger , saveLogsToFileSync } from '../../logger.js' ;
11+ import { logger , flushLogs , saveLogsToFile } from '../../logger.js' ;
12+ import type { WriteStream } from 'node:fs' ;
1213import type { OsType } from '../types.js' ;
1314import { WatchdogMessageType } from '../types.js' ;
1415
@@ -29,16 +30,28 @@ function main() {
2930 const appVersion = values [ 'app-version' ] ;
3031 const osType = parseInt ( values [ 'os-type' ] ?? '' , 10 ) ;
3132 const logFile = values [ 'log-file' ] ;
33+ let logStream : WriteStream | undefined ;
3234 if ( logFile ) {
33- saveLogsToFileSync ( logFile ) ;
35+ logStream = saveLogsToFile ( logFile ) ;
3436 }
3537
38+ const exit = ( code : number ) => {
39+ if ( ! logStream ) {
40+ process . exit ( code ) ;
41+ }
42+
43+ flushLogs ( logStream ) . finally ( ( ) => {
44+ process . exit ( code ) ;
45+ } ) ;
46+ } ;
47+
3648 if ( isNaN ( parentPid ) || ! appVersion || isNaN ( osType ) ) {
3749 logger (
3850 'Invalid arguments provided for watchdog process: ' ,
3951 JSON . stringify ( { parentPid, appVersion, osType} ) ,
4052 ) ;
41- process . exit ( 1 ) ;
53+ exit ( 1 ) ;
54+ return ;
4255 }
4356
4457 logger (
@@ -69,11 +82,11 @@ function main() {
6982 . sendShutdownEvent ( )
7083 . then ( ( ) => {
7184 logger ( 'Shutdown event sent. Exiting.' ) ;
72- process . exit ( 0 ) ;
85+ exit ( 0 ) ;
7386 } )
7487 . catch ( err => {
7588 logger ( 'Failed to send shutdown event' , err ) ;
76- process . exit ( 1 ) ;
89+ exit ( 1 ) ;
7790 } ) ;
7891 }
7992
0 commit comments