Compare commits

..

29 Commits

Author SHA1 Message Date
as2252258 efbd390d77 改名 2021-11-05 15:08:29 +08:00
as2252258 8f3d040cf2 111 2021-11-05 01:03:49 +08:00
as2252258 59391c5489 111 2021-11-05 01:03:31 +08:00
as2252258 801d4a3129 111 2021-11-05 01:02:49 +08:00
as2252258 c02f182601 111 2021-11-05 01:01:12 +08:00
as2252258 994d0694e8 111 2021-11-05 00:12:13 +08:00
as2252258 c170ed745e 111 2021-11-05 00:01:03 +08:00
as2252258 7c1f950ea3 111 2021-11-05 00:00:05 +08:00
as2252258 de67663c64 111 2021-11-04 23:58:05 +08:00
as2252258 d46f09bd18 改名 2021-11-04 18:53:31 +08:00
as2252258 36fedb789e 改名 2021-11-04 18:50:42 +08:00
as2252258 441e0867f6 改名 2021-11-04 16:40:08 +08:00
as2252258 31fd26aba5 改名 2021-11-04 16:31:54 +08:00
as2252258 16d5794ed0 改名 2021-11-04 16:19:55 +08:00
as2252258 78bca3b855 改名 2021-11-04 16:18:02 +08:00
as2252258 72536a34be 改名 2021-11-04 10:39:26 +08:00
as2252258 b5dbf7e779 改名 2021-11-04 10:31:51 +08:00
as2252258 1d48fdebc0 改名 2021-11-03 18:26:06 +08:00
as2252258 62b4e9edeb 改名 2021-11-03 18:25:43 +08:00
as2252258 f5785815ed 改名 2021-11-03 18:24:59 +08:00
as2252258 30a1081f33 改名 2021-11-03 18:24:24 +08:00
as2252258 5cbbe5ac48 改名 2021-11-03 18:22:56 +08:00
as2252258 f3a6b46104 改名 2021-11-03 18:02:07 +08:00
as2252258 b435eb4e82 改名 2021-11-03 17:43:34 +08:00
as2252258 353692d016 改名 2021-11-03 17:41:01 +08:00
as2252258 6a1a0503e2 改名 2021-11-03 17:39:08 +08:00
as2252258 98cecce76a 改名 2021-11-03 16:29:47 +08:00
as2252258 0570689e2c 改名 2021-11-03 15:51:01 +08:00
as2252258 8937574ecc 改名 2021-11-03 15:41:52 +08:00
13 changed files with 90 additions and 189 deletions
+55 -38
View File
@@ -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);
-9
View File
@@ -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);
}
-8
View File
@@ -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);
}
}
+1 -1
View File
@@ -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;
}
-41
View File
@@ -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
-16
View File
@@ -1,16 +0,0 @@
<?php
namespace Server\Events;
class OnAfterCommandExecute
{
/**
* @param mixed $data
*/
public function __construct(public mixed $data)
{
}
}
-8
View File
@@ -1,8 +0,0 @@
<?php
namespace Server\Events;
class OnBeforeCommandExecute
{
}
+6 -7
View File
@@ -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);
}
+2 -2
View File
@@ -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;
-1
View File
@@ -24,7 +24,6 @@ class Server extends HttpService
{
private array $process = [
LoggerProcess::class
];
+8 -9
View File
@@ -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
View File
@@ -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[%d]', Config::get('id', 'system-service'), $soloProcess->pid);
if (Kiri::getPlatform()->isLinux()) {
$soloProcess->name($system . '.' . $name . ' start.');
$soloProcess->name($system . '.' . $customProcess->getName() . ' start.');
}
$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
View File
@@ -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": {