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
* @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);
+1 -1
View File
@@ -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.');
+1 -1
View File
@@ -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();
+2 -10
View File
@@ -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);
+2 -1
View File
@@ -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);
+1 -1
View File
@@ -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']);
}
+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
* @throws ComponentException
* @throws Exception
*/
public function _load(Process $process)
{
putenv('environmental=' . Snowflake::PROCESS);
fire(Event::SERVER_WORKER_START);
if (Snowflake::isLinux()) {
if (Snowflake::getPlatform()->isLinux()) {
$this->name($this->getPrefix());
}
$this->onHandler($process);
+8 -25
View File
@@ -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);
}
+2 -1
View File
@@ -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);