1
- const { spawn } = require ( 'child_process' ) ;
2
- import * as fs from 'fs' ;
1
+ import { fork , ChildProcess } from 'child_process' ;
3
2
4
- const child = spawn ( 'npm' , [ 'run' , 'start' ] , {
5
- stdio : 'inherit' ,
6
- shell : true
7
- } ) ;
3
+ export function startNotificationAggregatorProcess ( ) {
4
+ const aggregator_process : ChildProcess = fork ( './dist/index.js' )
8
5
6
+ aggregator_process . on ( 'message' , ( message : string ) => {
7
+ console . log ( `Message from the notification aggregator process: ${ message } ` ) ;
8
+ } ) ;
9
9
10
- function getTimestamp ( ) {
11
- const now = new Date ( ) ;
12
- return ` ${ now . getFullYear ( ) } - ${ ( now . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) } - ${ now . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) } - ${ now . getHours ( ) . toString ( ) . padStart ( 2 , '0' ) } - ${ now . getMinutes ( ) . toString ( ) . padStart ( 2 , '0' ) } - ${ now . getSeconds ( ) . toString ( ) . padStart ( 2 , '0' ) } ` ;
10
+ aggregator_process . on ( 'exit' , ( code : number , signal : string ) => {
11
+ console . log ( `Notification aggregator process exited with code ${ code } and signal ${ signal } ` ) ;
12
+ } ) ;
13
13
}
14
14
15
- interface MemoryUsage {
16
- rss : number ;
17
- heapTotal : number ;
18
- heapUsed : number ;
19
- external : number ;
20
- }
21
-
22
- function logCPUMemoryUsage ( logFile : string ) {
23
- const cpuUsage = process . cpuUsage ( ) ;
24
- const memoryUsage : MemoryUsage = process . memoryUsage ( ) ;
25
- const timestamp = Date . now ( ) ;
26
- const logLine = `${ timestamp } ,${ cpuUsage . user } ,${ cpuUsage . system } ,${ memoryUsage . rss } ,${ memoryUsage . heapTotal } ,${ memoryUsage . heapUsed } ,${ memoryUsage . external } \n` ;
27
- fs . appendFileSync ( logFile , logLine ) ;
28
- }
29
-
30
- const timestamp = getTimestamp ( ) ;
31
- const logFile = `notifications-stream-aggregator-resource-usage-${ timestamp } .csv` ;
32
- fs . writeFileSync ( logFile , 'timestamp,cpu_user,cpu_system,memory_rss,memory_heapTotal,memory_heapUsed,memory_external\n' ) ;
33
-
34
- setInterval ( ( ) => {
35
- logCPUMemoryUsage ( logFile ) ;
36
- } , 500 ) ;
37
-
38
- child . on ( 'exit' , ( code : any ) => {
39
- console . log ( `Child process exited with code ${ code } ` ) ;
40
- process . exit ( code ) ;
41
- } ) ;
42
-
43
- child . on ( 'error' , ( err : any ) => {
44
- console . log ( `Child process error: ${ err } ` ) ;
45
- process . exit ( 1 ) ;
46
- } ) ;
15
+ startNotificationAggregatorProcess ( ) ;
0 commit comments