44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7-
87import process from 'node:process' ;
98import readline from 'node:readline' ;
10-
11- import yargs from 'yargs' ;
12- import { hideBin } from 'yargs/helpers' ;
9+ import { parseArgs } from 'node:util' ;
1310
1411import { logger , saveLogsToFileSync } from '../../logger.js' ;
15- import type { OsType } from '../types.js' ;
12+ import type { OsType } from '../types.js' ;
1613import { IpcMessageType } from '../types.js' ;
1714
1815import { ClearcutSender } from './clearcut-sender.js' ;
1916
20- async function main ( ) {
21- const argv = await yargs ( hideBin ( process . argv ) )
22- . option ( 'parent-pid' , {
23- type : 'number' ,
24- demandOption : true ,
25- describe : 'PID of the main process to monitor' ,
26- } )
27- . option ( 'app-version' , {
28- type : 'string' ,
29- demandOption : true ,
30- describe : 'Application version string' ,
31- } )
32- . option ( 'os-type' , {
33- type : 'number' ,
34- demandOption : true ,
35- describe : 'Integer specific to OsType' ,
36- } )
37- . option ( 'log-file' , {
38- type : 'string' ,
39- describe : 'Optional path to log file' ,
40- } )
41- . parse ( ) ;
17+ function main ( ) {
18+ const { values} = parseArgs ( {
19+ options : {
20+ 'parent-pid' : { type : 'string' } ,
21+ 'app-version' : { type : 'string' } ,
22+ 'os-type' : { type : 'string' } ,
23+ 'log-file' : { type : 'string' } ,
24+ } ,
25+ strict : true ,
26+ } ) ;
27+
28+ const parentPid = parseInt ( values [ 'parent-pid' ] ?? '' , 10 ) ;
29+ const appVersion = values [ 'app-version' ] ;
30+ const osType = parseInt ( values [ 'os-type' ] ?? '' , 10 ) ;
31+ const logFile = values [ 'log-file' ] ;
32+ if ( logFile ) {
33+ saveLogsToFileSync ( logFile ) ;
34+ }
4235
43- if ( argv . logFile ) {
44- saveLogsToFileSync ( argv . logFile ) ;
36+ if ( isNaN ( parentPid ) || ! appVersion || isNaN ( osType ) ) {
37+ logger (
38+ 'Invalid arguments provided for watchdog process: ' ,
39+ JSON . stringify ( { parentPid, appVersion, osType} ) ,
40+ ) ;
41+ process . exit ( 1 ) ;
4542 }
4643
47- logger ( 'Watchdog started' , JSON . stringify ( {
48- pid : process . pid ,
49- parentPid : argv . parentPid ,
50- version : argv . appVersion ,
51- osType : argv . osType ,
52- } , null , 2 ) ) ;
44+ logger (
45+ 'Watchdog started' ,
46+ JSON . stringify (
47+ {
48+ pid : process . pid ,
49+ parentPid,
50+ version : appVersion ,
51+ osType,
52+ } ,
53+ null ,
54+ 2 ,
55+ ) ,
56+ ) ;
5357
54- const sender = new ClearcutSender ( argv . appVersion , argv . osType as OsType ) ;
58+ const sender = new ClearcutSender ( appVersion , osType as OsType ) ;
5559
5660 let isShuttingDown = false ;
5761 function onParentDeath ( reason : string ) {
@@ -67,7 +71,7 @@ async function main() {
6771 logger ( 'Shutdown event sent. Exiting.' ) ;
6872 process . exit ( 0 ) ;
6973 } )
70- . catch ( ( err ) => {
74+ . catch ( err => {
7175 logger ( 'Failed to send shutdown event' , err ) ;
7276 process . exit ( 1 ) ;
7377 } ) ;
@@ -82,13 +86,15 @@ async function main() {
8286 terminal : false ,
8387 } ) ;
8488
85- rl . on ( 'line' , ( line ) => {
89+ rl . on ( 'line' , line => {
8690 try {
87- if ( ! line . trim ( ) ) { return ; }
88-
91+ if ( ! line . trim ( ) ) {
92+ return ;
93+ }
94+
8995 const msg = JSON . parse ( line ) ;
9096 if ( msg . type === IpcMessageType . EVENT && msg . payload ) {
91- sender . send ( msg . payload ) . catch ( ( err ) => {
97+ sender . send ( msg . payload ) . catch ( err => {
9298 logger ( 'Error sending event' , err ) ;
9399 } ) ;
94100 }
@@ -98,7 +104,9 @@ async function main() {
98104 } ) ;
99105}
100106
101- main ( ) . catch ( ( err ) => {
107+ try {
108+ main ( ) ;
109+ } catch ( err ) {
102110 console . error ( 'Watchdog fatal error:' , err ) ;
103111 process . exit ( 1 ) ;
104- } ) ;
112+ }
0 commit comments