@@ -26,17 +26,17 @@ class ProcessUtil
2626 /**
2727 * @return bool
2828 */
29- public static function isSupported (): bool
29+ public static function pcntlIsEnabled (): bool
3030 {
3131 return !Helper::isWindows () && \function_exists ('pcntl_fork ' );
3232 }
3333
3434 /**
3535 * @return bool
3636 */
37- public static function signalIsEnabled (): bool
37+ public static function posixIsEnabled (): bool
3838 {
39- return self :: isSupported ( );
39+ return !Helper:: isWindows () && \function_exists ( ' posix_kill ' );
4040 }
4141
4242 /**
@@ -72,7 +72,7 @@ public static function run(string $command, string $cwd = null): array
7272 // TODO: Write passphrase in pipes[3].
7373 fclose ($ pipes [3 ]);
7474
75- // Close all pipes before proc_close!
75+ // Close all pipes before proc_close! $code === 0 is success.
7676 $ code = proc_close ($ process );
7777
7878 return [$ code , $ output , $ error ];
@@ -85,7 +85,7 @@ public static function run(string $command, string $cwd = null): array
8585 */
8686 public static function daemonRun (\Closure $ beforeQuit = null ): int
8787 {
88- if (!self ::isSupported ()) {
88+ if (!self ::pcntlIsEnabled ()) {
8989 return 0 ;
9090 }
9191
@@ -160,7 +160,7 @@ public static function forks(int $number, callable $onStart = null, callable $on
160160 return false ;
161161 }
162162
163- if (!self ::isSupported ()) {
163+ if (!self ::pcntlIsEnabled ()) {
164164 return false ;
165165 }
166166
@@ -195,7 +195,7 @@ public static function create(callable $onStart = null, callable $onError = null
195195 */
196196 public static function fork (callable $ onStart = null , callable $ onError = null , $ id = 0 )
197197 {
198- if (!self ::isSupported ()) {
198+ if (!self ::pcntlIsEnabled ()) {
199199 return false ;
200200 }
201201
@@ -233,7 +233,7 @@ public static function fork(callable $onStart = null, callable $onError = null,
233233 */
234234 public static function wait (callable $ onExit ): bool
235235 {
236- if (!self ::isSupported ()) {
236+ if (!self ::pcntlIsEnabled ()) {
237237 return false ;
238238 }
239239
@@ -281,7 +281,7 @@ public static function stopWorkers(array $children, int $signal = SIGTERM, array
281281 return false ;
282282 }
283283
284- if (!self ::isSupported ()) {
284+ if (!self ::pcntlIsEnabled ()) {
285285 return false ;
286286 }
287287
@@ -317,27 +317,26 @@ public static function stopWorkers(array $children, int $signal = SIGTERM, array
317317 * @param int $timeout
318318 * @return bool
319319 */
320- public static function kill (int $ pid , $ force = false , int $ timeout = 3 ): bool
320+ public static function kill (int $ pid , bool $ force = false , int $ timeout = 3 ): bool
321321 {
322322 return self ::sendSignal ($ pid , $ force ? SIGKILL : SIGTERM , $ timeout );
323323 }
324324
325325 /**
326326 * Do shutdown process and wait it exit.
327- * @param int $pid Master Pid
328- * @param int $signal
327+ * @param int $pid Master Pid
328+ * @param bool $force
329329 * @param int $waitTime
330330 * @param null $error
331331 * @param string $name
332332 * @return bool
333333 */
334334 public static function killAndWait (
335- int $ pid , int $ signal = SIGTERM , int $ waitTime = 10 , & $ error = null , $ name = ' process '
335+ int $ pid , & $ error = null , $ name = ' process ' , bool $ force = false , int $ waitTime = 10
336336 ): bool
337337 {
338- // $opts = array_merge([], $opts);
339338 // do stop
340- if (!self ::kill ($ signal )) {
339+ if (!self ::kill ($ pid , $ force )) {
341340 $ error = "Send stop signal to the $ name(PID: $ pid) failed! " ;
342341
343342 return false ;
@@ -372,7 +371,7 @@ public static function killAndWait(
372371 return false ;
373372 }
374373
375- Show::write (' OK ' );
374+ Show::color (' OK ' );
376375 return true ;
377376 }
378377
@@ -421,7 +420,7 @@ public static function quit($code = 0)
421420 */
422421 public static function sendSignal (int $ pid , int $ signal , int $ timeout = 0 ): bool
423422 {
424- if ($ pid <= 0 || !self ::isSupported ()) {
423+ if ($ pid <= 0 || !self ::posixIsEnabled ()) {
425424 return false ;
426425 }
427426
@@ -467,6 +466,10 @@ public static function sendSignal(int $pid, int $signal, int $timeout = 0): bool
467466 */
468467 public static function installSignal ($ signal , callable $ handler ): bool
469468 {
469+ if (!self ::pcntlIsEnabled ()) {
470+ return false ;
471+ }
472+
470473 return pcntl_signal ($ signal , $ handler , false );
471474 }
472475
@@ -476,6 +479,10 @@ public static function installSignal($signal, callable $handler): bool
476479 */
477480 public static function dispatchSignal (): bool
478481 {
482+ if (!self ::pcntlIsEnabled ()) {
483+ return false ;
484+ }
485+
479486 // receive and dispatch sig
480487 return pcntl_signal_dispatch ();
481488 }
@@ -527,9 +534,14 @@ public static function getCurrentUser(): array
527534 /**
528535 * @param int $seconds
529536 * @param callable $handler
537+ * @return bool|int
530538 */
531539 public static function afterDo (int $ seconds , callable $ handler )
532540 {
541+ if (!self ::pcntlIsEnabled ()) {
542+ return false ;
543+ }
544+
533545 /* self::signal(SIGALRM, function () {
534546 static $i = 0;
535547 echo "#{$i}\talarm\n";
@@ -541,7 +553,7 @@ public static function afterDo(int $seconds, callable $handler)
541553 self ::installSignal (SIGALRM , $ handler );
542554
543555 // self::alarm($seconds);
544- pcntl_alarm ($ seconds );
556+ return pcntl_alarm ($ seconds );
545557 }
546558
547559 /**
0 commit comments