@@ -9,6 +9,7 @@ class ProcessManager
99 private string $ binaryPath ;
1010
1111 private $ currentProcess = null ;
12+ private mixed $ pipes ;
1213
1314 public function __construct (string $ binaryPath )
1415 {
@@ -20,19 +21,41 @@ public function __construct(string $binaryPath)
2021 }
2122 }
2223
23- private function handleSignal (int $ signal ): void
24+ public function handleSignal (int $ signal ): void
2425 {
2526 if ($ this ->currentProcess && is_resource ($ this ->currentProcess )) {
26- proc_terminate ($ this ->currentProcess );
27+ // Send SIGTERM to the process
28+ proc_terminate ($ this ->currentProcess , SIGTERM );
29+
30+ // Wait a bit for it to exit gracefully
31+ sleep (1 );
32+
33+ // Capture final output before closing the process
34+ $ output = '' ;
35+ if ($ this ->pipes && isset ($ this ->pipes [1 ]) && is_resource ($ this ->pipes [1 ])) {
36+ stream_set_blocking ($ this ->pipes [1 ], false ); // Ensure non-blocking mode
37+ $ output = stream_get_contents ($ this ->pipes [1 ]);
38+ fclose ($ this ->pipes [1 ]);
39+ }
40+
41+ // Ensure the process is closed
2742 proc_close ($ this ->currentProcess );
43+ $ this ->currentProcess = null ;
44+
45+ // Print the final output
46+ if (!empty ($ output )) {
47+ echo "\n$ output \n" ;
48+ }
2849 }
50+
2951 exit (0 );
3052 }
3153
3254 public function execute (array $ config , bool $ streamOutput ): string
3355 {
3456 [$ success , $ process , $ pipes ] = $ this ->openProcess ();
3557 $ this ->currentProcess = $ process ;
58+ $ this ->pipes = $ pipes ;
3659
3760 if (! $ success || ! is_array ($ pipes )) {
3861 throw new RuntimeException ('Failed to start process of volt test ' );
@@ -109,6 +132,10 @@ private function handleProcess(array $pipes, bool $streamOutput): string
109132 {
110133 $ output = '' ;
111134
135+ // Set non-blocking mode for stdout and stderr
136+ stream_set_blocking ($ pipes [1 ], false );
137+ stream_set_blocking ($ pipes [2 ], false );
138+
112139 while (true ) {
113140 $ read = array_filter ($ pipes , 'is_resource ' );
114141 if (empty ($ read )) {
@@ -130,7 +157,6 @@ private function handleProcess(array $pipes, bool $streamOutput): string
130157 if (feof ($ pipe )) {
131158 fclose ($ pipe );
132159 unset($ pipes [$ type ]);
133-
134160 continue ;
135161 }
136162 }
0 commit comments