1- const { exec} = require ( 'child_process' )
2- const { returnStringType} = require ( './helpers' )
1+ const { exec} = require ( 'child_process' ) ;
2+ const { returnStringType} = require ( './helpers' ) ;
33
4- function buildExecRunner ( failedByAssert , runOpts ) {
4+ function millisecondsToMinutes ( milliseconds ) {
5+ const minutes = Math . floor ( milliseconds / 60000 ) ;
6+ const seconds = ( ( milliseconds % 60000 ) / 1000 ) . toFixed ( 0 ) ;
7+ return ( seconds === '60' ? ( minutes + 1 ) + ":00" : minutes + ":" + ( seconds < 10 ? "0" : "" ) + seconds ) ;
8+ }
59
10+ function buildExecRunner ( failedByAssert , runOpts ) {
611 const {
712 addSpecificOptionsBeforeRun,
813 currentExecutionVariable,
@@ -11,124 +16,120 @@ function buildExecRunner(failedByAssert, runOpts) {
1116 reformatCommand,
1217 stackAnalize,
1318 execOpts = { maxBuffer : 1000 * 1024 }
14- } = runOpts
15-
16- const executeCommandAsync = ( cmd , index ) => new Promise ( ( resolve ) => {
17- let execProc = null
18-
19- let additionalOpts = null
20- let originalCmd = cmd
21- let specificCallBack = null
22- let executionStack = ''
19+ } = runOpts ;
2320
21+ return ( cmd , index ) => new Promise ( ( resolve ) => {
22+ let additionalOpts = null ;
23+ let originalCmd = cmd ;
24+ let specificCallBack = null ;
25+ let executionStack = '' ;
2426 /**
2527 * @now this variable will be used for process kill if time more than @longestProcessTime
2628 */
27- const startTime = + Date . now ( )
29+ const startTime = + Date . now ( ) ;
2830
2931 /**
3032 * @param {undefined|function } addSpecificOptions if function cmd will go to this function as argument
3133 */
32- if ( addSpecificOptionsBeforeRun ) {
33- const cmdObj = addSpecificOptionsBeforeRun ( cmd )
34- cmd = cmdObj . cmd
35- specificCallBack = cmdObj . cmdExecutableCB
36- additionalOpts = cmd . replace ( originalCmd , '' )
34+ if ( addSpecificOptionsBeforeRun ) {
35+ const cmdObj = addSpecificOptionsBeforeRun ( cmd ) ;
36+ cmd = cmdObj . cmd ;
37+ specificCallBack = cmdObj . cmdExecutableCB ;
38+ additionalOpts = cmd . replace ( originalCmd , '' ) ;
3739 }
3840
39- if ( currentExecutionVariable ) {
40- if ( cmd . includes ( currentExecutionVariable ) ) {
41- cmd = cmd . replace ( new RegExp ( `${ currentExecutionVariable } =\\d+` , 'ig' ) , `${ currentExecutionVariable } =${ index } ` )
41+ if ( currentExecutionVariable ) {
42+ if ( cmd . includes ( currentExecutionVariable ) ) {
43+ cmd = cmd . replace ( new RegExp ( `${ currentExecutionVariable } =\\d+` , 'ig' ) , `${ currentExecutionVariable } =${ index } ` ) ;
4244 } else {
43- cmd = `${ currentExecutionVariable } =${ index } ${ cmd } `
45+ cmd = `${ currentExecutionVariable } =${ index } ${ cmd } ` ;
4446 }
4547 }
4648
47- execProc = exec ( cmd , execOpts , ( error , stdout , stderr ) => {
48- if ( debugProcess ) {
49- console . log ( '___________________________________________________________________________' )
50- console . log ( `command for process: ${ cmd } ` )
49+ const execProc = exec ( cmd , execOpts , ( error , stdout , stderr ) => {
50+ if ( debugProcess ) {
51+ console . log ( '___________________________________________________________________________' ) ;
52+ console . log ( `command for process: ${ cmd } ` ) ;
53+ console . log ( `process duration: ${ millisecondsToMinutes ( + Date . now ( ) - startTime ) } ` ) ;
54+ console . log ( `PID: ${ execProc . pid } ` ) ;
5155 console . log ( `stdout: ${ stdout } ` ) ;
5256 console . error ( `stderr: ${ stderr } ` ) ;
53- console . error ( `error ${ error } ` )
54- console . log ( '___________________________________________________________________________' )
57+ console . error ( `error: ${ error } ` ) ;
58+ console . log ( '___________________________________________________________________________' ) ;
5559 }
56-
57- executionStack += `${ stdout } ${ stderr } `
58- } )
59-
60-
61- const killTooLongExecution = ( procWhatShouldBekilled ) => {
62- if ( + Date . now ( ) - startTime > longestProcessTime ) {
63- procWhatShouldBekilled . kill ( )
60+ executionStack += `${ stdout } ${ stderr } ` ;
61+ } ) ;
62+
63+ const killTooLongExecution = ( procWhatShouldBeKilled ) => {
64+ const executionTime = + Date . now ( ) - startTime ;
65+ if ( executionTime > longestProcessTime ) {
66+ if ( debugProcess ) {
67+ console . log ( `Process killed due to long time execution: ${ millisecondsToMinutes ( executionTime ) } ` ) ;
68+ }
69+ procWhatShouldBeKilled . kill ( ) ;
6470 }
65- }
71+ } ;
6672
67- const watcher = setInterval ( ( ) => killTooLongExecution ( execProc ) , 5000 )
73+ const watcher = setInterval ( ( ) => killTooLongExecution ( execProc ) , 5000 ) ;
6874
69- execProc . on ( 'exit' , ( ) => {
70- if ( debugProcess ) {
71- console . log ( ' EXIT PROCESS' )
75+ execProc . on ( 'exit' , ( code , signal ) => {
76+ if ( debugProcess ) {
77+ console . log ( ` EXIT PROCESS: PID=" ${ execProc . pid } ", code=" ${ code } " and signal=" ${ signal } "` ) ;
7278 }
73- } )
79+ } ) ;
7480
75- execProc . on ( 'close ' , ( ) => {
76- if ( debugProcess ) {
77- console . log ( 'CLOSE PROCESS' )
81+ execProc . on ( 'error ' , ( e ) => {
82+ if ( debugProcess ) {
83+ console . log ( `ERROR PROCESS: PID=" ${ execProc . pid } "` ) ;
7884 }
79- clearInterval ( watcher )
80- } )
85+ console . error ( e ) ;
86+ } ) ;
8187
82- execProc . on ( 'error' , ( e ) => { console . error ( e ) } )
83-
84- execProc . on ( 'close' , async ( code ) => {
88+ execProc . on ( 'close' , async ( code , signal ) => {
89+ if ( debugProcess ) {
90+ console . log ( `CLOSE PROCESS: PID="${ execProc . pid } ", code="${ code } " and signal="${ signal } "` ) ;
91+ }
8592 // clear watcher interval
86- clearInterval ( watcher )
87-
88- let commandToRerun = null
93+ clearInterval ( watcher ) ;
8994
95+ let commandToRerun = null ;
9096
9197 // if process code 0 - exit as a success result
92- if ( code === 0 ) {
93- resolve ( commandToRerun ) ; return
98+ if ( code === 0 ) {
99+ resolve ( commandToRerun ) ;
100+ return ;
94101 }
95102 // stackAnalize - check that stack contains or not contains some specific data
96- if ( code !== 0 && stackAnalize && stackAnalize ( executionStack ) ) {
97- commandToRerun = cmd
98- } else if ( code !== 0 && reformatCommand ) {
99- commandToRerun = cmd
103+ if ( stackAnalize && stackAnalize ( executionStack ) ) {
104+ commandToRerun = cmd ;
105+ } else if ( reformatCommand ) {
106+ commandToRerun = cmd ;
100107 } else {
101- failedByAssert . push ( cmd )
108+ failedByAssert . push ( cmd ) ;
102109 }
103110
104111 // if code === 0 do nothing, success
105- if ( specificCallBack ) {
106- if ( specificCallBack . then || returnStringType ( specificCallBack ) === '[object AsyncFunction]' ) {
107- await specificCallBack ( )
112+ if ( specificCallBack ) {
113+ if ( specificCallBack . then || returnStringType ( specificCallBack ) === '[object AsyncFunction]' ) {
114+ await specificCallBack ( ) ;
108115 } else {
109- specificCallBack ( )
116+ specificCallBack ( ) ;
110117 }
111118 }
112119
113- if ( reformatCommand && commandToRerun ) {
114- commandToRerun = reformatCommand ( commandToRerun , executionStack , failedByAssert )
120+ if ( reformatCommand && commandToRerun ) {
121+ commandToRerun = reformatCommand ( commandToRerun , executionStack , failedByAssert ) ;
115122 }
116123 // addSpecificOptionsBeforeRun was defined - we should remove useless opts what will be added in next iteration
117- if ( additionalOpts ) {
118- commandToRerun = commandToRerun . replace ( additionalOpts , '' )
124+ if ( commandToRerun && additionalOpts ) {
125+ commandToRerun = commandToRerun . replace ( additionalOpts , '' ) ;
119126 }
120127
121- resolve ( commandToRerun )
128+ resolve ( commandToRerun ) ;
122129 } )
123- } )
124- return executeCommandAsync
130+ } ) ;
125131}
126132
127133module . exports = {
128134 buildExecRunner
129- }
130-
131-
132- const a = `IT_TITLE:[Upload product offer]
133- E/launcher - Process exited with error code 1
134- `
135+ } ;
0 commit comments