From bc728071c72b06a2f48204a91dad88fe765564e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 11 Jul 2022 14:15:21 +0800 Subject: [PATCH] modify plugin name --- DatabasesProviders.php | 236 ++++++++++++++++++++--------------------- Mysql/PDO.php | 2 - 2 files changed, 118 insertions(+), 120 deletions(-) diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 16557ec..1aac0da 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -27,140 +27,140 @@ class DatabasesProviders extends Providers { - public int $timer = 30000; + public int $timer = 30000; - /** - * @var EventProvider - */ - #[Inject(EventProvider::class)] - public EventProvider $provider; + /** + * @var EventProvider + */ + #[Inject(EventProvider::class)] + public EventProvider $provider; - /** - * @param LocalService $application - * @return void - * @throws ConfigException - * @throws Exception - */ - public function onImport(LocalService $application): void - { - $databases = Config::get('databases.connections', []); - if (empty($databases)) { - return; - } - $this->provider->on(OnWorkerStart::class, [$this, 'check']); - $this->provider->on(OnTaskerStart::class, [$this, 'check']); - $this->provider->on(OnWorkerExit::class, [$this, 'exit'], 9999); - foreach ($databases as $key => $database) { - $application->set($key, $this->_settings($database)); - } - } + /** + * @param LocalService $application + * @return void + * @throws ConfigException + * @throws Exception + */ + public function onImport(LocalService $application): void + { + $databases = Config::get('databases.connections', []); + if (empty($databases)) { + return; + } + $this->provider->on(OnWorkerStart::class, [$this, 'check']); + $this->provider->on(OnTaskerStart::class, [$this, 'check']); + $this->provider->on(OnWorkerExit::class, [$this, 'exit'], 9999); + foreach ($databases as $key => $database) { + $application->set($key, $this->_settings($database)); + } + } - /** - * @param OnWorkerExit $exit - * @return void - * @throws ConfigException - * @throws Exception - */ - public function exit(OnWorkerExit $exit): void - { - Timer::clearAll(); - $databases = Config::get('databases.connections', []); - if (!empty($databases)) { - $connection = Kiri::getDi()->get(PoolConnection::class); - foreach ($databases as $database) { - $connection->disconnect($database['cds']); - } - } - } + /** + * @param OnWorkerExit $exit + * @return void + * @throws ConfigException + * @throws Exception + */ + public function exit(OnWorkerExit $exit): void + { + Timer::clearAll(); + $databases = Config::get('databases.connections', []); + if (!empty($databases)) { + $connection = Kiri::getDi()->get(PoolConnection::class); + foreach ($databases as $database) { + $connection->disconnect($database['cds']); + } + } + } - /** - * @param $name - * @return Connection - * @throws Exception - */ - public function get($name): Connection - { - return Kiri::service()->get($name); - } + /** + * @param $name + * @return Connection + * @throws Exception + */ + public function get($name): Connection + { + return Kiri::service()->get($name); + } - /** - * @param OnTaskerStart|OnWorkerStart $start - * @return void - */ - public function check(OnTaskerStart|OnWorkerStart $start): void - { - Timer::tick($this->timer, static function () use ($start) { - $valid = $count = 0; - $logger = Kiri::getDi()->get(LoggerInterface::class); - $databases = Config::get('databases.connections', []); - if (!empty($databases)) { - DatabasesProviders::each($databases, $logger, $count, $valid); - } - $const = 'Worker %d db client has %d, valid %d'; - $logger->alert(sprintf($const, $start->workerId, $count, $valid)); - }); - } + /** + * @param OnTaskerStart|OnWorkerStart $start + * @return void + */ + public function check(OnTaskerStart|OnWorkerStart $start): void + { + Timer::tick(60000, static function () use ($start) { + $valid = $count = 0; + $logger = Kiri::getDi()->get(LoggerInterface::class); + $databases = Config::get('databases.connections', []); + if (!empty($databases)) { + DatabasesProviders::each($databases, $logger, $count, $valid); + } + $const = 'Worker %d db client has %d, valid %d'; + $logger->alert(sprintf($const, $start->workerId, $count, $valid)); + }); + } - /** - * @param $databases - * @param LoggerInterface $logger - * @param $count - * @param $valid - * @return void - */ - public static function each($databases, LoggerInterface $logger, &$count, &$valid): void - { - $connection = Kiri::getDi()->get(PoolConnection::class); - foreach ($databases as $database) { - try { - [$total, $success] = $connection->check($database['cds']); + /** + * @param $databases + * @param LoggerInterface $logger + * @param $count + * @param $valid + * @return void + */ + public static function each($databases, LoggerInterface $logger, &$count, &$valid): void + { + $connection = Kiri::getDi()->get(PoolConnection::class); + foreach ($databases as $database) { + try { + [$total, $success] = $connection->check($database['cds']); - $count += $total; - $valid += $success; + $count += $total; + $valid += $success; - if (isset($database['slaveConfig']) && isset($database['slaveConfig']['cds'])) { - if ($database['slaveConfig']['cds'] != $database['cds']) { - [$total, $success] = $connection->check($database['slaveConfig']['cds']); + if (isset($database['slaveConfig']) && isset($database['slaveConfig']['cds'])) { + if ($database['slaveConfig']['cds'] != $database['cds']) { + [$total, $success] = $connection->check($database['slaveConfig']['cds']); - $count += $total; - $valid += $success; - } - } - } catch (\Throwable $throwable) { - $logger->error($throwable->getMessage()); - } - } - } + $count += $total; + $valid += $success; + } + } + } catch (\Throwable $throwable) { + $logger->error($throwable->getMessage()); + } + } + } - /** - * @param $database - * @return array - */ - private function _settings($database): array - { - $clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60]; - return [ - 'id' => $database['id'], - 'cds' => $database['cds'], - 'class' => Connection::class, - 'username' => $database['username'], - 'password' => $database['password'], - 'tablePrefix' => $database['tablePrefix'], - 'database' => $database['database'], - 'connect_timeout' => $database['connect_timeout'] ?? 30, - 'read_timeout' => $database['read_timeout'] ?? 10, - 'pool' => $clientPool, - 'attributes' => $database['attributes'] ?? [], - 'charset' => $database['charset'] ?? 'utf8mb4', - 'slaveConfig' => $database['slaveConfig'] - ]; - } + /** + * @param $database + * @return array + */ + private function _settings($database): array + { + $clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60]; + return [ + 'id' => $database['id'], + 'cds' => $database['cds'], + 'class' => Connection::class, + 'username' => $database['username'], + 'password' => $database['password'], + 'tablePrefix' => $database['tablePrefix'], + 'database' => $database['database'], + 'connect_timeout' => $database['connect_timeout'] ?? 30, + 'read_timeout' => $database['read_timeout'] ?? 10, + 'pool' => $clientPool, + 'attributes' => $database['attributes'] ?? [], + 'charset' => $database['charset'] ?? 'utf8mb4', + 'slaveConfig' => $database['slaveConfig'] + ]; + } } diff --git a/Mysql/PDO.php b/Mysql/PDO.php index 7882a5b..28101b0 100644 --- a/Mysql/PDO.php +++ b/Mysql/PDO.php @@ -4,12 +4,10 @@ namespace Database\Mysql; use Exception; use Kiri; -use Kiri\Abstracts\Config; use Kiri\Events\EventProvider; use Kiri\Pool\StopHeartbeatCheck; use Kiri\Server\Events\OnWorkerExit; use PDOStatement; -use Swoole\Timer; /** *