Files
kiri-core/HttpServer/Action.php
T

171 lines
3.0 KiB
PHP
Raw Normal View History

2020-09-07 15:19:13 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-09-07 15:19:13 +08:00
namespace HttpServer;
use Exception;
2020-12-17 14:12:44 +08:00
2021-01-12 16:27:49 +08:00
use JetBrains\PhpStorm\Pure;
2021-03-01 15:09:36 +08:00
use Snowflake\Abstracts\Config;
2020-09-07 18:35:56 +08:00
use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException;
2021-03-01 15:09:36 +08:00
use Snowflake\Exception\ConfigException;
2020-09-07 15:19:13 +08:00
use Snowflake\Snowflake;
2021-03-02 12:08:21 +08:00
use Swoole\Coroutine;
2020-09-07 15:19:13 +08:00
use Swoole\WebSocket\Server;
/**
* Class Action
* @package HttpServer
*/
trait Action
{
/**
* @param \HttpServer\Server $socket
* @return mixed
* @throws Exception
*/
2021-01-11 11:10:09 +08:00
public function restart(\HttpServer\Server $socket): mixed
2020-09-07 15:19:13 +08:00
{
$this->_shutdown($socket);
return $this->start();
}
/**
* @param \HttpServer\Server $socket
* @throws Exception
*/
2020-10-09 16:34:33 +08:00
public function stop(\HttpServer\Server $socket)
2020-09-07 15:19:13 +08:00
{
$this->_shutdown($socket);
}
/**
* @param $server
* @return void
* @throws Exception
*/
private function _shutdown($server)
{
2021-03-01 15:36:35 +08:00
$pid_file = $this->getPidFile();
if (!file_exists($pid_file)) {
2021-03-01 15:35:17 +08:00
return;
}
2021-03-01 15:36:35 +08:00
$content = file_get_contents($pid_file);
2021-03-02 13:36:33 +08:00
exec("ps -ef $content | grep $content", $output);
2021-03-01 15:36:35 +08:00
if (!empty($output)) {
2021-03-02 13:36:33 +08:00
exec("kill -15 $content");
2020-09-07 15:19:13 +08:00
}
2021-03-09 19:47:48 +08:00
unset($content);
2021-03-01 15:36:35 +08:00
$this->close($server);
2021-03-01 15:07:11 +08:00
}
/**
* @return mixed
2021-03-01 15:09:36 +08:00
* @throws ConfigException
2021-03-01 15:07:11 +08:00
*/
private function getPidFile(): string
{
2021-03-01 15:09:36 +08:00
$settings = Config::get('settings', false, []);
if (!isset($settings['pid_file'])) {
return PID_PATH;
}
return $settings['pid_file'];
2020-09-07 15:19:13 +08:00
}
2020-09-07 18:56:00 +08:00
2020-09-07 15:19:13 +08:00
/**
* @param \HttpServer\Server $server
* @return void
* @throws Exception
*/
2020-09-11 18:23:56 +08:00
private function close(\HttpServer\Server $server)
2020-09-07 15:19:13 +08:00
{
echo 'waite.';
while ($server->isRunner()) {
2020-10-15 11:52:30 +08:00
if (!$this->masterIdCheck()) {
break;
}
2021-03-09 19:47:48 +08:00
sleep(1);
2020-09-07 15:19:13 +08:00
}
echo PHP_EOL;
}
2020-10-14 10:57:50 +08:00
/**
* WorkerId Iterator
*/
2020-12-17 14:09:14 +08:00
private function masterIdCheck(): bool
2020-10-14 10:57:50 +08:00
{
echo '.';
$files = new \DirectoryIterator($this->getWorkerPath());
2020-10-15 11:52:30 +08:00
if ($files->getSize() < 1) {
return false;
}
2020-10-14 10:57:50 +08:00
foreach ($files as $file) {
2021-03-09 19:47:48 +08:00
clearstatcache(true, $file->getFilename());
2020-10-14 10:57:50 +08:00
$content = file_get_contents($file->getRealPath());
2021-03-02 13:36:33 +08:00
exec("ps -ax | awk '{ print $1 }' | grep -e '^{$content}$'", $output);
2020-10-14 10:57:50 +08:00
if (count($output) > 0) {
$this->closeByPid($content);
} else {
@unlink($file->getRealPath());
}
}
2020-10-15 11:52:30 +08:00
return true;
2020-10-14 10:57:50 +08:00
}
/**
* @return string
*/
2021-01-12 16:27:49 +08:00
#[Pure] private function getWorkerPath(): string
2020-10-14 10:57:50 +08:00
{
return "glob://" . ltrim(APP_PATH, '/') . '/storage/worker/*.sock';
}
2020-09-07 15:19:13 +08:00
/**
* @param $port
* @return bool|array
2021-03-04 14:40:33 +08:00
* @throws Exception
2020-09-07 15:19:13 +08:00
*/
2020-12-17 14:09:14 +08:00
private function isUse($port): bool|array
2020-09-07 15:19:13 +08:00
{
if (empty($port)) {
return false;
}
2021-03-04 14:40:33 +08:00
if (Snowflake::getPlatform()->isLinux()) {
2021-03-02 13:36:33 +08:00
exec('netstat -tunlp | grep ' . $port, $output);
2020-09-07 15:52:42 +08:00
} else {
2021-03-02 13:36:33 +08:00
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
2020-09-07 15:52:42 +08:00
}
2020-09-07 15:19:13 +08:00
if (empty($output)) {
return false;
}
2020-10-10 11:15:00 +08:00
$this->error(implode(PHP_EOL, $output));
2020-09-07 15:19:13 +08:00
return $output;
}
/**
* @param $pid
*/
private function closeByPid($pid)
{
2021-03-02 13:36:33 +08:00
exec("ps -ef | grep $pid | grep -v grep | grep -v kill
2021-03-01 15:07:11 +08:00
if [ $? -eq 0 ];then
kill -9 `ps -ef | grep $pid | grep -v grep | grep -v kill | awk '{print $2}'`
else
echo $pid' No Found Process'
fi");
2020-09-07 15:19:13 +08:00
}
}