modify
This commit is contained in:
@@ -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']));
|
||||
|
||||
+27
-30
@@ -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;
|
||||
|
||||
/**
|
||||
@@ -20,40 +16,41 @@ use Snowflake\Snowflake;
|
||||
class Command extends \Console\Command
|
||||
{
|
||||
|
||||
public string $command = 'sw:server';
|
||||
public string $command = 'sw:server';
|
||||
|
||||
|
||||
public string $description = 'server start|stop|reload|restart';
|
||||
public string $description = 'server start|stop|reload|restart';
|
||||
|
||||
|
||||
const ACTIONS = ['start', 'stop', 'restart'];
|
||||
const ACTIONS = ['start', 'stop', 'restart'];
|
||||
|
||||
|
||||
/**
|
||||
* @param Input $dtl
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function onHandler(Input $dtl): string
|
||||
{
|
||||
$manager = Snowflake::app()->getServer();
|
||||
$manager->setDaemon($dtl->get('daemon', 0));
|
||||
/**
|
||||
* @param Input $dtl
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function onHandler(Input $dtl): string
|
||||
{
|
||||
$manager = Snowflake::app()->getServer();
|
||||
$manager->setDaemon($dtl->get('daemon', 0));
|
||||
|
||||
if (!in_array($dtl->get('action'), self::ACTIONS)) {
|
||||
return 'I don\'t know what I want to do.';
|
||||
}
|
||||
if (!in_array($dtl->get('action'), self::ACTIONS)) {
|
||||
return 'I don\'t know what I want to do.';
|
||||
}
|
||||
|
||||
if ($manager->isRunner() && $dtl->get('action') == 'start') {
|
||||
return 'Service is running. Please use restart.';
|
||||
}
|
||||
/** @var Shutdown $shutdown */
|
||||
$shutdown = Snowflake::app()->get('shutdown');
|
||||
if ($shutdown->isRunning() && $dtl->get('action') == 'start') {
|
||||
return 'Service is running. Please use restart.';
|
||||
}
|
||||
|
||||
$manager->shutdown();
|
||||
if ($dtl->get('action') == 'stop') {
|
||||
return 'shutdown success.';
|
||||
}
|
||||
|
||||
return $manager->start();
|
||||
}
|
||||
$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,7 +69,9 @@ class OnWorkerStart extends Callback
|
||||
{
|
||||
putenv('environmental=' . Snowflake::TASK);
|
||||
|
||||
fire(Event::SERVER_TASK_START);
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user