modify
This commit is contained in:
@@ -126,11 +126,11 @@ class Loader extends BaseObject
|
||||
*/
|
||||
public function _scanDir(DirectoryIterator $paths, $namespace)
|
||||
{
|
||||
/** @var DirectoryIterator $path */
|
||||
$DIRECTORY = $this->createDirectoryMap($paths);
|
||||
foreach ($paths as $path) {
|
||||
if ($path->getFilename() === '.' || $path->getFilename() === '..') {
|
||||
continue;
|
||||
}
|
||||
if ($path->isDot()) continue;
|
||||
|
||||
if (str_starts_with($path->getFilename(), '.')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -36,8 +36,6 @@ abstract class Callback extends HttpService
|
||||
protected function clear(Server $server, $worker_id, $message)
|
||||
{
|
||||
try {
|
||||
Snowflake::clearProcessId($server->worker_pid);
|
||||
|
||||
/** @var Process $logger */
|
||||
$logger = Snowflake::app()->get(LoggerProcess::class);
|
||||
$logger->write(Json::encode([$this->_MESSAGE[$message] . $worker_id, 'app']));
|
||||
|
||||
@@ -5,12 +5,8 @@ namespace HttpServer;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Input;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindPropertyException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
@@ -44,15 +40,16 @@ class Command extends \Console\Command
|
||||
return 'I don\'t know what I want to do.';
|
||||
}
|
||||
|
||||
if ($manager->isRunner() && $dtl->get('action') == 'start') {
|
||||
/** @var Shutdown $shutdown */
|
||||
$shutdown = Snowflake::app()->get('shutdown');
|
||||
if ($shutdown->isRunning() && $dtl->get('action') == 'start') {
|
||||
return 'Service is running. Please use restart.';
|
||||
}
|
||||
|
||||
$manager->shutdown();
|
||||
$shutdown->shutdown();
|
||||
if ($dtl->get('action') == 'stop') {
|
||||
return 'shutdown success.';
|
||||
}
|
||||
|
||||
return $manager->start();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ class OnBeforeReload extends Callback
|
||||
{
|
||||
$event = Snowflake::app()->getEvent();
|
||||
$event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
|
||||
|
||||
Snowflake::clearWorkerPid();
|
||||
Snowflake::clearTaskPid();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ class OnWorkerStart extends Callback
|
||||
{
|
||||
putenv('environmental=' . Snowflake::TASK);
|
||||
|
||||
Snowflake::setTaskId($server->worker_pid);
|
||||
|
||||
fire(Event::SERVER_TASK_START);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace HttpServer;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
|
||||
|
||||
/**
|
||||
* Class Shutdown
|
||||
* @package HttpServer
|
||||
*/
|
||||
class Shutdown extends Component
|
||||
{
|
||||
|
||||
|
||||
private string $taskDirectory;
|
||||
private string $workerDirectory;
|
||||
private string $managerDirectory;
|
||||
private string $processDirectory;
|
||||
|
||||
|
||||
public function init()
|
||||
{
|
||||
$this->taskDirectory = storage(null, 'pid/task');
|
||||
$this->workerDirectory = storage(null, 'pid/worker');
|
||||
$this->managerDirectory = storage(null, 'pid/manager');
|
||||
$this->processDirectory = storage(null, 'pid/process');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown(): void
|
||||
{
|
||||
$master_pid = Server()->setting['pid_file'] ?? PID_PATH;
|
||||
clearstatcache($master_pid);
|
||||
if (file_exists($master_pid)) {
|
||||
$this->close($master_pid);
|
||||
}
|
||||
$this->closeOther();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 关闭其他进程
|
||||
*/
|
||||
private function closeOther(): void
|
||||
{
|
||||
$this->directoryCheck($this->managerDirectory);
|
||||
$this->directoryCheck($this->taskDirectory);
|
||||
$this->directoryCheck($this->workerDirectory);
|
||||
$this->directoryCheck($this->processDirectory);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* check server is running.
|
||||
*/
|
||||
public function isRunning()
|
||||
{
|
||||
$master_pid = Server()->setting['pid_file'] ?? PID_PATH;
|
||||
|
||||
return $this->pidIsExists($master_pid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return bool
|
||||
*/
|
||||
public function pidIsExists($content): bool
|
||||
{
|
||||
$content = shell_exec('ps -eo pid,cmd,state | grep ' . $content . ' | grep -v grep');
|
||||
if (empty($content)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*/
|
||||
public function directoryCheck(string $path)
|
||||
{
|
||||
$dir = new \DirectoryIterator($path);
|
||||
if ($dir->getSize() < 1) {
|
||||
return true;
|
||||
}
|
||||
foreach ($dir as $value) {
|
||||
/** @var \DirectoryIterator $value */
|
||||
if (!$value->valid()) continue;
|
||||
|
||||
$this->close($value->getRealPath());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function close(string $value)
|
||||
{
|
||||
$resource = fopen($value, 'r');
|
||||
$content = fgets($resource);
|
||||
fclose($resource);
|
||||
|
||||
while ($this->pidIsExists($content)) {
|
||||
exec('kill -15 ' . $content);
|
||||
sleep(1);
|
||||
}
|
||||
@unlink($value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ use HttpServer\Service\Http;
|
||||
use HttpServer\Service\Packet;
|
||||
use HttpServer\Service\Receive;
|
||||
use HttpServer\Service\Websocket;
|
||||
use HttpServer\Shutdown;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kafka\Producer;
|
||||
use Annotation\Annotation as SAnnotation;
|
||||
@@ -482,6 +483,7 @@ abstract class BaseApplication extends Service
|
||||
'rpc' => ['class' => \Rpc\Producer::class],
|
||||
'rpc-service' => ['class' => \Rpc\Service::class],
|
||||
'http2' => ['class' => Http2::class],
|
||||
'shutdown' => ['class' => Shutdown::class],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use HttpServer\Http\Response;
|
||||
use HttpServer\HttpFilter;
|
||||
use HttpServer\Route\Router;
|
||||
use HttpServer\Server;
|
||||
use HttpServer\Shutdown;
|
||||
use Kafka\Producer;
|
||||
use Snowflake\Async;
|
||||
use Snowflake\Cache\Redis;
|
||||
@@ -50,6 +51,7 @@ use Rpc\Producer as RPCProducer;
|
||||
* @property HttpFilter $filter
|
||||
* @property RPCProducer $rpc
|
||||
* @property Channel $channel
|
||||
* @property Shutdown $shutdown
|
||||
*/
|
||||
trait TraitApplication
|
||||
{
|
||||
|
||||
@@ -34,7 +34,7 @@ abstract class Process extends \Swoole\Process implements SProcess
|
||||
{
|
||||
parent::__construct([$this, '_load'], false, 1, $enable_coroutine);
|
||||
$this->application = $application;
|
||||
Snowflake::setWorkerId($this->pid);
|
||||
Snowflake::setProcessId($this->pid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+63
-5
@@ -152,7 +152,7 @@ class Snowflake
|
||||
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
|
||||
$path = Config::get('storage', false, $default);
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path);
|
||||
mkdir($path, 0777, true);
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
@@ -176,6 +176,17 @@ class Snowflake
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $workerId
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function setManagerId($workerId): mixed
|
||||
{
|
||||
return self::writeFile(storage($workerId . '.sock', 'pid/manager'), $workerId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $workerId
|
||||
* @return mixed
|
||||
@@ -183,7 +194,7 @@ class Snowflake
|
||||
*/
|
||||
public static function setProcessId($workerId): mixed
|
||||
{
|
||||
return self::writeFile(storage('socket.sock'), $workerId);
|
||||
return self::writeFile(storage($workerId . '.sock', 'pid/process'), $workerId);
|
||||
}
|
||||
|
||||
|
||||
@@ -197,10 +208,23 @@ class Snowflake
|
||||
if (empty($workerId)) {
|
||||
return $workerId;
|
||||
}
|
||||
return self::writeFile(storage($workerId . '.sock', 'worker'), $workerId);
|
||||
return self::writeFile(storage($workerId . '.sock', 'pid/worker'), $workerId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $workerId
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function setTaskId($workerId): mixed
|
||||
{
|
||||
if (empty($workerId)) {
|
||||
return $workerId;
|
||||
}
|
||||
return self::writeFile(storage($workerId . '.sock', 'pid/task'), $workerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $fileName
|
||||
* @param $content
|
||||
@@ -236,11 +260,45 @@ class Snowflake
|
||||
|
||||
/**
|
||||
* @param $workerId
|
||||
* @param bool $isWorker
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function clearProcessId($workerId)
|
||||
public static function clearProcessId($workerId, $isWorker = false)
|
||||
{
|
||||
@unlink(storage($workerId . '.sock', 'worker'));
|
||||
clearstatcache();
|
||||
$directory = $isWorker === true ? 'pid/worker' : 'pid/task';
|
||||
if (!file_exists($file = storage($workerId, $directory))) {
|
||||
return;
|
||||
}
|
||||
shell_exec('rm -rf ' . $file);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|null $taskPid
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function clearTaskPid(string $taskPid = null)
|
||||
{
|
||||
if (empty($taskPid)) {
|
||||
exec('rm -rf ' . storage(null, 'pid/task'));
|
||||
} else {
|
||||
static::clearProcessId($taskPid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $taskPid
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function clearWorkerPid($taskPid = null)
|
||||
{
|
||||
if (empty($taskPid)) {
|
||||
exec('rm -rf ' . storage(null, 'pid/worker'));
|
||||
} else {
|
||||
static::clearProcessId($taskPid, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+31
-20
@@ -9,6 +9,10 @@ use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Http\Response;
|
||||
use HttpServer\Route\Router;
|
||||
use HttpServer\Service\Http;
|
||||
use HttpServer\Service\Packet;
|
||||
use HttpServer\Service\Receive;
|
||||
use HttpServer\Service\Websocket;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Error\Logger;
|
||||
@@ -467,6 +471,20 @@ if (!function_exists('Input')) {
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('Server')) {
|
||||
|
||||
|
||||
/**
|
||||
* @return Http|Packet|Receive|Websocket|null
|
||||
* @throws Exception
|
||||
*/
|
||||
function Server(): Http|Packet|Receive|Websocket|null
|
||||
{
|
||||
return Snowflake::app()->getSwoole();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('storage')) {
|
||||
|
||||
/**
|
||||
@@ -477,14 +495,18 @@ if (!function_exists('storage')) {
|
||||
*/
|
||||
function storage($fileName = '', $path = ''): string
|
||||
{
|
||||
$basePath = Snowflake::getStoragePath();
|
||||
if (empty($path)) {
|
||||
$fileName = rtrim($basePath, '/') . '/' . $fileName;
|
||||
} else if (empty($fileName)) {
|
||||
return rtrim(initDir($basePath, $path));
|
||||
} else {
|
||||
$fileName = rtrim(initDir($basePath, $path)) . '/' . $fileName;
|
||||
|
||||
$basePath = rtrim(Snowflake::getStoragePath(), '/');
|
||||
if (!empty($path)) {
|
||||
$path = ltrim($path, '/');
|
||||
if (!is_dir($basePath . '/' . $path)) {
|
||||
mkdir($basePath . '/' . $path, 0777, true);
|
||||
}
|
||||
}
|
||||
if (empty($fileName)) {
|
||||
return $basePath . '/' . $path . '/';
|
||||
}
|
||||
$fileName = $basePath . '/' . $path . '/' . $fileName;
|
||||
if (!file_exists($fileName)) {
|
||||
touch($fileName);
|
||||
}
|
||||
@@ -498,20 +520,9 @@ if (!function_exists('storage')) {
|
||||
* @return false|string
|
||||
* @throws Exception
|
||||
*/
|
||||
function initDir($basePath, $path): bool|string
|
||||
function initDir($path): bool|string
|
||||
{
|
||||
$explode = array_filter(explode('/', $path));
|
||||
$_path = '/' . trim($basePath, '/') . '/';
|
||||
foreach ($explode as $value) {
|
||||
$_path .= $value . '/';
|
||||
if (!is_dir(rtrim($_path, '/'))) {
|
||||
mkdir(rtrim($_path, '/'));
|
||||
}
|
||||
if (!is_dir($_path)) {
|
||||
throw new Exception('System error, directory ' . $_path . ' is not writable');
|
||||
}
|
||||
}
|
||||
return realpath($_path);
|
||||
return mkdir($path, 0777, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user