Files
kiri-databases/DatabasesProviders.php
T

146 lines
3.7 KiB
PHP
Raw Normal View History

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-06-17 14:11:26 +08:00
use Kiri\Server\Events\OnWorkerExit;
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-06-17 14:11:26 +08:00
$this->provider->on(OnWorkerExit::class, [$this, 'exit']);
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
}
2022-06-17 14:11:26 +08:00
/**
* @param OnWorkerExit $exit
* @return void
*/
public function exit(OnWorkerExit $exit): void
{
$id = (int)Kiri\Context::getContext('db.loop.id');
if (!empty($id)) {
$exit->server->clearTimer($id);
}
}
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 14:11:26 +08:00
$timerTick = $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 14:25:29 +08:00
$valid = 0;
$count = 0;
2022-06-17 13:54:11 +08:00
$logger = Kiri::getDi()->get(LoggerInterface::class);
if (!empty($databases)) {
$connection = Kiri::getDi()->get(PoolConnection::class);
foreach ($databases as $database) {
2022-06-17 14:25:29 +08:00
try {
[$total, $success] = $connection->check($database['cds']);
2022-06-17 14:07:16 +08:00
2022-06-17 14:25:29 +08:00
$count += $total;
$valid += $success;
2022-06-17 13:54:11 +08:00
2022-06-17 14:25:29 +08:00
if (isset($database['slaveConfig']) && isset($database['slaveConfig']['cds'])) {
if ($database['slaveConfig']['cds'] != $database['cds']) {
[$total, $success] = $connection->check($database['slaveConfig']['cds']);
2022-06-17 14:07:16 +08:00
2022-06-17 14:25:29 +08:00
$count += $total;
$valid += $success;
}
2022-06-17 12:05:37 +08:00
}
2022-06-17 14:25:29 +08:00
} catch (\Throwable $throwable) {
$logger->error($throwable->getMessage());
2022-06-17 11:59:19 +08:00
}
}
2022-06-17 13:54:11 +08:00
}
2022-06-17 14:25:29 +08:00
$logger->alert(sprintf('Worker %d db client has %d, valid %d', $start->workerId, $count, $valid));
2022-06-17 11:59:19 +08:00
});
2022-06-17 14:11:26 +08:00
Kiri\Context::setContext('db.loop.id', $timerTick);
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']
];
}
}