diff --git a/http-helper/Command.php b/http-helper/Command.php index b82c0235..3168ca6f 100644 --- a/http-helper/Command.php +++ b/http-helper/Command.php @@ -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(); diff --git a/http-server/Events/OnBeforeWorkerStart.php b/http-server/Events/OnBeforeWorkerStart.php new file mode 100644 index 00000000..1018e7e6 --- /dev/null +++ b/http-server/Events/OnBeforeWorkerStart.php @@ -0,0 +1,12 @@ +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)); - } } diff --git a/http-server/Worker/OnWorkerStart.php b/http-server/Worker/OnWorkerStart.php index 520ba25c..4e698f96 100644 --- a/http-server/Worker/OnWorkerStart.php +++ b/http-server/Worker/OnWorkerStart.php @@ -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]);