2022-01-09 03:49:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-10 11:39:55 +08:00
|
|
|
namespace Kiri\Server;
|
2022-01-09 03:49:02 +08:00
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Abstracts\Component;
|
|
|
|
|
use Kiri\Abstracts\Config;
|
2022-06-16 17:49:06 +08:00
|
|
|
use Kiri\Server\Abstracts\TraitServer;
|
2022-01-09 03:49:02 +08:00
|
|
|
use Swoole\Process;
|
|
|
|
|
|
|
|
|
|
class State extends Component
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
use TraitServer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public array $servers = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->servers = Config::get('server.ports');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function isRunner(): bool
|
|
|
|
|
{
|
|
|
|
|
$ports = $this->sortService($this->servers);
|
|
|
|
|
foreach ($ports as $config) {
|
|
|
|
|
if (checkPortIsAlready($config['port'])) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $port
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function exit($port)
|
|
|
|
|
{
|
|
|
|
|
if (!($pid = checkPortIsAlready($port))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (checkPortIsAlready($port)) {
|
2022-07-11 16:31:57 +08:00
|
|
|
Process::kill($pid, 0) && Process::kill($pid, SIGTERM);
|
2022-01-09 03:49:02 +08:00
|
|
|
usleep(300);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|