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

101 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 Kiri\Runtime;
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-08-29 04:44:33 +08:00
#[Inject(Annotation::class)]
2021-08-29 01:18:42 +08:00
public Annotation $annotation;
2021-08-29 04:44:33 +08:00
#[Inject(Router::class)]
public Router $router;
2021-08-29 01:18:42 +08:00
/**
* @param object $event
* @return object|void
* @throws \Kiri\Exception\ConfigException
* @throws \ReflectionException
*/
public function dispatch(object $event)
{
2021-08-29 04:44:33 +08:00
$isWorker = $event->workerId < $event->server->setting['worker_num'];
2021-08-29 01:18:42 +08:00
2021-09-04 00:08:34 +08:00
$this->annotation->read(APP_PATH . 'app', 'App', $isWorker ? [] : [CONTROLLER_PATH]);
2021-08-29 04:44:33 +08:00
$this->interpretDirectory();
if ($isWorker) {
2021-09-04 00:08:34 +08:00
ServerManager::setEnv('environmental', Kiri::WORKER);
2021-08-29 05:59:10 +08:00
Kiri::getFactory()->getRouter()->_loader();
2021-08-29 01:18:42 +08:00
2021-08-29 04:44:33 +08:00
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
2021-08-29 01:18:42 +08:00
2021-08-29 04:44:33 +08:00
$this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId));
} else {
2021-09-04 00:08:34 +08:00
ServerManager::setEnv('environmental', Kiri::TASK);
2021-08-29 01:18:42 +08:00
2021-08-29 04:44:33 +08:00
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $event->server->worker_pid, $event->workerId) . PHP_EOL;
2021-08-29 04:31:14 +08:00
2021-08-29 04:44:33 +08:00
$this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId));
2021-08-29 01:18:42 +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
*/
private function interpretDirectory()
{
$fileLists = $this->annotation->runtime(APP_PATH . 'app');
$di = Kiri::getDi();
foreach ($fileLists as $class) {
2021-09-04 00:11:53 +08:00
foreach (NoteManager::getTargetNote($class) as $value) {
2021-08-29 05:53:47 +08:00
$value->execute($class);
2021-08-29 01:18:42 +08:00
}
$methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) {
if (empty($attribute)) {
continue;
}
foreach ($attribute as $item) {
2021-08-29 05:53:47 +08:00
$item->execute($class, $method);
2021-08-29 01:18:42 +08:00
}
}
}
}
}