Skip to content

Commit

Permalink
up: fix some error, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 2, 2021
1 parent e9f7056 commit ef8d1b6
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# System Utils

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

Some useful system utils for php
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
{
"name": "inhere",
"email": "in.798@qq.com",
"homepage": "http://www.yzone.net/"
"homepage": "https://github.com/inhere"
}
],
"require": {
"php": ">7.1.0",
"toolkit/stdlib": "~1.0"
"php": ">8.0.0",
"toolkit/stdlib": "~2.0"
},
"autoload": {
"psr-4": {
"Toolkit\\Sys\\": "src/"
}
},
"suggest": {
"inhere/php-validate": "Very lightweight data validate tool",
"toolkit/cli-utils": "useful cli tool library of the php",
"inhere/console": "a lightweight php console application library."
}
}
6 changes: 3 additions & 3 deletions src/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public static function run(string $command, string $cwd = ''): array
* 3. exec
* 4. shell_exec
*
* @param string $command
* @param bool $returnStatus
* @param string|null $cwd
* @param string $command
* @param bool $returnStatus
* @param string $cwd
*
* @return array|string
*/
Expand Down
116 changes: 109 additions & 7 deletions src/Proc/ProcManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,88 @@ class ProcManager
{
use AutoConfigTrait;

public const ON_WORKER_START = 'workerStart';
public const ON_WORKER_STOP = 'workerStop';
public const ON_WORKER_ERROR = 'workerError';
public const ON_WORKER_EXIT = 'workerExit';

public const BEFORE_START_WORKERS = 'beforeStartWorkers';

public const AFTER_START_WORKERS = 'afterStartWorkers';

/**
* hooks on sub-process started.
* - param#1 is PID. param#2 is index.
*
* @var callable(int, int): void
*/
private $workHandler;

/**
* @var callable
* hooks on error
* - param#1 is PID.
*
* @var callable(int): void
*/
private $handler;
private $onError;

/**
* hooks on child exit
* - param#1 is PID.
*
* @var callable(int): void
*/
private $onExit;

private int $procNum = 1;

/**
* @var bool
*/
private bool $daemon = false;

/**
* @var string custom process name
*/
private string $procName = '';

/**
* @return $this
*/
public function run(): self
{
Assert::notNull($this->handler, 'process: logic handler must be set before run');
$handler = $this->handler;
$handler = $this->workHandler;
Assert::notNull($handler, 'process: logic handler must be set before run');

try {
// set process name.
if ($this->procName) {
ProcessUtil::setTitle($this->procName);
}

// create processes
$pidInfo = ProcessUtil::forks($this->procNum, $handler, $this->onError);

// in parent process.
Assert::notEmpty($pidInfo, 'create process failed');

// wait child exit.
ProcessUtil::wait($this->onExit);
} catch (\Throwable $e) {

}

return $this;
}

/**
* @param callable $handler
* @param callable $workHandler
*
* @return $this
*/
public function setHandler(callable $handler): self
public function setWorkHandler(callable $workHandler): self
{
$this->handler = $handler;
$this->workHandler = $workHandler;
return $this;
}

Expand All @@ -65,4 +118,53 @@ public function setProcNum(int $procNum): self
$this->procNum = $procNum;
return $this;
}

/**
* @return bool
*/
public function isDaemon(): bool
{
return $this->daemon;
}

/**
* @param bool $daemon
*
* @return ProcManager
*/
public function setDaemon(bool $daemon): self
{
$this->daemon = $daemon;
return $this;
}

/**
* @return string
*/
public function getProcName(): string
{
return $this->procName;
}

/**
* @param string $procName
*
* @return ProcManager
*/
public function setProcName(string $procName): self
{
$this->procName = $procName;
return $this;
}

/**
* @param callable $onError
*
* @return ProcManager
*/
public function setOnError(callable $onError): self
{
$this->onError = $onError;
return $this;
}
}
8 changes: 4 additions & 4 deletions src/Proc/ProcWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class ProcWrapper
private string $command;

/**
* @var string|null
* @var string
*/
private ?string $workDir;
private string $workDir = '';

/**
* @var array
Expand All @@ -54,7 +54,7 @@ class ProcWrapper
*
* @var array|null
*/
private ?array $runENV;
private ?array $runENV = null;

/**
* @var array
Expand Down Expand Up @@ -421,7 +421,7 @@ public function setCommand(string $command): self
}

/**
* @param index $index
* @param int $index
* @param array $spec
*
* @return ProcWrapper
Expand Down
25 changes: 10 additions & 15 deletions src/Proc/ProcessUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ public static function isPtySupported(): bool
/**
* fork/create a child process.
*
* @param callable|null $onStart Will running on the child process start.
* @param callable|null $onError
* @param null|callable(int, int):void $onStart Will running on the child process start.
* @param null|callable(int):void $onError
* @param int $id The process index number. will use `forks()`
*
* @return array|false
* @return array{id: int, pid: int, startTime: int}
* @throws RuntimeException
*/
public static function fork(callable $onStart = null, callable $onError = null, int $id = 0): bool|array
public static function fork(callable $onStart = null, callable $onError = null, int $id = 0): array
{
if (!self::hasPcntl()) {
return false;
return [];
}

$info = [];
Expand Down Expand Up @@ -178,7 +178,6 @@ public static function daemonRun(Closure $beforeQuit = null): int
switch ($pid) {
case 0: // at new process
$pid = self::getPid();

if (posix_setsid() < 0) {
throw new RuntimeException('posix_setsid() execute failed! exiting');
}
Expand Down Expand Up @@ -219,17 +218,13 @@ public static function multi(int $number, callable $onStart = null, callable $on
* @param callable|null $onStart Will running on the child processes.
* @param callable|null $onError
*
* @return array|false
* @return array<int, array{id: int, pid: int, startTime: int}>
* @throws RuntimeException
*/
public static function forks(int $number, callable $onStart = null, callable $onError = null): bool|array
public static function forks(int $number, callable $onStart = null, callable $onError = null): array
{
if ($number <= 0) {
return false;
}

if (!self::hasPcntl()) {
return false;
if ($number <= 0 || !self::hasPcntl()) {
return [];
}

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

return false;
return true;
}

/**
Expand Down

0 comments on commit ef8d1b6

Please sign in to comment.