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

219 lines
5.3 KiB
PHP
Raw Normal View History

2022-06-16 17:49:06 +08:00
<?php
namespace Kiri\Server\Abstracts;
2022-10-11 15:15:04 +08:00
use Exception;
2023-04-22 02:04:31 +08:00
use Kiri;
2022-06-16 17:49:06 +08:00
use Swoole\Http\Server as HServer;
use Swoole\Server;
2024-09-03 15:05:18 +08:00
use Kiri\Server\Processes\AbstractProcess;
2022-06-16 17:51:22 +08:00
use Kiri\Server\Constant;
use Kiri\Server\Config;
2022-06-16 17:49:06 +08:00
use Swoole\WebSocket\Server as WServer;
2024-09-03 15:05:18 +08:00
use Swoole\Process;
2022-06-16 17:49:06 +08:00
trait TraitServer
{
2023-04-22 02:04:31 +08:00
2023-05-26 11:16:43 +08:00
/**
* @return void
2023-12-12 15:35:34 +08:00
* @throws
2023-05-26 11:16:43 +08:00
*/
public function onSignal(): void
{
2025-12-18 15:39:40 +08:00
$signal = \config('servers.signal', []);
2023-05-26 11:16:43 +08:00
$this->onPcntlSignal(SIGINT, [$this, 'onSigint']);
foreach ($signal as $sig => $value) {
if (is_array($value) && is_string($value[0])) {
$value[0] = \Kiri::getDi()->get($value[0]);
}
if (!is_callable($value, true)) {
throw new Exception('Register signal callback must can exec.');
}
$this->onPcntlSignal($sig, $value);
}
}
2023-08-11 00:12:32 +08:00
/**
* @param $no
* @param array $signInfo
* @return void
*/
public function onSigint($no, array $signInfo): void
{
2026-07-08 09:42:56 +08:00
static $handling = false;
if ($handling) {
return;
}
$handling = true;
2023-08-11 00:12:32 +08:00
try {
2026-07-08 11:39:27 +08:00
if (!$this->shouldHandleSigint()) {
2026-07-08 09:42:56 +08:00
return;
}
2024-08-29 17:01:07 +08:00
Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no);
2023-08-11 00:12:32 +08:00
$this->shutdown();
} catch (\Throwable $exception) {
2026-07-08 09:42:56 +08:00
\Kiri::getLogger()->json_log($exception);
2023-08-11 00:12:32 +08:00
}
}
2026-07-08 11:39:27 +08:00
private function shouldHandleSigint(): bool
{
$currentPid = getmypid();
if ($this->isMasterProcess($currentPid)) {
return true;
}
$masterPid = $this->getVerifiedPidFileMasterPid();
return $masterPid !== null && $masterPid === $currentPid;
}
private function getVerifiedPidFileMasterPid(): ?int
{
$pidFile = function_exists('storage') ? storage('.swoole.pid') : '';
if (!is_file($pidFile)) {
return null;
}
$pid = (int)trim((string)file_get_contents($pidFile));
if ($pid <= 1 || !$this->isProcessAlive($pid) || !$this->isSameAppProcess($pid) || !$this->isMasterProcess($pid)) {
return null;
}
return $pid;
}
private function isMasterProcess(int $pid): bool
{
$command = $this->getProcessCommand($pid);
if ($command === '') {
return false;
}
$prefix = '[' . config('site.id', 'system-service') . '].Master[';
return str_contains($command, $prefix);
}
private function getProcessCommand(int $pid): string
{
$command = @file_get_contents('/proc/' . $pid . '/cmdline');
if (!is_string($command) || $command === '') {
return '';
}
return str_replace("\0", ' ', $command);
}
private function isSameAppProcess(int $pid): bool
{
$cwd = @readlink('/proc/' . $pid . '/cwd');
if ($cwd === false || $cwd === '') {
return false;
}
return $this->normalizePath($cwd) === $this->normalizePath(APP_PATH);
}
private function normalizePath(string $path): string
{
$real = realpath($path) ?: $path;
return rtrim(str_replace('\\', '/', $real), '/');
}
private function isProcessAlive(int $pid): bool
{
return $pid > 1 && @Process::kill($pid, 0);
}
2023-05-26 11:16:43 +08:00
/**
* @param $signal
* @param $callback
* @return void
*/
private function onPcntlSignal($signal, $callback): void
{
2026-07-08 11:50:02 +08:00
Process::signal((int)$signal, static function (int $signo) use ($callback): void {
call_user_func($callback, $signo, []);
});
2023-05-26 11:16:43 +08:00
}
/**
* @param array $ports
* @return array
*/
public function sortService(array $ports): array
{
$array = [];
foreach ($ports as $port) {
$array = $this->sort($array, $port);
}
return $array;
}
/**
* @param array $ports
* @return array<Config>
*/
public function genConfigService(array $ports): array
{
$array = [];
$ports = $ports['ports'] ?? [];
foreach ($ports as $port) {
$array = $this->sort($array, $port);
}
return $array;
}
/**
* @param array $array
* @param $port
* @return array
*/
private function sort(array $array, $port): array
{
2023-05-26 11:21:36 +08:00
$config = instance(Config::class, [], $port);
2023-05-26 11:16:43 +08:00
if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) {
array_unshift($array, $config);
} else if ($port['type'] == Constant::SERVER_TYPE_HTTP) {
2026-04-17 11:47:59 +08:00
if (!empty($array) && $array[0]->type == Constant::SERVER_TYPE_WEBSOCKET) {
2023-05-26 11:16:43 +08:00
$array[] = $config;
} else {
array_unshift($array, $config);
}
} else {
$array[] = $config;
}
return $array;
}
/**
* @param $type
* @return string|null
*/
public function getServerClass($type): ?string
{
return match ($type) {
Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP,
2024-06-20 17:30:49 +08:00
Constant::SERVER_TYPE_UDP => Server::class,
Constant::SERVER_TYPE_HTTP => HServer::class,
2023-05-26 11:16:43 +08:00
Constant::SERVER_TYPE_WEBSOCKET => WServer::class,
2024-06-20 17:30:49 +08:00
default => null
2023-05-26 11:16:43 +08:00
};
}
2022-06-16 17:49:06 +08:00
}