first commit
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Kiri;
|
||||
use Server\SInterface\OnProcessInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class BaseProcess implements OnProcessInterface
|
||||
{
|
||||
|
||||
/** @var bool */
|
||||
protected bool $enableSwooleCoroutine = true;
|
||||
|
||||
|
||||
protected bool $isStop = false;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function onProcessStop(): void
|
||||
{
|
||||
$this->isStop = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function checkProcessIsStop(): bool
|
||||
{
|
||||
return $this->isStop === true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function exit(): void
|
||||
{
|
||||
putenv('process.status=idle');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isWorking(): bool
|
||||
{
|
||||
return env('process.status', 'working') == 'working';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function waiteExit(Process $process): void
|
||||
{
|
||||
$this->onProcessStop();
|
||||
while ($this->isWorking()) {
|
||||
$this->sleep();
|
||||
}
|
||||
$process->exit(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function sleep(): void
|
||||
{
|
||||
if ($this->enableSwooleCoroutine) {
|
||||
Coroutine::sleep(0.1);
|
||||
} else {
|
||||
usleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use ReflectionException;
|
||||
use Server\ServerManager;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Handler\Router;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Di\NoteManager;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use ReflectionException;
|
||||
use Server\ServerManager;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class Server
|
||||
* @package Server\Abstracts
|
||||
*/
|
||||
abstract class Server
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function setProcessName($prefix)
|
||||
{
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
swoole_set_process_name($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Annotation\Inject;
|
||||
use Http\Handler\Router;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Di\NoteManager;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
class WorkerStart
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Annotation
|
||||
*/
|
||||
#[Inject(Annotation::class)]
|
||||
public Annotation $annotation;
|
||||
|
||||
|
||||
/**
|
||||
* @var Router
|
||||
*/
|
||||
#[Inject(Router::class)]
|
||||
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
|
||||
*/
|
||||
protected function setProcessName($prefix)
|
||||
{
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
swoole_set_process_name($name);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user