This commit is contained in:
as2252258@163.com
2021-09-19 16:42:29 +08:00
parent d77f5b16c3
commit ff58d0faef
16 changed files with 256 additions and 262 deletions
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Server\Events;
use Swoole\Server;
/**
*
*/
class OnTaskerStart
{
/**
* @param Server $server
* @param int $workerId
*/
public function __construct(public Server $server, public int $workerId)
{
}
}
+1 -1
View File
@@ -61,7 +61,7 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
$PsrResponse = $this->handler($handler, $PsrRequest);
}
} catch (\Throwable $throwable) {
$PsrResponse = \response()->withStatus($throwable->getCode())
$PsrResponse = $this->response->withStatus($throwable->getCode())
->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML)
->withBody(new Stream(jTraceEx($throwable, null, true)));
} finally {
+9 -3
View File
@@ -7,6 +7,7 @@ use Exception;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Kiri;
use Kiri\Runtime;
use Server\Events\OnAfterWorkerStart;
use Server\Events\OnBeforeWorkerStart;
@@ -17,6 +18,7 @@ use Server\Events\OnWorkerStop;
use Server\ServerManager;
use Swoole\Server;
use Swoole\Timer;
use Server\Events\OnTaskerStart as OnTaskStart;
/**
@@ -42,9 +44,13 @@ class OnServerWorker extends \Server\Abstracts\Server
public function onWorkerStart(Server $server, int $workerId)
{
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
if ($workerId < $server->setting['worker_num']) {
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
} else {
$this->eventDispatch->dispatch(new OnTaskStart($server, $workerId));
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
}
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
}
+33
View File
@@ -0,0 +1,33 @@
<?php
namespace Server\Worker;
use Kiri\Abstracts\Config;
use Kiri\Kiri;
use Psr\EventDispatcher\EventDispatcherInterface;
use Server\ServerManager;
class OnTaskerStart extends WorkerStart implements EventDispatcherInterface
{
/**
* @throws \Kiri\Exception\ConfigException
* @throws \ReflectionException
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
ServerManager::setEnv('environmental', Kiri::TASK);
if (!is_enable_file_modification_listening()) {
$this->interpretDirectory();
}
$this->mixed($event, $isWorker, $time);
}
}
+19 -101
View File
@@ -14,111 +14,29 @@ use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException;
use Server\ServerManager;
class OnWorkerStart implements EventDispatcherInterface
class OnWorkerStart extends WorkerStart implements EventDispatcherInterface
{
/**
* @var Annotation
*/
#[Inject(Annotation::class)]
public Annotation $annotation;
/**
* @param object $event
* @return void
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
/**
* @var Router
*/
#[Inject(Router::class)]
public Router $router;
/**
* @param object $event
* @return void
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$time = microtime(true);
$this->interpretDirectory($isWorker);
if ($isWorker) {
ServerManager::setEnv('environmental', Kiri::WORKER);
Kiri::getDi()->get(Router::class)->_loader();
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else {
ServerManager::setEnv('environmental', Kiri::TASK);
$this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId));
}
$this->mixed($event, Config::get('id', 'system-service'), $isWorker, $time);
}
/**
* @param $event
* @param $name
* @param $isWorker
* @param $time
*/
private function mixed($event, $name, $isWorker, $time)
{
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);
}
/**
* @throws ReflectionException
* @throws Exception
*/
private function interpretDirectory($isWorker)
{
$di = Kiri::getDi();
// $namespace = array_filter(explode('\\', Router::getNamespace()));
//
// $namespace = APP_PATH . implode('/', $namespace);
$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);
}
}
}
}
ServerManager::setEnv('environmental', Kiri::WORKER);
if (!is_enable_file_modification_listening()) {
$this->router->read_files();
$this->interpretDirectory();
}
$this->mixed($event, $isWorker, $time);
}
}
+88
View File
@@ -0,0 +1,88 @@
<?php
namespace Server\Worker;
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);
}
}