1+ #!/usr/bin/env node
2+ const { fork } = require ( 'child_process' )
3+ const chalk = require ( 'chalk' )
4+ const { Transform } = require ( 'stream' )
5+ const { Logger } = require ( 'streamr-network' )
6+
7+ const processes = new Map ( )
8+
9+ const killProcesses = ( ) => {
10+ processes . forEach ( ( p ) => {
11+ if ( ! p . killed ) {
12+ p . kill ( )
13+ }
14+ } )
15+ }
16+
17+ const createCollectLineTransform = ( ) => {
18+ let prevMsg = ''
19+ return new Transform ( {
20+ transform : ( buf , _encoding , done ) => {
21+ const msg = buf . toString ( )
22+ if ( msg . endsWith ( '\n' ) ) {
23+ const fullMsg = prevMsg + msg
24+ done ( null , fullMsg )
25+ prevMsg = ''
26+ } else {
27+ prevMsg = msg
28+ done ( )
29+ }
30+ }
31+ } )
32+ }
33+
34+ const createColorizeTransform = ( defaultColor ) => {
35+ const getLevelColor = ( id ) => {
36+ if ( id === 'ERROR' ) {
37+ return chalk . hex ( '#FF0000' )
38+ } else if ( id === 'WARN' ) {
39+ return chalk . hex ( '#FFFF00' )
40+ } else {
41+ return undefined
42+ }
43+ }
44+ const META_PATTERN = new RegExp ( `^(ERROR|WARN|INFO|DEBUG|TRACE) \\[(.{23})\\] \\((.{${ Logger . NAME_LENGTH } })\\):` )
45+ return new Transform ( {
46+ transform : ( buf , _encoding , done ) => {
47+ const msg = buf . toString ( )
48+ const lines = msg . split ( '\n' ) . map ( ( line ) => {
49+ const groups = line . match ( META_PATTERN )
50+ if ( groups !== null ) {
51+ const match = groups [ 0 ]
52+ const level = groups [ 1 ]
53+ const time = groups [ 2 ]
54+ const name = groups [ 3 ]
55+ const levelColor = getLevelColor ( level )
56+ const metaPrefix = `${ level . substring ( 0 , 1 ) } ${ time } ${ name } `
57+ const msg = line . substring ( match . length )
58+ if ( levelColor !== undefined ) {
59+ return levelColor ( `${ metaPrefix } ${ msg } ` )
60+ } else {
61+ return `${ defaultColor ( metaPrefix ) } ${ msg } `
62+ }
63+ } else {
64+ return line
65+ }
66+ } )
67+ done ( null , lines . join ( '\n' ) )
68+ }
69+ } )
70+ }
71+
72+ const pipeOutputStreams = ( p , defaultColor ) => {
73+ p . stdout
74+ . pipe ( createCollectLineTransform ( ) )
75+ . pipe ( createColorizeTransform ( defaultColor ) )
76+ . pipe ( process . stdout )
77+ p . stderr
78+ . pipe ( process . stderr )
79+ }
80+
81+ const getProcessLogLevel = ( processName ) => {
82+ const key = `LOG_LEVEL_${ processName } `
83+ return process . env [ key ] || process . env . LOG_LEVEL
84+ }
85+
86+ const forkProcess = ( processName , filePath , args , color ) => {
87+ const p = fork ( filePath , args , {
88+ silent : true ,
89+ env : {
90+ STREAMR_APPLICATION_ID : processName ,
91+ LOG_LEVEL : getProcessLogLevel ( processName ) ,
92+ LOG_COLORS : 'false'
93+ }
94+ } )
95+ processes . set ( processName , p )
96+ pipeOutputStreams ( p , color )
97+ p . on ( 'close' , ( ) => {
98+ killProcesses ( )
99+ } )
100+ }
101+
102+ forkProcess ( 'T1' , './tracker.js' , [ '0xa4fc7b912ed4588080ac9f2044cb3d6cab961bcf0d96de6fa9e773860bf59e1c' , 'T1' , '--port=30301' ] , chalk . hex ( '#66CC66' ) ) // 0xDE11165537ef6C01260ee89A850a281525A5b63F
103+ forkProcess ( 'T2' , './tracker.js' , [ '0x3b328103c0d043cd1b34f1203c0313eb70c98443444dfdf7125f2d4479b89eb9' , 'T2' , '--port=30302' ] , chalk . hex ( '#00FF66' ) ) // 0xDE22222da3F861c2Ec63b03e16a1dce153Cf069c
104+ forkProcess ( 'T3' , './tracker.js' , [ '0xf456bb2a958574416f4b9e1b1b642cb3092953aa6131aba6e36f7529d1137795' , 'T3' , '--port=30303' ] , chalk . hex ( '#66FFAA' ) ) // 0xDE33390cC85aBf61d9c27715Fa61d8E5efC61e75
105+
106+ forkProcess ( 'S1' , './broker.js' , [ '../configs/development-1.env.json' ] , chalk . hex ( '#8888FF' ) ) // 0xde1112f631486CfC759A50196853011528bC5FA0
107+ forkProcess ( 'B1' , './broker.js' , [ '../configs/development-2.env.json' ] , chalk . hex ( '#0088FF' ) ) // 0xde222E8603FCf641F928E5F66a0CBf4de70d5352
108+ forkProcess ( 'B2' , './broker.js' , [ '../configs/development-3.env.json' ] , chalk . hex ( '#88CCFF' ) ) // 0xde3331cA6B8B636E0b82Bf08E941F727B8927442
0 commit comments