This commit is contained in:
2021-03-04 14:40:33 +08:00
parent 486daf639a
commit 5d1a3b3533
10 changed files with 67 additions and 42 deletions
+2 -1
View File
@@ -131,13 +131,14 @@ trait Action
/** /**
* @param $port * @param $port
* @return bool|array * @return bool|array
* @throws Exception
*/ */
private function isUse($port): bool|array private function isUse($port): bool|array
{ {
if (empty($port)) { if (empty($port)) {
return false; return false;
} }
if (Snowflake::isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
exec('netstat -tunlp | grep ' . $port, $output); exec('netstat -tunlp | grep ' . $port, $output);
} else { } else {
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output); exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
+1 -1
View File
@@ -31,7 +31,7 @@ class OnManagerStart extends Callback
$events = Snowflake::app()->getEvent(); $events = Snowflake::app()->getEvent();
$events->trigger(Event::SERVER_MANAGER_START, [$server]); $events->trigger(Event::SERVER_MANAGER_START, [$server]);
if (!Snowflake::isLinux()) { if (!Snowflake::getPlatform()->isLinux()) {
return; return;
} }
name(Config::get('id', false, 'system') . ' Server Manager.'); name(Config::get('id', false, 'system') . ' Server Manager.');
+1 -1
View File
@@ -25,7 +25,7 @@ class OnStart extends Callback
public function onHandler(Server $server) public function onHandler(Server $server)
{ {
Snowflake::setProcessId($server->master_pid); Snowflake::setProcessId($server->master_pid);
if (Snowflake::isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
name(Config::get('id', false, 'system:') . ' master.'); name(Config::get('id', false, 'system:') . ' master.');
} }
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
+2 -10
View File
@@ -51,7 +51,6 @@ class OnWorkerStart extends Callback
/** /**
* @param Server $server * @param Server $server
* @param int $worker_id * @param int $worker_id
* @param $prefix
* @throws ComponentException * @throws ComponentException
* @throws ConfigException * @throws ConfigException
*/ */
@@ -59,11 +58,7 @@ class OnWorkerStart extends Callback
{ {
putenv('environmental=' . Snowflake::TASK); 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); fire(Event::SERVER_TASK_START);
$this->debug(sprintf('%s use time %s', $prefix, microtime(true) - $start));
$this->set_process_name($server, $worker_id); $this->set_process_name($server, $worker_id);
} }
@@ -82,12 +77,8 @@ class OnWorkerStart extends Callback
Snowflake::setWorkerId($server->worker_pid); Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER); putenv('environmental=' . Snowflake::WORKER);
$prefix = sprintf('%s #%d Pid:%d start.', ucfirst(env('environmental')), $worker_id, $server->worker_pid);
try { try {
$start = microtime(true);
fire(Event::SERVER_WORKER_START, [$worker_id]); fire(Event::SERVER_WORKER_START, [$worker_id]);
$this->debug(sprintf('%s use time %s', $prefix, microtime(true) - $start));
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception); $this->addError($exception);
write($exception->getMessage(), 'worker'); write($exception->getMessage(), 'worker');
@@ -101,6 +92,7 @@ class OnWorkerStart extends Callback
* @param $worker_id * @param $worker_id
* @return string * @return string
* @throws ConfigException * @throws ConfigException
* @throws Exception
*/ */
private function set_process_name($socket, $worker_id): mixed private function set_process_name($socket, $worker_id): mixed
{ {
@@ -110,7 +102,7 @@ class OnWorkerStart extends Callback
} else { } else {
$name = $prefix . ' worker: No.' . $worker_id; $name = $prefix . ' worker: No.' . $worker_id;
} }
if (Snowflake::isMac()) { if (Snowflake::getPlatform()->isMac()) {
return 1; return 1;
} }
return swoole_set_process_name($name); return swoole_set_process_name($name);
+2 -1
View File
@@ -177,6 +177,7 @@ class Server extends HttpService
/** /**
* @return bool * @return bool
* @throws ConfigException * @throws ConfigException
* @throws Exception
*/ */
public function isRunner(): bool public function isRunner(): bool
{ {
@@ -185,7 +186,7 @@ class Server extends HttpService
return false; return false;
} }
foreach ($port as $value) { foreach ($port as $value) {
if (Snowflake::isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
exec('netstat -tunlp | grep ' . $value['port'], $output); exec('netstat -tunlp | grep ' . $value['port'], $output);
} else { } else {
exec('lsof -i :' . $value['port'] . ' | grep -i "LISTEN"', $output); exec('lsof -i :' . $value['port'] . ' | grep -i "LISTEN"', $output);
+1 -1
View File
@@ -53,7 +53,7 @@ class Kafka extends \Snowflake\Process\Process
try { try {
$prefix = rtrim(Snowflake::app()->id, ':'); $prefix = rtrim(Snowflake::app()->id, ':');
if (!Snowflake::isMac()) { if (!Snowflake::getPlatform()->isMac()) {
swoole_set_process_name($prefix . ' Kafka Consumer ' . $kafkaServer['topic']); swoole_set_process_name($prefix . ' Kafka Consumer ' . $kafkaServer['topic']);
} }
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace Snowflake;
use JetBrains\PhpStorm\Pure;
/**
* Class Environmental
* @package Snowflake
*/
class Environmental
{
/**
* @return bool
*/
#[Pure] public function isMac(): bool
{
$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 bool
*/
#[Pure] public function isLinux(): bool
{
if (!static::isMac()) {
return true;
} else {
return false;
}
}
}
+2 -1
View File
@@ -40,13 +40,14 @@ abstract class Process extends \Swoole\Process implements SProcess
/** /**
* @param Process $process * @param Process $process
* @throws ComponentException * @throws ComponentException
* @throws Exception
*/ */
public function _load(Process $process) public function _load(Process $process)
{ {
putenv('environmental=' . Snowflake::PROCESS); putenv('environmental=' . Snowflake::PROCESS);
fire(Event::SERVER_WORKER_START); fire(Event::SERVER_WORKER_START);
if (Snowflake::isLinux()) { if (Snowflake::getPlatform()->isLinux()) {
$this->name($this->getPrefix()); $this->name($this->getPrefix());
} }
$this->onHandler($process); $this->onHandler($process);
+8 -25
View File
@@ -8,6 +8,7 @@ namespace Snowflake;
use Exception; use Exception;
use HttpServer\IInterface\Task; use HttpServer\IInterface\Task;
use JetBrains\PhpStorm\Pure;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Core\Json; use Snowflake\Core\Json;
@@ -293,7 +294,7 @@ class Snowflake
* @param $v2 * @param $v2
* @return float * @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']); $maxX = max($v1['x'], $v2['x']);
$minX = min($v1['x'], $v2['x']); $minX = min($v1['x'], $v2['x']);
@@ -315,6 +316,8 @@ class Snowflake
/** /**
* @param $process * @param $process
* @throws ComponentException * @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
*/ */
public static function shutdown($process): void public static function shutdown($process): void
{ {
@@ -343,32 +346,13 @@ 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); return Snowflake::createObject(Environmental::class);
if (str_contains('mac', $output)) {
return true;
} else if (str_contains('darwin', $output)) {
return true;
} else {
return false;
} }
}
/**
* @return bool
*/
public static function isLinux(): bool
{
if (!static::isMac()) {
return true;
} else {
return false;
}
}
/** /**
@@ -378,7 +362,6 @@ class Snowflake
public static function reload(): mixed public static function reload(): mixed
{ {
return Snowflake::app()->getSwoole()->reload(); return Snowflake::app()->getSwoole()->reload();
return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1);
} }
+2 -1
View File
@@ -542,10 +542,11 @@ if (!function_exists('name')) {
/** /**
* @param string $name * @param string $name
* @throws Exception
*/ */
function name(string $name) function name(string $name)
{ {
if (Snowflake::isMac()) { if (Snowflake::getPlatform()->isMac()) {
return; return;
} }
swoole_set_process_name($name); swoole_set_process_name($name);