Skip to content

Commit ef8d1b6

Browse files
committed
up: fix some error, update readme
1 parent e9f7056 commit ef8d1b6

File tree

6 files changed

+131
-34
lines changed

6 files changed

+131
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# System Utils
22

33
[![License](https://img.shields.io/packagist/l/toolkit/sys-utils.svg?style=flat-square)](LICENSE)
4-
[![Php Version](https://img.shields.io/badge/php-%3E7.1.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/sys-utils)
4+
[![Php Version](https://img.shields.io/badge/php-%3E8.0.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/sys-utils)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/sys-utils.svg)](https://packagist.org/packages/toolkit/sys-utils)
66

77
Some useful system utils for php

composer.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
{
1515
"name": "inhere",
1616
"email": "[email protected]",
17-
"homepage": "http://www.yzone.net/"
17+
"homepage": "https://github.com/inhere"
1818
}
1919
],
2020
"require": {
21-
"php": ">7.1.0",
22-
"toolkit/stdlib": "~1.0"
21+
"php": ">8.0.0",
22+
"toolkit/stdlib": "~2.0"
2323
},
2424
"autoload": {
2525
"psr-4": {
2626
"Toolkit\\Sys\\": "src/"
2727
}
2828
},
2929
"suggest": {
30-
"inhere/php-validate": "Very lightweight data validate tool",
30+
"toolkit/cli-utils": "useful cli tool library of the php",
3131
"inhere/console": "a lightweight php console application library."
3232
}
3333
}

src/Exec.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ public static function run(string $command, string $cwd = ''): array
150150
* 3. exec
151151
* 4. shell_exec
152152
*
153-
* @param string $command
154-
* @param bool $returnStatus
155-
* @param string|null $cwd
153+
* @param string $command
154+
* @param bool $returnStatus
155+
* @param string $cwd
156156
*
157157
* @return array|string
158158
*/

src/Proc/ProcManager.php

+109-7
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,88 @@ class ProcManager
1414
{
1515
use AutoConfigTrait;
1616

17+
public const ON_WORKER_START = 'workerStart';
18+
public const ON_WORKER_STOP = 'workerStop';
19+
public const ON_WORKER_ERROR = 'workerError';
20+
public const ON_WORKER_EXIT = 'workerExit';
21+
22+
public const BEFORE_START_WORKERS = 'beforeStartWorkers';
23+
24+
public const AFTER_START_WORKERS = 'afterStartWorkers';
25+
26+
/**
27+
* hooks on sub-process started.
28+
* - param#1 is PID. param#2 is index.
29+
*
30+
* @var callable(int, int): void
31+
*/
32+
private $workHandler;
33+
1734
/**
18-
* @var callable
35+
* hooks on error
36+
* - param#1 is PID.
37+
*
38+
* @var callable(int): void
1939
*/
20-
private $handler;
40+
private $onError;
41+
42+
/**
43+
* hooks on child exit
44+
* - param#1 is PID.
45+
*
46+
* @var callable(int): void
47+
*/
48+
private $onExit;
2149

2250
private int $procNum = 1;
2351

52+
/**
53+
* @var bool
54+
*/
55+
private bool $daemon = false;
56+
2457
/**
2558
* @var string custom process name
2659
*/
2760
private string $procName = '';
2861

62+
/**
63+
* @return $this
64+
*/
2965
public function run(): self
3066
{
31-
Assert::notNull($this->handler, 'process: logic handler must be set before run');
32-
$handler = $this->handler;
67+
$handler = $this->workHandler;
68+
Assert::notNull($handler, 'process: logic handler must be set before run');
69+
70+
try {
71+
// set process name.
72+
if ($this->procName) {
73+
ProcessUtil::setTitle($this->procName);
74+
}
75+
76+
// create processes
77+
$pidInfo = ProcessUtil::forks($this->procNum, $handler, $this->onError);
78+
79+
// in parent process.
80+
Assert::notEmpty($pidInfo, 'create process failed');
3381

82+
// wait child exit.
83+
ProcessUtil::wait($this->onExit);
84+
} catch (\Throwable $e) {
85+
86+
}
3487

3588
return $this;
3689
}
3790

3891
/**
39-
* @param callable $handler
92+
* @param callable $workHandler
4093
*
4194
* @return $this
4295
*/
43-
public function setHandler(callable $handler): self
96+
public function setWorkHandler(callable $workHandler): self
4497
{
45-
$this->handler = $handler;
98+
$this->workHandler = $workHandler;
4699
return $this;
47100
}
48101

@@ -65,4 +118,53 @@ public function setProcNum(int $procNum): self
65118
$this->procNum = $procNum;
66119
return $this;
67120
}
121+
122+
/**
123+
* @return bool
124+
*/
125+
public function isDaemon(): bool
126+
{
127+
return $this->daemon;
128+
}
129+
130+
/**
131+
* @param bool $daemon
132+
*
133+
* @return ProcManager
134+
*/
135+
public function setDaemon(bool $daemon): self
136+
{
137+
$this->daemon = $daemon;
138+
return $this;
139+
}
140+
141+
/**
142+
* @return string
143+
*/
144+
public function getProcName(): string
145+
{
146+
return $this->procName;
147+
}
148+
149+
/**
150+
* @param string $procName
151+
*
152+
* @return ProcManager
153+
*/
154+
public function setProcName(string $procName): self
155+
{
156+
$this->procName = $procName;
157+
return $this;
158+
}
159+
160+
/**
161+
* @param callable $onError
162+
*
163+
* @return ProcManager
164+
*/
165+
public function setOnError(callable $onError): self
166+
{
167+
$this->onError = $onError;
168+
return $this;
169+
}
68170
}

src/Proc/ProcWrapper.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class ProcWrapper
3333
private string $command;
3434

3535
/**
36-
* @var string|null
36+
* @var string
3737
*/
38-
private ?string $workDir;
38+
private string $workDir = '';
3939

4040
/**
4141
* @var array
@@ -54,7 +54,7 @@ class ProcWrapper
5454
*
5555
* @var array|null
5656
*/
57-
private ?array $runENV;
57+
private ?array $runENV = null;
5858

5959
/**
6060
* @var array
@@ -421,7 +421,7 @@ public function setCommand(string $command): self
421421
}
422422

423423
/**
424-
* @param index $index
424+
* @param int $index
425425
* @param array $spec
426426
*
427427
* @return ProcWrapper

src/Proc/ProcessUtil.php

+10-15
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ public static function isPtySupported(): bool
105105
/**
106106
* fork/create a child process.
107107
*
108-
* @param callable|null $onStart Will running on the child process start.
109-
* @param callable|null $onError
108+
* @param null|callable(int, int):void $onStart Will running on the child process start.
109+
* @param null|callable(int):void $onError
110110
* @param int $id The process index number. will use `forks()`
111111
*
112-
* @return array|false
112+
* @return array{id: int, pid: int, startTime: int}
113113
* @throws RuntimeException
114114
*/
115-
public static function fork(callable $onStart = null, callable $onError = null, int $id = 0): bool|array
115+
public static function fork(callable $onStart = null, callable $onError = null, int $id = 0): array
116116
{
117117
if (!self::hasPcntl()) {
118-
return false;
118+
return [];
119119
}
120120

121121
$info = [];
@@ -178,7 +178,6 @@ public static function daemonRun(Closure $beforeQuit = null): int
178178
switch ($pid) {
179179
case 0: // at new process
180180
$pid = self::getPid();
181-
182181
if (posix_setsid() < 0) {
183182
throw new RuntimeException('posix_setsid() execute failed! exiting');
184183
}
@@ -219,17 +218,13 @@ public static function multi(int $number, callable $onStart = null, callable $on
219218
* @param callable|null $onStart Will running on the child processes.
220219
* @param callable|null $onError
221220
*
222-
* @return array|false
221+
* @return array<int, array{id: int, pid: int, startTime: int}>
223222
* @throws RuntimeException
224223
*/
225-
public static function forks(int $number, callable $onStart = null, callable $onError = null): bool|array
224+
public static function forks(int $number, callable $onStart = null, callable $onError = null): array
226225
{
227-
if ($number <= 0) {
228-
return false;
229-
}
230-
231-
if (!self::hasPcntl()) {
232-
return false;
226+
if ($number <= 0 || !self::hasPcntl()) {
227+
return [];
233228
}
234229

235230
$pidAry = [];
@@ -672,7 +667,7 @@ public static function setTitle(string $title): bool
672667
throw new RuntimeException($error['message']);
673668
}
674669

675-
return false;
670+
return true;
676671
}
677672

678673
/**

0 commit comments

Comments
 (0)