diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index c1cb7687..f84702ee 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -11,8 +11,6 @@ use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Runtime; use Snowflake\Snowflake; -use Swoole\Coroutine; -use Swoole\Coroutine\System; use Swoole\Server; /** @@ -23,84 +21,92 @@ class OnWorkerStart extends Callback { - /** - * @param Server $server - * @param int $worker_id - * - * @return mixed - * @throws Exception - */ - public function onHandler(Server $server, int $worker_id): void - { - putenv('state=start'); - putenv('worker=' . $worker_id); + /** + * @param Server $server + * @param int $worker_id + * + * @return mixed + * @throws Exception + */ + public function onHandler(Server $server, int $worker_id): void + { + $this->setConfigs($worker_id); - Config::sets(sweep()); - - $annotation = Snowflake::app()->getAnnotation(); - $annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME)))); - if ($worker_id >= $server->setting['worker_num']) { - $annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]); - $this->onTask($server, $annotation); - } else { - $annotation->runtime(CONTROLLER_PATH); - $annotation->runtime(directory('app'), [CONTROLLER_PATH]); - $this->onWorker($server, $annotation); - } - } + $annotation = Snowflake::app()->getAnnotation(); + $annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME)))); + if ($worker_id >= $server->setting['worker_num']) { + $this->onTask($server, $annotation); + } else { + $this->onWorker($server, $annotation); + } + } - /** - * @param Server $server - * @param int $worker_id - * @return bool - */ - private function isWorker(Server $server, int $worker_id): bool - { - return $worker_id < $server->setting['worker_num']; - } + /** + * @param $worker_id + * @throws Exception + */ + private function setConfigs($worker_id) + { + putenv('state=start'); + putenv('worker=' . $worker_id); + + $serialize = file_get_contents(Runtime::CONFIG_NAME); + + Config::sets(unserialize($serialize)); + } - /** - * @param Server $server - * @param Annotation $annotation - * @throws ConfigException - * @throws Exception - */ - public function onTask(Server $server, Annotation $annotation) - { - putenv('environmental=' . Snowflake::TASK); - - $annotation->runtime(directory('app'), [ - CONTROLLER_PATH, - TASK_PATH, - ]); - name($server->worker_pid, 'Task#' . $server->worker_id); - - Snowflake::setTaskId($server->worker_pid); - - fire(Event::SERVER_TASK_START); - } + /** + * @param Server $server + * @param int $worker_id + * @return bool + */ + private function isWorker(Server $server, int $worker_id): bool + { + return $worker_id < $server->setting['worker_num']; + } - /** - * @param Server $server - * @param Annotation $annotation - * @throws Exception - */ - public function onWorker(Server $server, Annotation $annotation) - { - name($server->worker_pid, 'Worker#' . $server->worker_id); + /** + * @param Server $server + * @param Annotation $annotation + * @throws ConfigException + * @throws Exception + */ + public function onTask(Server $server, Annotation $annotation) + { + putenv('environmental=' . Snowflake::TASK); - $time = microtime(true); - $annotation->runtime(CONTROLLER_PATH); - $this->debug('use time.' . (microtime(true) - $time)); - $annotation->runtime(directory('app'), CONTROLLER_PATH); + $annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]); - Snowflake::setWorkerId($server->worker_pid); - putenv('environmental=' . Snowflake::WORKER); + name($server->worker_pid, 'Task#' . $server->worker_id); - fire(Event::SERVER_WORKER_START, [getenv('worker')]); - } + Snowflake::setTaskId($server->worker_pid); + + fire(Event::SERVER_TASK_START); + } + + + /** + * @param Server $server + * @param Annotation $annotation + * @throws Exception + */ + public function onWorker(Server $server, Annotation $annotation) + { + name($server->worker_pid, 'Worker#' . $server->worker_id); + + $time = microtime(true); + + $annotation->runtime(CONTROLLER_PATH); + $this->debug('use time.' . (microtime(true) - $time)); + $annotation->runtime(directory('app'), [CONTROLLER_PATH]); + + Snowflake::setWorkerId($server->worker_pid); + putenv('environmental=' . Snowflake::WORKER); + + fire(Event::SERVER_WORKER_START, [getenv('worker')]); + } } diff --git a/System/Runtime.php b/System/Runtime.php index dfb37bc8..27196677 100644 --- a/System/Runtime.php +++ b/System/Runtime.php @@ -23,6 +23,7 @@ class Runtime extends Command const CACHE_NAME = '.runtime.cache'; + const CONFIG_NAME = '.config.cache'; /** @@ -36,6 +37,7 @@ class Runtime extends Command $runtime = storage(static::CACHE_NAME); + Snowflake::writeFile(storage(static::CONFIG_NAME), serialize(sweep())); Snowflake::writeFile($runtime, serialize($annotation->getLoader())); }