Files
kiri-http-server/Abstracts/BaseProcess.php
T

144 lines
1.8 KiB
PHP
Raw Normal View History

2021-11-03 15:17:52 +08:00
<?php
namespace Server\Abstracts;
use JetBrains\PhpStorm\Pure;
use Server\SInterface\OnProcessInterface;
use Swoole\Coroutine;
use Swoole\Process;
/**
*
*/
abstract class BaseProcess implements OnProcessInterface
{
2021-11-03 17:39:08 +08:00
protected bool $isStop = false;
2021-11-03 15:17:52 +08:00
2021-11-03 17:39:08 +08:00
protected mixed $redirect_stdin_and_stdout = null;
protected int $pipe_type = SOCK_DGRAM;
protected bool $enable_coroutine = true;
protected string $name = 'swoole process.';
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return bool
*/
public function isStop(): bool
{
return $this->isStop;
}
/**
* @return mixed
*/
public function getRedirectStdinAndStdout(): mixed
{
return $this->redirect_stdin_and_stdout;
}
/**
* @return int
*/
public function getPipeType(): int
{
return $this->pipe_type;
}
/**
* @return bool
*/
public function isEnableCoroutine(): bool
{
return $this->enable_coroutine;
}
2021-11-03 15:17:52 +08:00
/**
*
*/
public function onProcessStop(): void
{
$this->isStop = true;
}
/**
* @return bool
*/
public function checkProcessIsStop(): bool
{
return $this->isStop === true;
}
/**
* @param Process $process
*/
public function signListen(Process $process): void
{
}
/**
*
*/
protected function exit(): void
{
putenv('process.status=idle');
}
/**
* @return bool
*/
#[Pure] public function isWorking(): bool
{
return env('process.status', 'working') == 'working';
}
/**
*
*/
private function waiteExit(Process $process): void
{
$this->onProcessStop();
while ($this->isWorking()) {
$this->sleep();
}
$process->exit(0);
}
/**
*
*/
private function sleep(): void
{
2021-11-03 17:39:08 +08:00
if ($this->enable_coroutine) {
2021-11-03 15:17:52 +08:00
Coroutine::sleep(0.1);
} else {
usleep(100);
}
}
}