@@ -33,7 +33,8 @@ public function execute(
3333 $ progressCallbackParams = null ,
3434 $ showProgress = false ,
3535 $ callbackTurns = 0 ,
36- $ logKey = null )
36+ $ logKey = null ,
37+ $ maxSeconds = 3600 )
3738 {
3839 if ($ logKey )
3940 $ this ->logKey = $ logKey ;
@@ -66,81 +67,89 @@ public function execute(
6667 $ allOut = "" ;
6768 $ allOutErr = "" ;
6869
69- // Check process status at every turn
70- do {
71- sleep ($ sleep );
70+ $ startTime = time ();
71+ try {
72+ // Check process status at every turn
73+ do {
74+ if ($ maxSeconds > 0 && (time () - $ startTime ) > $ maxSeconds ) {
75+ proc_terminate ($ process , 9 );
76+ throw new CpeSdk \CpeException (
77+ "Command timed out after {$ maxSeconds }s: $ cmd " ,
78+ self ::EXEC_FAILED
79+ );
80+ }
81+ sleep ($ sleep );
82+
83+ // If callback only after N turns
84+ if ( !$ callbackTurns || in_array ($ i , array (0 , $ callbackTurns )) )
85+ {
86+ if ($ showProgress ) {
87+ echo ". \n" ;
88+ }
89+
90+ // Call user provided callback.
91+ // Callback should be an array as per doc here:
92+ // http://www.php.net/manual/en/language.types.callable.php
93+ // Type 3: Object method call
94+ if (isset ($ progressCallback ) && $ progressCallback ) {
95+ call_user_func ($ progressCallback , $ progressCallbackParams ,
96+ $ allOut , $ allOutErr );
97+ }
98+
99+ $ i = 0 ;
100+ }
72101
73- // If callback only after N turns
74- if ( !$ callbackTurns || in_array ($ i , array (0 , $ callbackTurns )) )
75- {
102+ // Get latest status
103+ $ procStatus = proc_get_status ($ process );
76104 if ($ showProgress ) {
77- echo ". \n" ;
105+ echo ". " ;
106+ flush ();
78107 }
79108
80- // Call user provided callback.
81- // Callback should be an array as per doc here:
82- // http://www.php.net/manual/en/language.types.callable.php
83- // Type 3: Object method call
84- if (isset ($ progressCallback ) && $ progressCallback ) {
85- call_user_func ($ progressCallback , $ progressCallbackParams ,
86- $ allOut , $ allOutErr );
109+ // Read prog output
110+ if (isset ($ pipes [1 ]) && $ pipes [1 ]) {
111+ $ out = stream_get_contents ($ pipes [1 ], -1 );
112+ $ allOut .= $ out ;
87113 }
88114
89- $ i = 0 ;
90- }
91-
92- // Get latest status
93- $ procStatus = proc_get_status ($ process );
94- if ($ showProgress ) {
95- echo ". " ;
96- flush ();
97- }
98-
99- // Read prog output
100- if (isset ($ pipes [1 ]) && $ pipes [1 ]) {
101- $ out = stream_get_contents ($ pipes [1 ], -1 );
102- $ allOut .= $ out ;
103- }
104-
105- // Read prog errors
106- if (isset ($ pipes [2 ]) && $ pipes [2 ]) {
107- $ outErr = stream_get_contents ($ pipes [2 ], -1 );
108- $ allOutErr .= $ outErr ;
109- }
110-
111- $ i ++;
112- } while ($ procStatus ['running ' ]);
115+ // Read prog errors
116+ if (isset ($ pipes [2 ]) && $ pipes [2 ]) {
117+ $ outErr = stream_get_contents ($ pipes [2 ], -1 );
118+ $ allOutErr .= $ outErr ;
119+ }
113120
114- if (isset ($ pipes [1 ]))
115- fclose ($ pipes [1 ]);
116- if (isset ($ pipes [2 ]))
117- fclose ($ pipes [2 ]);
121+ $ i ++;
122+ } while ($ procStatus ['running ' ]);
118123
119- if ($ procStatus ['exitcode ' ] > 0 )
120- {
121- $ this ->cpeLogger ->logOut ("ERROR " ,
122- basename (__FILE__ ),
123- "Can't execute: $ cmd. Exit Code: " .$ procStatus ['exitcode ' ],
124- $ this ->logKey );
125- if ($ allOut ) {
124+ if ($ procStatus ['exitcode ' ] > 0 )
125+ {
126126 $ this ->cpeLogger ->logOut ("ERROR " ,
127- basename (__FILE__ ), "COMMAND STDOUT: " .$ allOut ,
127+ basename (__FILE__ ),
128+ "Can't execute: $ cmd. Exit Code: " .$ procStatus ['exitcode ' ],
128129 $ this ->logKey );
129- $ allOut = null ;
130+ if ($ allOut ) {
131+ $ this ->cpeLogger ->logOut ("ERROR " ,
132+ basename (__FILE__ ), "COMMAND STDOUT: " .$ allOut ,
133+ $ this ->logKey );
134+ $ allOut = null ;
135+ }
136+ if ($ allOutErr )
137+ $ this ->cpeLogger ->logOut ("ERROR " ,
138+ basename (__FILE__ ), "COMMAND STDERR: " .$ allOutErr ,
139+ $ this ->logKey );
130140 }
131- if ($ allOutErr )
132- $ this ->cpeLogger ->logOut ("ERROR " ,
133- basename (__FILE__ ), "COMMAND STDERR: " .$ allOutErr ,
134- $ this ->logKey );
135- }
136141
137- if ($ showProgress ) {
138- echo "\n" ;
142+ if ($ showProgress ) {
143+ echo "\n" ;
144+ }
145+ } finally {
146+ if (isset ($ pipes [1 ]) && is_resource ($ pipes [1 ]))
147+ fclose ($ pipes [1 ]);
148+ if (isset ($ pipes [2 ]) && is_resource ($ pipes [2 ]))
149+ fclose ($ pipes [2 ]);
150+ proc_close ($ process );
139151 }
140152
141- // Process is over
142- proc_close ($ process );
143-
144153 return array ('out ' => $ allOut , 'outErr ' => $ allOutErr );
145154 }
146155}
0 commit comments