22
33namespace Toolkit \Sys \Proc ;
44
5+ use Exception ;
56use JetBrains \PhpStorm \NoReturn ;
7+ use Throwable ;
68use Toolkit \Stdlib \Helper \Assert ;
79use Toolkit \Stdlib \Num ;
810use Toolkit \Stdlib \Obj \Traits \AutoConfigTrait ;
911use Toolkit \Sys \Sys ;
1012use function array_chunk ;
1113use function count ;
14+ use function println ;
1215use function time ;
1316
1417/**
@@ -40,12 +43,14 @@ class ProcTasks
4043 private $ taskHandler ;
4144
4245 /**
43- * hooks on error
44- * - param#1 is PID.
46+ * hooks on run task error.
47+ * - param#1 is Exception.
48+ * - param#2 is PID.
49+ * - return bool. TRUE - stop handle task, FALSE - continue handle next.
4550 *
46- * @var callable(int): void
51+ * @var callable(Exception, int): bool
4752 */
48- private $ onError ;
53+ private $ errorHandler ;
4954
5055 /**
5156 * hooks on before create workers, in parent.
@@ -297,13 +302,38 @@ protected function doRunTasks(array $tasks, int $pid, int $id): void
297302 // exec task handler
298303 $ handler = $ this ->taskHandler ;
299304 foreach ($ tasks as $ task ) {
300- $ handler ($ task , $ info );
305+ try {
306+ $ handler ($ task , $ info );
307+ } catch (Throwable $ e ) {
308+ if ($ this ->handleTaskError ($ e , $ pid )) {
309+ break ;
310+ }
311+ }
301312 }
302313
303314 // exit worker
304315 exit (0 );
305316 }
306317
318+ /**
319+ * @param Throwable $e
320+ * @param int $pid
321+ *
322+ * @return bool TRUE - stop continue handle. FALSE - continue next.
323+ */
324+ protected function handleTaskError (Throwable $ e , int $ pid ): bool
325+ {
326+ // call hooks
327+ if ($ fn = $ this ->errorHandler ) {
328+ $ stop = $ fn ($ e , $ pid );
329+
330+ return (bool )$ stop ;
331+ }
332+
333+ println ("[PID: $ pid] ERROR: handle task - " , $ e ->getMessage ());
334+ return false ;
335+ }
336+
307337 /**
308338 * In parent.
309339 *
@@ -328,7 +358,6 @@ protected function handleWorkerExit(int $pid, int $exitCode, int $status): void
328358
329359 $ fn ($ pid , $ info ['id ' ], $ info );
330360 }
331-
332361 }
333362
334363 /**
@@ -384,15 +413,15 @@ public function onWorkerExit(callable $listener): self
384413 }
385414
386415 /**
387- * On all worker process exited, in parent.
416+ * On run task error
388417 *
389- * @param callable $listener
418+ * @param callable(Exception, int): void $errorHandler
390419 *
391- * @return $this
420+ * @return ProcTasks
392421 */
393- public function onCompleted (callable $ listener ): self
422+ public function onTaskError (callable $ errorHandler ): self
394423 {
395- $ this ->workersExitedFn = $ listener ;
424+ $ this ->errorHandler = $ errorHandler ;
396425 return $ this ;
397426 }
398427
@@ -403,7 +432,7 @@ public function onCompleted(callable $listener): self
403432 *
404433 * @return $this
405434 */
406- public function onWorkersExited (callable $ listener ): self
435+ public function onCompleted (callable $ listener ): self
407436 {
408437 $ this ->workersExitedFn = $ listener ;
409438 return $ this ;
@@ -417,6 +446,14 @@ public function getProcNum(): int
417446 return $ this ->procNum ;
418447 }
419448
449+ /**
450+ * @return int
451+ */
452+ public function getTaskNum (): int
453+ {
454+ return count ($ this ->tasks );
455+ }
456+
420457 /**
421458 * @param int $procNum
422459 *
@@ -468,21 +505,18 @@ public function setProcName(string $procName): self
468505 }
469506
470507 /**
471- * @param callable $onError
472- *
473- * @return ProcTasks
508+ * @return array
474509 */
475- public function setOnError ( callable $ onError ): self
510+ public function getProcesses ( ): array
476511 {
477- $ this ->onError = $ onError ;
478- return $ this ;
512+ return $ this ->processes ;
479513 }
480514
481515 /**
482- * @return array
516+ * @return int
483517 */
484- public function getProcesses (): array
518+ public function getMasterPid (): int
485519 {
486- return $ this ->processes ;
520+ return $ this ->masterPid ;
487521 }
488522}
0 commit comments