111
This commit is contained in:
@@ -10,6 +10,8 @@ use Kiri\Abstracts\Input;
|
|||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnBeforeWorkerStart;
|
||||||
|
use Server\Worker\OnServerWorker;
|
||||||
use Server\Worker\OnWorkerStart as WorkerDispatch;
|
use Server\Worker\OnWorkerStart as WorkerDispatch;
|
||||||
use Server\Events\OnWorkerStart;
|
use Server\Events\OnWorkerStart;
|
||||||
|
|
||||||
@@ -70,6 +72,7 @@ class Command extends \Console\Command
|
|||||||
{
|
{
|
||||||
exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder');
|
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']);
|
$this->eventProvider->on(OnWorkerStart::class, [di(WorkerDispatch::class), 'dispatch']);
|
||||||
|
|
||||||
return $manager->start();
|
return $manager->start();
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Server\Events;
|
||||||
|
|
||||||
|
class OnBeforeWorkerStart
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(public int $workerId)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ use Kiri\Runtime;
|
|||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Events\OnAfterRequest;
|
use Server\Events\OnAfterRequest;
|
||||||
use Server\Events\OnAfterWorkerStart;
|
use Server\Events\OnAfterWorkerStart;
|
||||||
|
use Server\Events\OnBeforeReload;
|
||||||
|
use Server\Events\OnBeforeWorkerStart;
|
||||||
use Server\Events\OnWorkerError;
|
use Server\Events\OnWorkerError;
|
||||||
use Server\Events\OnWorkerExit;
|
use Server\Events\OnWorkerExit;
|
||||||
use Server\Events\OnWorkerStart;
|
use Server\Events\OnWorkerStart;
|
||||||
@@ -44,93 +46,26 @@ class OnServerWorker extends \Server\Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onWorkerStart(Server $server, int $workerId)
|
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());
|
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param $workerId
|
||||||
* @param int $workerId
|
* @throws \Exception
|
||||||
* @throws ConfigException
|
|
||||||
* @throws ReflectionException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
private function onWorkerStartInit(Server $server, int $workerId)
|
public function setConfigure(OnBeforeWorkerStart $worker)
|
||||||
{
|
{
|
||||||
putenv('state=start');
|
putenv('state=start');
|
||||||
putenv('worker=' . $workerId);
|
putenv('worker=' . $worker->workerId);
|
||||||
|
|
||||||
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
||||||
if (!empty($serialize)) {
|
if (!empty($serialize)) {
|
||||||
Config::sets(unserialize($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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use Kiri\Kiri;
|
|||||||
use Kiri\Runtime;
|
use Kiri\Runtime;
|
||||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Events\OnWorkerStart as WorkerStart;
|
|
||||||
|
|
||||||
class OnWorkerStart implements EventDispatcherInterface
|
class OnWorkerStart implements EventDispatcherInterface
|
||||||
{
|
{
|
||||||
@@ -34,13 +33,6 @@ class OnWorkerStart implements EventDispatcherInterface
|
|||||||
*/
|
*/
|
||||||
public function dispatch(object $event)
|
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'];
|
$isWorker = $event->workerId < $event->server->setting['worker_num'];
|
||||||
|
|
||||||
$this->annotation->read(APP_PATH . 'app','App', $isWorker ? [] : [CONTROLLER_PATH]);
|
$this->annotation->read(APP_PATH . 'app','App', $isWorker ? [] : [CONTROLLER_PATH]);
|
||||||
|
|||||||
Reference in New Issue
Block a user