Files
kiri-core/http-server/Worker/OnWorkerStart.php
T

125 lines
2.8 KiB
PHP
Raw Normal View History

2021-08-29 01:18:42 +08:00
<?php
namespace Server\Worker;
use Annotation\Annotation;
2021-08-29 04:44:33 +08:00
use Annotation\Inject;
2021-08-29 01:18:42 +08:00
use Exception;
2021-08-29 04:44:33 +08:00
use Http\Route\Router;
2021-08-29 01:18:42 +08:00
use Kiri\Abstracts\Config;
2021-09-04 00:11:53 +08:00
use Kiri\Di\NoteManager;
2021-08-29 01:18:42 +08:00
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException;
2021-09-04 00:08:34 +08:00
use Server\ServerManager;
2021-08-29 01:18:42 +08:00
class OnWorkerStart implements EventDispatcherInterface
{
2021-09-06 14:20:04 +08:00
/**
* @var Annotation
*/
2021-09-06 14:30:20 +08:00
#[Inject(Annotation::class)]
public Annotation $annotation;
2021-08-29 01:18:42 +08:00
2021-09-06 14:20:04 +08:00
/**
* @var Router
*/
2021-09-06 14:30:20 +08:00
#[Inject(Router::class)]
public Router $router;
2021-08-29 01:18:42 +08:00
2021-09-06 14:20:04 +08:00
/**
* @param object $event
* @return void
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
2021-09-06 14:30:20 +08:00
public function dispatch(object $event)
{
$isWorker = $event->workerId < $event->server->setting['worker_num'];
2021-09-06 16:24:59 +08:00
$time = microtime(true);
2021-09-06 16:22:15 +08:00
$this->interpretDirectory($isWorker);
2021-09-06 14:30:20 +08:00
if ($isWorker) {
ServerManager::setEnv('environmental', Kiri::WORKER);
Kiri::getFactory()->getRouter()->_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));
}
2021-09-06 17:04:16 +08:00
$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)
{
2021-09-06 16:51:21 +08:00
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',
2021-09-06 16:25:45 +08:00
$event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL;
2021-09-06 14:30:20 +08:00
}
/**
* @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
*/
2021-09-06 16:22:15 +08:00
private function interpretDirectory($isWorker)
2021-09-06 14:30:20 +08:00
{
$di = Kiri::getDi();
2021-09-06 16:22:15 +08:00
$namespace = array_filter(explode('\\', Router::getNamespace()));
$namespace = APP_PATH . implode('/', $namespace);
$this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [$namespace]);
2021-09-06 14:30:20 +08:00
$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);
}
}
}
}
2021-08-29 01:18:42 +08:00
}