This commit is contained in:
as2252258@163.com
2021-08-29 04:57:50 +08:00
parent 5b2e91b1ed
commit ff99cdbf61
4 changed files with 23 additions and 81 deletions
+3
View File
@@ -10,6 +10,8 @@ use Kiri\Abstracts\Input;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Server\Events\OnBeforeWorkerStart;
use Server\Worker\OnServerWorker;
use Server\Worker\OnWorkerStart as WorkerDispatch;
use Server\Events\OnWorkerStart;
@@ -70,6 +72,7 @@ class Command extends \Console\Command
{
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
$this->eventProvider->on(OnBeforeWorkerStart::class, [di(OnServerWorker::class), 'setConfigure']);
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
return $manager->start();
@@ -0,0 +1,12 @@
<?php
namespace Server\Events;
class OnBeforeWorkerStart
{
public function __construct(public int $workerId)
{
}
}
+8 -73
View File
@@ -14,6 +14,8 @@ use Kiri\Runtime;
use ReflectionException;
use Server\Events\OnAfterRequest;
use Server\Events\OnAfterWorkerStart;
use Server\Events\OnBeforeReload;
use Server\Events\OnBeforeWorkerStart;
use Server\Events\OnWorkerError;
use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStart;
@@ -44,93 +46,26 @@ class OnServerWorker extends \Server\Abstracts\Server
*/
public function onWorkerStart(Server $server, int $workerId)
{
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId));
// $this->onWorkerStartInit($server, $workerId);
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @param Server $server
* @param int $workerId
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
* @param $workerId
* @throws \Exception
*/
private function onWorkerStartInit(Server $server, int $workerId)
public function setConfigure(OnBeforeWorkerStart $worker)
{
putenv('state=start');
putenv('worker=' . $workerId);
putenv('worker=' . $worker->workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) {
Config::sets(unserialize($serialize));
}
if (is_enable_file_modification_listening()) {
$annotation = Kiri::app()->getAnnotation();
$annotation->read(APP_PATH . 'app', 'App',
$workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH]
);
}
$this->interpretDirectory($annotation);
$this->workerInitExecutor($server, $workerId);
}
/**
* @param Annotation $annotation
* @throws ReflectionException
* @throws Exception
*/
private function interpretDirectory(Annotation $annotation)
{
$fileLists = $annotation->runtime(APP_PATH . 'app');
$di = Kiri::getDi();
foreach ($fileLists as $class) {
foreach ($di->getTargetNote($class) as $value) {
$value['class']::execute((object)$value['params'], $class);
}
$methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) {
if (empty($attribute)) {
continue;
}
foreach ($attribute as $item) {
$item['class']::execute((object)$item['params'], $class, $method);
}
}
}
}
/**
* @param Server $server
* @param Annotation $annotation
* @param int $workerId
* @throws ConfigException
* @throws Exception
*/
private function workerInitExecutor(Server $server, int $workerId)
{
if ($workerId < $server->setting['worker_num']) {
putenv('environmental=' . Kiri::WORKER);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId));
if (is_enable_file_modification_listening()) {
$loader = Kiri::app()->getRouter();
$loader->_loader();
}
} else {
putenv('environmental=' . Kiri::TASK);
echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Tasker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL;
$this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId));
}
}
-8
View File
@@ -12,7 +12,6 @@ use Kiri\Kiri;
use Kiri\Runtime;
use Psr\EventDispatcher\EventDispatcherInterface;
use ReflectionException;
use Server\Events\OnWorkerStart as WorkerStart;
class OnWorkerStart implements EventDispatcherInterface
{
@@ -34,13 +33,6 @@ class OnWorkerStart implements EventDispatcherInterface
*/
public function dispatch(object $event)
{
putenv('state=start');
putenv('worker=' . $event->workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) {
Config::sets(unserialize($serialize));
}
$isWorker = $event->workerId < $event->server->setting['worker_num'];
$this->annotation->read(APP_PATH . 'app','App', $isWorker ? [] : [CONTROLLER_PATH]);