diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php index 899f944d..5bef5f09 100644 --- a/HttpServer/Abstracts/Callback.php +++ b/HttpServer/Abstracts/Callback.php @@ -4,13 +4,17 @@ declare(strict_types=1); namespace HttpServer\Abstracts; +use Database\Connection; use Exception; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; +use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Error\Logger; use Snowflake\Event; +use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\Coroutine\Server; use Swoole\Timer; @@ -132,4 +136,39 @@ abstract class Callback extends HttpService } } + + /** + * @throws ConfigException + * @throws ComponentException + * @throws Exception + */ + protected function clearMysqlClient() + { + $databases = Config::get('databases', false, []); + if (empty($databases)) { + return; + } + $application = Snowflake::app(); + foreach ($databases as $name => $database) { + /** @var Connection $connection */ + $connection = $application->get('databases.' . $name, false); + if (empty($connection)) { + continue; + } + $connection->disconnect(); + } + } + + + /** + * @throws ConfigException + * @throws ComponentException + * @throws Exception + */ + protected function clearRedisClient() + { + $redis = Snowflake::app()->getRedis(); + $redis->destroy(); + } + } diff --git a/HttpServer/Events/OnBeforeReload.php b/HttpServer/Events/OnBeforeReload.php index 34e52064..bb0046eb 100644 --- a/HttpServer/Events/OnBeforeReload.php +++ b/HttpServer/Events/OnBeforeReload.php @@ -25,6 +25,9 @@ class OnBeforeReload extends Callback { $event = Snowflake::app()->getEvent(); $event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]); + + $this->clearMysqlClient(); + $this->clearRedisClient(); } } diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index d953c3a7..99a36fe1 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -146,19 +146,13 @@ abstract class Pool extends Component /** * @param $name * @param mixed $callback - * @throws ComponentException * @throws Exception */ private function createByCallback($name, mixed $callback) { -// if ($this->creates === -1 && !is_callable($callback)) { -// $this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); -// -// $event = Snowflake::app()->getEvent(); -// $event->on(Event::SERVER_WORKER_STOP, function () use ($creates) { -// Timer::clear($creates); -// }); -// } + if ($this->creates === -1 && !is_callable($callback)) { + $this->creates = $creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); + } if (!Context::hasContext('create::client::ing::' . $name)) { $this->push($name, $this->createClient($name, $callback)); Context::remove('create::client::ing::' . $name); @@ -283,6 +277,7 @@ abstract class Pool extends Component */ public function clean(string $name) { + Timer::clear($this->creates); if (!Context::inCoroutine() || !isset($this->_items[$name])) { return; } diff --git a/System/Di/Service.php b/System/Di/Service.php index e60116c2..52d3e53d 100644 --- a/System/Di/Service.php +++ b/System/Di/Service.php @@ -24,127 +24,132 @@ use Snowflake\Snowflake; class Service extends Component { - private array $_components = []; + private array $_components = []; - private array $_definition = []; + private array $_definition = []; - protected array $_alias = []; + protected array $_alias = []; - /** - * @param $id - * - * @return mixed - * @throws - */ - public function get($id): mixed - { - if (isset($this->_components[$id])) { - return $this->_components[$id]; - } - if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) { - throw new ComponentException("Unknown component ID: $id"); - } - if (isset($this->_definition[$id])) { - $config = $this->_definition[$id]; - if (is_object($config)) { - return $this->_components[$id] = $config; - } - $object = Snowflake::createObject($config); - } else { - $config = $this->_alias[$id]; + /** + * @param $id + * @param bool $try + * @return mixed + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + */ + public function get($id, $try = true): mixed + { + if (isset($this->_components[$id])) { + return $this->_components[$id]; + } + if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) { + if ($try === false) { + return null; + } + throw new ComponentException("Unknown component ID: $id"); + } + if (isset($this->_definition[$id])) { + $config = $this->_definition[$id]; + if (is_object($config)) { + return $this->_components[$id] = $config; + } + $object = Snowflake::createObject($config); + } else { + $config = $this->_alias[$id]; - $object = Snowflake::createObject($config); - } - return $this->_components[$id] = $object; - } + $object = Snowflake::createObject($config); + } + return $this->_components[$id] = $object; + } - /** - * @param string $className - * @param string $alias - */ - public function setAlias(string $className, string $alias) - { - $this->_alias[$className] = $alias; - } + /** + * @param string $className + * @param string $alias + */ + public function setAlias(string $className, string $alias) + { + $this->_alias[$className] = $alias; + } - /** - * @param $id - * @param $definition - * - * @return mixed - * @throws ComponentException - * @throws ReflectionException - * @throws NotFindClassException - */ - public function set($id, $definition): mixed - { - if ($definition === NULL) { - return $this->remove($id); - } - unset($this->_components[$id]); - if (is_object($definition) || is_callable($definition, TRUE)) { - return $this->_definition[$id] = $definition; - } else if (!is_array($definition)) { - throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); - } - if (!isset($definition['class'])) { - throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element."); - } else { - $this->_definition[$id] = $definition; - } - return $this->get($id); - } + /** + * @param $id + * @param $definition + * + * @return mixed + * @throws ComponentException + * @throws ReflectionException + * @throws NotFindClassException + */ + public function set($id, $definition): mixed + { + if ($definition === NULL) { + return $this->remove($id); + } + unset($this->_components[$id]); + if (is_object($definition) || is_callable($definition, TRUE)) { + return $this->_definition[$id] = $definition; + } else if (!is_array($definition)) { + throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); + } + if (!isset($definition['class'])) { + throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element."); + } else { + $this->_definition[$id] = $definition; + } + return $this->get($id); + } - /** - * @param $id - * @return bool - */ - public function has($id): bool - { - return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]); - } + /** + * @param $id + * @return bool + */ + public function has($id): bool + { + return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]); + } - /** - * @param array $data - * @throws Exception - */ - public function setComponents(array $data) - { - foreach ($data as $key => $val) { - $this->set($key, $val); - } - } + /** + * @param array $data + * @throws Exception + */ + public function setComponents(array $data) + { + foreach ($data as $key => $val) { + $this->set($key, $val); + } + } - /** - * @param $name - * @return mixed - * @throws Exception - */ - public function __get($name): mixed - { - if ($this->has($name)) { - return $this->get($name); - } + /** + * @param $name + * @return mixed + * @throws Exception + */ + public function __get($name): mixed + { + if ($this->has($name)) { + return $this->get($name); + } - return parent::__get($name); - } + return parent::__get($name); + } - /** - * @param $id - * @return bool - */ - public function remove($id): bool - { - unset($this->_components[$id]); - unset($this->_definition[$id]); - if (isset($this->_alias[$id])) { - unset($this->_components[$this->_alias[$id]]); - unset($this->_definition[$this->_alias[$id]]); - unset($this->_alias[$id]); - } - return $this->has($id); - } + /** + * @param $id + * @return bool + */ + public function remove($id): bool + { + unset($this->_components[$id]); + unset($this->_definition[$id]); + if (isset($this->_alias[$id])) { + unset($this->_components[$this->_alias[$id]]); + unset($this->_definition[$this->_alias[$id]]); + unset($this->_alias[$id]); + } + return $this->has($id); + } }