Skip to content

Commit 3ce13df

Browse files
committed
update: update some proc task logic
1 parent 20e6894 commit 3ce13df

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ProcTasks::new(['procNum' => 2])
3737
->onWorkersCreated(fn($pid, $info) => println("master [PID:$pid] - All task process started,", 'Workers info', $info))
3838
->onWorkerStart(fn($pid, $id) => println("worker#$id started, pid is", $pid))
3939
->onWorkerExit(fn($pid, $id) => println("worker#$id exited, pid is", $pid))
40-
->onWorkersExited(fn($pid) => println("master [PID:$pid] - all workers exited, tasks run completed"))
40+
->onCompleted(fn($pid) => println("master [PID:$pid] - all workers exited, tasks run completed"))
4141
->setTasks([
4242
['task1'], // one task
4343
['task2'],

Diff for: example/proc-tasks.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// run: php example/proc-tasks.php
88
// ProcTasks::new() // will auto get cpu num as proc num
99
ProcTasks::new(['procNum' => 3])
10+
->setProcName("procTasks")
1011
->setTaskHandler(function (array $task, array $ctx) {
1112
$pid = $ctx['pid'];
1213
println("worker#{$ctx['id']} [PID:$pid] - handle task, task data", $task);
@@ -16,7 +17,7 @@
1617
->onWorkersCreated(fn($pid, $info) => println("master [PID:$pid] - All task process started,", 'Workers info', $info))
1718
->onWorkerStart(fn($pid, $id) => println("worker#$id started, pid is", $pid))
1819
->onWorkerExit(fn($pid, $id) => println("worker#$id exited, pid is", $pid))
19-
->onWorkersExited(fn($pid) => println("master [PID:$pid] - all workers exited, tasks run completed"))
20+
->onCompleted(fn($pid) => println("master [PID:$pid] - all workers exited, tasks run completed"))
2021
->setTasks([
2122
['task1'], // one task
2223
['task2'],

Diff for: src/Cmd/AbstractCmdBuilder.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Toolkit\Sys\Cmd;
1111

1212
use RuntimeException;
13+
use Toolkit\Stdlib\Helper\Assert;
1314
use Toolkit\Sys\Exec;
1415
use function trim;
1516

@@ -239,6 +240,7 @@ public function getWorkDir(): string
239240
*/
240241
public function setWorkDir(string $workDir): static
241242
{
243+
Assert::isDir($workDir, "workdir is not exists. path: $workDir");
242244
$this->workDir = $workDir;
243245
return $this;
244246
}

Diff for: src/Proc/ProcTasks.php

+55-21
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Toolkit\Sys\Proc;
44

5+
use Exception;
56
use JetBrains\PhpStorm\NoReturn;
7+
use Throwable;
68
use Toolkit\Stdlib\Helper\Assert;
79
use Toolkit\Stdlib\Num;
810
use Toolkit\Stdlib\Obj\Traits\AutoConfigTrait;
911
use Toolkit\Sys\Sys;
1012
use function array_chunk;
1113
use function count;
14+
use function println;
1215
use 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

Comments
 (0)