diff --git a/HttpServer/Action.php b/HttpServer/Action.php index 0df9e3d3..1be47456 100644 --- a/HttpServer/Action.php +++ b/HttpServer/Action.php @@ -131,13 +131,14 @@ trait Action /** * @param $port * @return bool|array + * @throws Exception */ private function isUse($port): bool|array { if (empty($port)) { return false; } - if (Snowflake::isLinux()) { + if (Snowflake::getPlatform()->isLinux()) { exec('netstat -tunlp | grep ' . $port, $output); } else { exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); diff --git a/HttpServer/Events/OnManagerStart.php b/HttpServer/Events/OnManagerStart.php index 11ef4c57..e290ecf3 100644 --- a/HttpServer/Events/OnManagerStart.php +++ b/HttpServer/Events/OnManagerStart.php @@ -31,7 +31,7 @@ class OnManagerStart extends Callback $events = Snowflake::app()->getEvent(); $events->trigger(Event::SERVER_MANAGER_START, [$server]); - if (!Snowflake::isLinux()) { + if (!Snowflake::getPlatform()->isLinux()) { return; } name(Config::get('id', false, 'system') . ' Server Manager.'); diff --git a/HttpServer/Events/OnStart.php b/HttpServer/Events/OnStart.php index daf01bed..2bb8a7c5 100644 --- a/HttpServer/Events/OnStart.php +++ b/HttpServer/Events/OnStart.php @@ -25,7 +25,7 @@ class OnStart extends Callback public function onHandler(Server $server) { Snowflake::setProcessId($server->master_pid); - if (Snowflake::isLinux()) { + if (Snowflake::getPlatform()->isLinux()) { name(Config::get('id', false, 'system:') . ' master.'); } $event = Snowflake::app()->getEvent(); diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 167978d3..dcd80b32 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -51,7 +51,6 @@ class OnWorkerStart extends Callback /** * @param Server $server * @param int $worker_id - * @param $prefix * @throws ComponentException * @throws ConfigException */ @@ -59,11 +58,7 @@ class OnWorkerStart extends Callback { putenv('environmental=' . Snowflake::TASK); - $prefix = sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid); - - $start = microtime(true); fire(Event::SERVER_TASK_START); - $this->debug(sprintf('%s use time %s', $prefix, microtime(true) - $start)); $this->set_process_name($server, $worker_id); } @@ -82,12 +77,8 @@ class OnWorkerStart extends Callback Snowflake::setWorkerId($server->worker_pid); putenv('environmental=' . Snowflake::WORKER); - $prefix = sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid); - try { - $start = microtime(true); fire(Event::SERVER_WORKER_START, [$worker_id]); - $this->debug(sprintf('%s use time %s', $prefix, microtime(true) - $start)); } catch (\Throwable $exception) { $this->addError($exception); write($exception->getMessage(), 'worker'); @@ -101,6 +92,7 @@ class OnWorkerStart extends Callback * @param $worker_id * @return string * @throws ConfigException + * @throws Exception */ private function set_process_name($socket, $worker_id): mixed { @@ -110,7 +102,7 @@ class OnWorkerStart extends Callback } else { $name = $prefix . ' worker: No.' . $worker_id; } - if (Snowflake::isMac()) { + if (Snowflake::getPlatform()->isMac()) { return 1; } return swoole_set_process_name($name); diff --git a/HttpServer/Server.php b/HttpServer/Server.php index e88853fc..54df5d4f 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -177,6 +177,7 @@ class Server extends HttpService /** * @return bool * @throws ConfigException + * @throws Exception */ public function isRunner(): bool { @@ -185,7 +186,7 @@ class Server extends HttpService return false; } foreach ($port as $value) { - if (Snowflake::isLinux()) { + if (Snowflake::getPlatform()->isLinux()) { exec('netstat -tunlp | grep ' . $value['port'], $output); } else { exec('lsof -i :' . $value['port'] . ' | grep -i "LISTEN"', $output); diff --git a/Kafka/Kafka.php b/Kafka/Kafka.php index bbab688f..e39d904a 100644 --- a/Kafka/Kafka.php +++ b/Kafka/Kafka.php @@ -53,7 +53,7 @@ class Kafka extends \Snowflake\Process\Process try { $prefix = rtrim(Snowflake::app()->id, ':'); - if (!Snowflake::isMac()) { + if (!Snowflake::getPlatform()->isMac()) { swoole_set_process_name($prefix . ' Kafka Consumer ' . $kafkaServer['topic']); } diff --git a/System/Environmental.php b/System/Environmental.php new file mode 100644 index 00000000..70a6c45c --- /dev/null +++ b/System/Environmental.php @@ -0,0 +1,46 @@ +isLinux()) { $this->name($this->getPrefix()); } $this->onHandler($process); diff --git a/System/Snowflake.php b/System/Snowflake.php index e06dcaa4..ad7f8f57 100644 --- a/System/Snowflake.php +++ b/System/Snowflake.php @@ -8,6 +8,7 @@ namespace Snowflake; use Exception; use HttpServer\IInterface\Task; +use JetBrains\PhpStorm\Pure; use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Core\Json; @@ -293,7 +294,7 @@ class Snowflake * @param $v2 * @return float */ - public static function distance(array $v1, array $v2): float + #[Pure] public static function distance(array $v1, array $v2): float { $maxX = max($v1['x'], $v2['x']); $minX = min($v1['x'], $v2['x']); @@ -315,6 +316,8 @@ class Snowflake /** * @param $process * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException */ public static function shutdown($process): void { @@ -343,33 +346,14 @@ class Snowflake /** - * @return bool + * @return Environmental + * @throws Exception */ - public static function isMac(): bool + public static function getPlatform(): Environmental { - $output = strtolower(PHP_OS | PHP_OS_FAMILY); - if (str_contains('mac', $output)) { - return true; - } else if (str_contains('darwin', $output)) { - return true; - } else { - return false; - } + return Snowflake::createObject(Environmental::class); } - /** - * @return bool - */ - public static function isLinux(): bool - { - if (!static::isMac()) { - return true; - } else { - return false; - } - } - - /** * @return mixed @@ -378,7 +362,6 @@ class Snowflake public static function reload(): mixed { return Snowflake::app()->getSwoole()->reload(); - return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1); } diff --git a/function.php b/function.php index 60287efe..63044f1d 100644 --- a/function.php +++ b/function.php @@ -542,10 +542,11 @@ if (!function_exists('name')) { /** * @param string $name + * @throws Exception */ function name(string $name) { - if (Snowflake::isMac()) { + if (Snowflake::getPlatform()->isMac()) { return; } swoole_set_process_name($name);