2022-01-09 03:49:51 +08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2022-01-14 16:07:57 +08:00
|
|
|
use Kiri;
|
2022-01-09 03:49:51 +08:00
|
|
|
use Kiri\Abstracts\Config;
|
|
|
|
|
use Kiri\Abstracts\Providers;
|
|
|
|
|
use Kiri\Application;
|
2022-06-17 11:59:19 +08:00
|
|
|
use Kiri\Pool\Connection as PoolConnection;
|
2022-02-18 15:11:44 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2022-06-17 11:59:19 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
|
|
|
|
use Kiri\Annotation\Inject;
|
|
|
|
|
use Kiri\Server\Events\OnWorkerStart;
|
|
|
|
|
use Kiri\Server\Events\OnTaskerStart;
|
2022-06-17 12:05:37 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-01-09 03:49:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class DatabasesProviders
|
|
|
|
|
* @package Database
|
|
|
|
|
*/
|
|
|
|
|
class DatabasesProviders extends Providers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2022-06-17 11:59:19 +08:00
|
|
|
/**
|
|
|
|
|
* @var EventProvider
|
|
|
|
|
*/
|
|
|
|
|
#[Inject(EventProvider::class)]
|
|
|
|
|
public EventProvider $provider;
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
|
|
|
|
* @param Application $application
|
2022-02-17 18:31:33 +08:00
|
|
|
* @return void
|
2022-02-18 15:28:29 +08:00
|
|
|
* @throws ConfigException
|
2022-06-17 11:59:19 +08:00
|
|
|
* @throws Exception
|
2022-01-09 03:49:51 +08:00
|
|
|
*/
|
2022-06-17 11:59:19 +08:00
|
|
|
public function onImport(Application $application): void
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-02-18 15:28:29 +08:00
|
|
|
$databases = Config::get('databases.connections', []);
|
|
|
|
|
if (empty($databases)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-17 11:59:19 +08:00
|
|
|
$this->provider->on(OnWorkerStart::class, [$this, 'check']);
|
|
|
|
|
$this->provider->on(OnTaskerStart::class, [$this, 'check']);
|
2022-02-18 15:28:29 +08:00
|
|
|
foreach ($databases as $key => $database) {
|
2022-06-17 11:59:19 +08:00
|
|
|
$application->set($key, $this->_settings($database));
|
2022-02-18 15:28:29 +08:00
|
|
|
}
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return Connection
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function get($name): Connection
|
|
|
|
|
{
|
|
|
|
|
return Kiri::app()->get($name);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 11:59:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param OnTaskerStart|OnWorkerStart $start
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function check(OnTaskerStart|OnWorkerStart $start): void
|
|
|
|
|
{
|
2022-06-17 13:54:11 +08:00
|
|
|
$start->server->tick(50 * 1000, static function () use ($start) {
|
2022-06-17 11:59:19 +08:00
|
|
|
$databases = Config::get('databases.connections', []);
|
2022-06-17 13:54:11 +08:00
|
|
|
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
2022-06-17 14:04:23 +08:00
|
|
|
$logger->alert('db size ' . count($databases) . ' ticker ' . date('Y-m-d H:i:s'));
|
2022-06-17 13:54:11 +08:00
|
|
|
if (!empty($databases)) {
|
|
|
|
|
$valid = 0;
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
|
|
$connection = Kiri::getDi()->get(PoolConnection::class);
|
|
|
|
|
foreach ($databases as $database) {
|
|
|
|
|
$count += 1;
|
|
|
|
|
|
|
|
|
|
$success = $connection->check($database['cds']);
|
|
|
|
|
if ($success) {
|
|
|
|
|
$valid += 1;
|
|
|
|
|
}
|
|
|
|
|
if (isset($database['slaveConfig']) && isset($database['slaveConfig']['cds'])) {
|
|
|
|
|
if ($database['slaveConfig']['cds'] != $database['cds']) {
|
|
|
|
|
$count += 1;
|
|
|
|
|
$success = $connection->check($database['slaveConfig']['cds']);
|
|
|
|
|
if ($success) {
|
|
|
|
|
$valid += 1;
|
|
|
|
|
}
|
2022-06-17 12:05:37 +08:00
|
|
|
}
|
2022-06-17 11:59:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-17 12:05:37 +08:00
|
|
|
|
2022-06-17 13:54:11 +08:00
|
|
|
$message = sprintf('Worker %d db client has %d, valid %d', $start->workerId, $count, $valid);
|
|
|
|
|
$logger->alert($message);
|
|
|
|
|
}
|
2022-06-17 11:59:19 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:49:51 +08:00
|
|
|
/**
|
|
|
|
|
* @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'],
|
2022-02-18 15:28:29 +08:00
|
|
|
'class' => Connection::class,
|
2022-01-09 03:49:51 +08:00
|
|
|
'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']
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|