This commit is contained in:
2021-08-18 19:11:19 +08:00
parent 7a2f8e0e00
commit d2731aa8b9
2 changed files with 46 additions and 46 deletions
+32 -32
View File
@@ -5,20 +5,18 @@ namespace Server\Worker;
use Annotation\Annotation; use Annotation\Annotation;
use Annotation\Inject; use Annotation\Inject;
use Exception; use Exception;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Kiri\Runtime;
use ReflectionException; use ReflectionException;
use Server\Constant;
use Server\Events\OnAfterWorkerStart; use Server\Events\OnAfterWorkerStart;
use Server\Events\OnWorkerError; use Server\Events\OnWorkerError;
use Server\Events\OnWorkerExit; use Server\Events\OnWorkerExit;
use Server\Events\OnWorkerStart; use Server\Events\OnWorkerStart;
use Server\Events\OnWorkerStop; use Server\Events\OnWorkerStop;
use Kiri\Abstracts\Config;
use Kiri\Core\Help;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Runtime;
use Kiri\Kiri;
use Swoole\Server; use Swoole\Server;
use Swoole\Timer; use Swoole\Timer;
@@ -45,18 +43,37 @@ class OnServerWorker extends \Server\Abstracts\Server
*/ */
public function onWorkerStart(Server $server, int $workerId) public function onWorkerStart(Server $server, int $workerId)
{ {
$this->_setConfigCache($workerId); $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->onWorkerStartInit($server, $workerId);
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
}
/**
* @param Server $server
* @param int $workerId
* @throws ConfigException
* @throws ReflectionException
* @throws Exception
*/
private function onWorkerStartInit(Server $server, int $workerId)
{
putenv('state=start');
putenv('worker=' . $workerId);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (!empty($serialize)) {
Config::sets(unserialize($serialize));
}
$annotation = Kiri::app()->getAnnotation(); $annotation = Kiri::app()->getAnnotation();
$annotation->read(APP_PATH . 'app', 'App', $annotation->read(APP_PATH . 'app', 'App',
$workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH] $workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH]
); );
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
$this->workerInitExecutor($server, $annotation, $workerId); $this->workerInitExecutor($server, $annotation, $workerId);
// $this->interpretDirectory($annotation); $this->interpretDirectory($annotation);
$this->eventDispatch->dispatch(new OnAfterWorkerStart());
} }
@@ -113,23 +130,6 @@ class OnServerWorker extends \Server\Abstracts\Server
} }
/**
* @param $worker_id
* @throws Exception
*/
private function _setConfigCache($worker_id)
{
putenv('state=start');
putenv('worker=' . $worker_id);
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
if (empty($serialize)) {
return;
}
Config::sets(unserialize($serialize));
}
/** /**
* @param Server $server * @param Server $server
* @param int $workerId * @param int $workerId
+14 -14
View File
@@ -548,20 +548,20 @@ class Router extends HttpService implements RouterInterface
public function _loader() public function _loader()
{ {
$this->loadRouteDir(APP_PATH . 'routes'); $this->loadRouteDir(APP_PATH . 'routes');
// $classes = Kiri::getAnnotation()->runtime(CONTROLLER_PATH); $classes = Kiri::getAnnotation()->runtime(CONTROLLER_PATH);
//
// $di = Kiri::getDi(); $di = Kiri::getDi();
// foreach ($classes as $class) { foreach ($classes as $class) {
// $methods = $di->getMethodAttribute($class); $methods = $di->getMethodAttribute($class);
// foreach ($methods as $method => $attribute) { foreach ($methods as $method => $attribute) {
// if (empty($attribute)) { if (empty($attribute)) {
// continue; continue;
// } }
// foreach ($attribute as $item) { foreach ($attribute as $item) {
// $item->execute($class, $method); $item->execute($class, $method);
// } }
// } }
// } }
} }
/** /**