Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba43c969cf | |||
| efbd390d77 | |||
| 8f3d040cf2 | |||
| 59391c5489 | |||
| 801d4a3129 | |||
| c02f182601 | |||
| 994d0694e8 | |||
| c170ed745e | |||
| 7c1f950ea3 | |||
| de67663c64 | |||
| d46f09bd18 | |||
| 36fedb789e | |||
| 441e0867f6 | |||
| 31fd26aba5 | |||
| 16d5794ed0 | |||
| 78bca3b855 | |||
| 72536a34be | |||
| b5dbf7e779 | |||
| 1d48fdebc0 | |||
| 62b4e9edeb | |||
| f5785815ed | |||
| 30a1081f33 | |||
| 5cbbe5ac48 | |||
| f3a6b46104 | |||
| b435eb4e82 | |||
| 353692d016 | |||
| 6a1a0503e2 | |||
| 98cecce76a | |||
| 0570689e2c | |||
| 8937574ecc |
+55
-38
@@ -4,7 +4,6 @@ namespace Server\Abstracts;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Kiri;
|
||||
use Server\SInterface\OnProcessInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
@@ -15,13 +14,63 @@ use Swoole\Process;
|
||||
abstract class BaseProcess implements OnProcessInterface
|
||||
{
|
||||
|
||||
/** @var bool */
|
||||
protected bool $enableSwooleCoroutine = true;
|
||||
|
||||
|
||||
protected bool $isStop = false;
|
||||
|
||||
|
||||
protected mixed $redirect_stdin_and_stdout = null;
|
||||
|
||||
|
||||
protected int $pipe_type = SOCK_DGRAM;
|
||||
|
||||
|
||||
protected bool $enable_coroutine = true;
|
||||
|
||||
|
||||
public string $name = 'swoole process.';
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isStop(): bool
|
||||
{
|
||||
return $this->isStop;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRedirectStdinAndStdout(): mixed
|
||||
{
|
||||
return $this->redirect_stdin_and_stdout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPipeType(): int
|
||||
{
|
||||
return $this->pipe_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnableCoroutine(): bool
|
||||
{
|
||||
return $this->enable_coroutine;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -40,43 +89,11 @@ abstract class BaseProcess implements OnProcessInterface
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
*/
|
||||
public function signListen(Process $process): void
|
||||
{
|
||||
// if (Coroutine::getCid() === -1) {
|
||||
// Process::signal(SIGTERM | SIGKILL, function ($signo) use ($process) {
|
||||
// if ($signo) {
|
||||
// $lists = Kiri::app()->getProcess();
|
||||
// foreach ($lists as $process) {
|
||||
// $process->exit(0);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// } else {
|
||||
// Coroutine::create(function () use ($process) {
|
||||
// /** @var Coroutine\Socket $message */
|
||||
// $message = $process->exportSocket();
|
||||
// if ($message->recv() == 0x03455343213212) {
|
||||
// $this->waiteExit($process);
|
||||
// }
|
||||
// });
|
||||
// Coroutine::create(function () use ($process) {
|
||||
// $data = Coroutine::waitSignal(SIGTERM | SIGKILL, -1);
|
||||
// if ($data) {
|
||||
// $lists = Kiri::app()->getProcess();
|
||||
// foreach ($lists as $name => $process) {
|
||||
// foreach ($process as $item) {
|
||||
// /** @var Coroutine\Socket $export */
|
||||
// $export = $item->exportSocket();
|
||||
// $export->send(0x03455343213212);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +133,7 @@ abstract class BaseProcess implements OnProcessInterface
|
||||
*/
|
||||
private function sleep(): void
|
||||
{
|
||||
if ($this->enableSwooleCoroutine) {
|
||||
if ($this->enable_coroutine) {
|
||||
Coroutine::sleep(0.1);
|
||||
} else {
|
||||
usleep(100);
|
||||
|
||||
@@ -17,19 +17,10 @@ class OnTaskerStart extends WorkerStart implements EventDispatcherInterface
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function dispatch(object $event)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
ServerManager::setEnv('environmental', Kiri::TASK);
|
||||
// if (!is_enable_file_modification_listening()) {
|
||||
// $this->interpretDirectory();
|
||||
// }
|
||||
|
||||
$this->mixed($event, false, $time);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,19 +21,11 @@ class OnWorkerStart extends WorkerStart implements EventDispatcherInterface
|
||||
/**
|
||||
* @param object $event
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function dispatch(object $event)
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
ServerManager::setEnv('environmental', Kiri::WORKER);
|
||||
// if (is_enable_file_modification_listening()) {
|
||||
// $this->router->read_files();
|
||||
// $this->interpretDirectory();
|
||||
// }
|
||||
$this->mixed($event, true, $time);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ abstract class Server
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
$name = '[' . Config::get('id', 'system-service') . ']';
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
|
||||
@@ -28,47 +28,6 @@ class WorkerStart
|
||||
public Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function interpretDirectory()
|
||||
{
|
||||
$di = Kiri::getDi();
|
||||
|
||||
$this->annotation->read(APP_PATH . 'app', 'App');
|
||||
|
||||
$fileLists = $this->annotation->read(APP_PATH . 'app');
|
||||
foreach ($fileLists->runtime(APP_PATH . 'app') as $class) {
|
||||
foreach (NoteManager::getTargetNote($class) as $value) {
|
||||
$value->execute($class);
|
||||
}
|
||||
$methods = $di->getMethodAttribute($class);
|
||||
foreach ($methods as $method => $attribute) {
|
||||
if (empty($attribute)) {
|
||||
continue;
|
||||
}
|
||||
foreach ($attribute as $item) {
|
||||
$item->execute($class, $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $event
|
||||
* @param $isWorker
|
||||
* @param $time
|
||||
* @throws \Kiri\Exception\ConfigException
|
||||
*/
|
||||
protected function mixed($event, $isWorker, $time)
|
||||
{
|
||||
$name = Config::get('id', 'system-service');
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, $isWorker ? 'Worker' : 'Taker',
|
||||
$event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnAfterCommandExecute
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function __construct(public mixed $data)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnBeforeCommandExecute
|
||||
{
|
||||
|
||||
}
|
||||
@@ -75,9 +75,8 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
public function onWorkerStop(Server $server, int $workerId)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
||||
|
||||
Timer::clearAll();
|
||||
Timer::clearAll();
|
||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
||||
}
|
||||
|
||||
|
||||
@@ -88,10 +87,10 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
*/
|
||||
public function onWorkerExit(Server $server, int $workerId)
|
||||
{
|
||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||
|
||||
ServerManager::setEnv('state', 'exit');
|
||||
}
|
||||
|
||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -112,7 +111,7 @@ class OnServerWorker extends \Server\Abstracts\Server
|
||||
|
||||
$this->logger->error($message);
|
||||
|
||||
$this->system_mail($message);
|
||||
// $this->system_mail($message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ interface OnOpenInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\WebSocket\Server $server
|
||||
* @param \Swoole\Http\Request $request
|
||||
* @param Server $server
|
||||
* @param Request $request
|
||||
*/
|
||||
public function onOpen(Server $server, Request $request): void;
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ class Server extends HttpService
|
||||
{
|
||||
|
||||
private array $process = [
|
||||
LoggerProcess::class
|
||||
];
|
||||
|
||||
|
||||
|
||||
+8
-9
@@ -6,12 +6,7 @@ namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Constrict\Request;
|
||||
use Http\Constrict\RequestInterface;
|
||||
use Http\Constrict\Response;
|
||||
use Http\Constrict\ResponseInterface;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Di\ContainerInterface;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
@@ -53,8 +48,8 @@ class ServerCommand extends Command
|
||||
{
|
||||
$this->setName('sw:server')
|
||||
->setDescription('server start|stop|reload|restart')
|
||||
->addArgument('action', InputArgument::REQUIRED)
|
||||
->addOption('daemon', 'd', InputOption::VALUE_OPTIONAL,'is run daemonize',-1);
|
||||
->addArgument('action', InputArgument::OPTIONAL, 'run action', 'start')
|
||||
->addOption('daemon', 'd', InputOption::VALUE_OPTIONAL, 'is run daemonize');
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +63,10 @@ class ServerCommand extends Command
|
||||
{
|
||||
try {
|
||||
$manager = Kiri::app()->getServer();
|
||||
$manager->setDaemon((int)is_null($input->getOption('daemon')));
|
||||
$manager->setDaemon((int)!is_null($input->getOption('daemon')));
|
||||
if (is_null($input->getArgument('action'))) {
|
||||
$input->setArgument('action', 'restart');
|
||||
}
|
||||
if (!in_array($input->getArgument('action'), self::ACTIONS)) {
|
||||
throw new Exception('I don\'t know what I want to do.');
|
||||
}
|
||||
@@ -110,12 +108,13 @@ class ServerCommand extends Command
|
||||
/**
|
||||
* @param $manager
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function generate_runtime_builder($manager): void
|
||||
{
|
||||
$this->configure_set();
|
||||
|
||||
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
|
||||
Kiri::app()->getRouter()->read_files();
|
||||
|
||||
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
|
||||
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
|
||||
|
||||
+16
-46
@@ -10,14 +10,13 @@ use Kiri\Di\ContainerInterface;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
|
||||
use Server\Abstracts\BaseProcess;
|
||||
use Server\Handler\OnPipeMessage;
|
||||
use Server\Handler\OnServer;
|
||||
use Server\Handler\OnServerManager;
|
||||
use Server\Handler\OnServerReload;
|
||||
use Server\Handler\OnServerWorker;
|
||||
use Server\Handler\OnServerTask;
|
||||
|
||||
use Server\Handler\OnServerWorker;
|
||||
use Server\SInterface\OnCloseInterface;
|
||||
use Server\SInterface\OnConnectInterface;
|
||||
use Server\SInterface\OnDisconnectInterface;
|
||||
@@ -155,56 +154,29 @@ class ServerManager
|
||||
|
||||
|
||||
/**
|
||||
* @param string|OnProcessInterface $customProcess
|
||||
* @param null $redirect_stdin_and_stdout
|
||||
* @param int|null $pipe_type
|
||||
* @param bool $enable_coroutine
|
||||
* @param string|OnProcessInterface|BaseProcess $customProcess
|
||||
* @throws Exception
|
||||
*/
|
||||
public function addProcess(string|OnProcessInterface $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
|
||||
public function addProcess(string|OnProcessInterface|BaseProcess $customProcess)
|
||||
{
|
||||
$process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine);
|
||||
$this->server->addProcess($process);
|
||||
if ($customProcess instanceof OnProcessInterface) {
|
||||
Kiri::app()->addProcess($customProcess::class, $process);
|
||||
} else {
|
||||
Kiri::app()->addProcess($customProcess, $process);
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::getDi()->get($customProcess);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $customProcess
|
||||
* @param $redirect_stdin_and_stdout
|
||||
* @param $pipe_type
|
||||
* @param $enable_coroutine
|
||||
* @return Process
|
||||
*/
|
||||
private function initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine): Process
|
||||
{
|
||||
$server = $this->server;
|
||||
return new Process(function (Process $soloProcess) use ($customProcess, $server) {
|
||||
$time = microtime(true);
|
||||
if (is_string($customProcess)) {
|
||||
$customProcess = Kiri::createObject($customProcess, [$server]);
|
||||
}
|
||||
|
||||
$name = $customProcess->getProcessName($soloProcess);
|
||||
|
||||
$system = sprintf('%s.process[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
|
||||
$process = new Process(function (Process $soloProcess) use ($customProcess) {
|
||||
$system = sprintf('[%s].process', Config::get('id', 'system-service'));
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
$soloProcess->name($system . '.' . $name . ' start.');
|
||||
$soloProcess->name($system . '(' . $customProcess->getName() . ')');
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, 'Process ' . $name,
|
||||
$server->master_pid, $soloProcess->pid, round(microtime(true) - $time, 6) . 's') . PHP_EOL;
|
||||
echo "\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m " . $system . $customProcess->getName() . ' start.' . PHP_EOL;
|
||||
$customProcess->signListen($soloProcess);
|
||||
$customProcess->onHandler($soloProcess);
|
||||
},
|
||||
$redirect_stdin_and_stdout,
|
||||
$pipe_type,
|
||||
$enable_coroutine
|
||||
$customProcess->getRedirectStdinAndStdout(),
|
||||
$customProcess->getPipeType(),
|
||||
$customProcess->isEnableCoroutine()
|
||||
);
|
||||
// $this->container->setBindings($customProcess::class, $process);
|
||||
$this->server->addProcess($process);
|
||||
}
|
||||
|
||||
|
||||
@@ -279,9 +251,7 @@ class ServerManager
|
||||
private function mergeConfig($config, $daemon): array
|
||||
{
|
||||
$config['settings'] = $config['settings'] ?? [];
|
||||
if (!isset($config['settings']['daemonize']) || !$config['settings']['daemonize'] != $daemon) {
|
||||
$config['settings']['daemonize'] = $daemon;
|
||||
}
|
||||
$config['settings']['daemonize'] = $daemon;
|
||||
if (!isset($config['settings']['log_file'])) {
|
||||
$config['settings']['log_file'] = storage('system.log');
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"name": "game-worker/http-server",
|
||||
"name": "game-worker/kiri-http-server",
|
||||
"description": "http-server",
|
||||
"type": "composer-plugin",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@@ -16,7 +15,7 @@
|
||||
"psr/http-server-middleware": "^1.0",
|
||||
"psr/http-message": "^1.0",
|
||||
"psr/event-dispatcher": "^1.0",
|
||||
"game-worker/kiri-http-message": "^v1.0"
|
||||
"game-worker/kiri-http-message": "^v1.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
Reference in New Issue
Block a user