2
2
3
3
namespace Toolkit \Sys \Proc ;
4
4
5
+ use Exception ;
5
6
use JetBrains \PhpStorm \NoReturn ;
7
+ use Throwable ;
6
8
use Toolkit \Stdlib \Helper \Assert ;
7
9
use Toolkit \Stdlib \Num ;
8
10
use Toolkit \Stdlib \Obj \Traits \AutoConfigTrait ;
9
11
use Toolkit \Sys \Sys ;
10
12
use function array_chunk ;
11
13
use function count ;
14
+ use function println ;
12
15
use function time ;
13
16
14
17
/**
@@ -40,12 +43,14 @@ class ProcTasks
40
43
private $ taskHandler ;
41
44
42
45
/**
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.
45
50
*
46
- * @var callable(int): void
51
+ * @var callable(Exception, int): bool
47
52
*/
48
- private $ onError ;
53
+ private $ errorHandler ;
49
54
50
55
/**
51
56
* hooks on before create workers, in parent.
@@ -297,13 +302,38 @@ protected function doRunTasks(array $tasks, int $pid, int $id): void
297
302
// exec task handler
298
303
$ handler = $ this ->taskHandler ;
299
304
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
+ }
301
312
}
302
313
303
314
// exit worker
304
315
exit (0 );
305
316
}
306
317
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
+
307
337
/**
308
338
* In parent.
309
339
*
@@ -328,7 +358,6 @@ protected function handleWorkerExit(int $pid, int $exitCode, int $status): void
328
358
329
359
$ fn ($ pid , $ info ['id ' ], $ info );
330
360
}
331
-
332
361
}
333
362
334
363
/**
@@ -384,15 +413,15 @@ public function onWorkerExit(callable $listener): self
384
413
}
385
414
386
415
/**
387
- * On all worker process exited, in parent.
416
+ * On run task error
388
417
*
389
- * @param callable $listener
418
+ * @param callable(Exception, int): void $errorHandler
390
419
*
391
- * @return $this
420
+ * @return ProcTasks
392
421
*/
393
- public function onCompleted (callable $ listener ): self
422
+ public function onTaskError (callable $ errorHandler ): self
394
423
{
395
- $ this ->workersExitedFn = $ listener ;
424
+ $ this ->errorHandler = $ errorHandler ;
396
425
return $ this ;
397
426
}
398
427
@@ -403,7 +432,7 @@ public function onCompleted(callable $listener): self
403
432
*
404
433
* @return $this
405
434
*/
406
- public function onWorkersExited (callable $ listener ): self
435
+ public function onCompleted (callable $ listener ): self
407
436
{
408
437
$ this ->workersExitedFn = $ listener ;
409
438
return $ this ;
@@ -417,6 +446,14 @@ public function getProcNum(): int
417
446
return $ this ->procNum ;
418
447
}
419
448
449
+ /**
450
+ * @return int
451
+ */
452
+ public function getTaskNum (): int
453
+ {
454
+ return count ($ this ->tasks );
455
+ }
456
+
420
457
/**
421
458
* @param int $procNum
422
459
*
@@ -468,21 +505,18 @@ public function setProcName(string $procName): self
468
505
}
469
506
470
507
/**
471
- * @param callable $onError
472
- *
473
- * @return ProcTasks
508
+ * @return array
474
509
*/
475
- public function setOnError ( callable $ onError ): self
510
+ public function getProcesses ( ): array
476
511
{
477
- $ this ->onError = $ onError ;
478
- return $ this ;
512
+ return $ this ->processes ;
479
513
}
480
514
481
515
/**
482
- * @return array
516
+ * @return int
483
517
*/
484
- public function getProcesses (): array
518
+ public function getMasterPid (): int
485
519
{
486
- return $ this ->processes ;
520
+ return $ this ->masterPid ;
487
521
}
488
522
}
0 commit comments