Files
kiri-core/HttpServer/Action.php
T

155 lines
2.7 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-09-07 18:35:56 +08:00
use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException;
2020-09-07 15:19:13 +08:00
use Snowflake\Snowflake;
use Swoole\WebSocket\Server;
/**
* Class Action
* @package HttpServer
*/
trait Action
{
/**
* @param \HttpServer\Server $socket
* @return mixed
* @throws Exception
*/
2020-10-09 16:34:33 +08:00
public function restart(\HttpServer\Server $socket)
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)
{
$socket = storage('socket.sock');
if (!file_exists($socket)) {
$this->close($server);
} else {
$pathId = file_get_contents($socket);
@unlink($socket);
2020-11-11 10:52:53 +08:00
clearstatcache(true, $socket);
2020-09-07 15:19:13 +08:00
if (empty($pathId)) {
$this->close($server);
} else {
2020-09-09 01:01:09 +08:00
exec("ps -ef $pathId | grep $pathId", $output);
2020-09-08 11:20:41 +08:00
if (!empty($output)) {
exec("kill -TERM $pathId");
}
2020-09-07 15:19:13 +08:00
$this->close($server);
}
}
2020-09-07 16:34:07 +08:00
Snowflake::clearWorkerId();
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;
}
2020-09-07 15:19:13 +08:00
usleep(100);
}
echo PHP_EOL;
}
2020-10-14 10:57:50 +08:00
/**
* WorkerId Iterator
*/
private function masterIdCheck()
{
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) {
$content = file_get_contents($file->getRealPath());
exec("ps -ax | awk '{ print $1 }' | grep -e '^{$content}$'", $output);
if (count($output) > 0) {
$this->closeByPid($content);
} else {
@unlink($file->getRealPath());
2020-11-11 10:52:53 +08:00
clearstatcache(true, $file->getRealPath());
2020-10-14 10:57:50 +08:00
}
}
2020-10-15 11:52:30 +08:00
return true;
2020-10-14 10:57:50 +08:00
}
/**
* @return string
*/
private function getWorkerPath()
{
return "glob://" . ltrim(APP_PATH, '/') . '/storage/worker/*.sock';
}
2020-09-07 15:19:13 +08:00
/**
* @param $port
* @return bool|array
*/
private function isUse($port)
{
if (empty($port)) {
return false;
}
2020-09-07 15:52:42 +08:00
if (Snowflake::isLinux()) {
exec('netstat -tunlp | grep ' . $port, $output);
} else {
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
}
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)
{
2020-10-12 13:57:24 +08:00
exec("ps -ef $pid | grep $pid", $output);
if (!empty($output)) {
exec("kill -TERM $pid");
}
2020-09-07 15:19:13 +08:00
}
}