Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
This commit is contained in:
134
src/Framework/Redis/RedisConnection.php
Normal file
134
src/Framework/Redis/RedisConnection.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Framework\Redis;
|
||||
|
||||
use Redis;
|
||||
use RedisException;
|
||||
|
||||
/**
|
||||
* Managed Redis connection with health checking and reconnection using php-redis extension
|
||||
*/
|
||||
final class RedisConnection implements RedisConnectionInterface
|
||||
{
|
||||
private Redis $client;
|
||||
|
||||
private bool $connected = false;
|
||||
|
||||
public function __construct(
|
||||
private readonly RedisConfig $config,
|
||||
private readonly string $name = 'default'
|
||||
) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
public function getClient(): Redis
|
||||
{
|
||||
if (! $this->isConnected()) {
|
||||
$this->reconnect();
|
||||
}
|
||||
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function getDatabase(): int
|
||||
{
|
||||
return $this->config->database;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function isConnected(): bool
|
||||
{
|
||||
if (! $this->connected) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->client->ping() === '+PONG';
|
||||
} catch (RedisException) {
|
||||
$this->connected = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function reconnect(): void
|
||||
{
|
||||
$this->connected = false;
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
private function connect(): void
|
||||
{
|
||||
if (! extension_loaded('redis')) {
|
||||
throw new RedisConnectionException(
|
||||
"Redis extension is not loaded. Please install php-redis extension or use alternative cache drivers."
|
||||
);
|
||||
}
|
||||
|
||||
$this->client = new Redis();
|
||||
|
||||
try {
|
||||
// Connect to Redis
|
||||
$success = $this->client->connect(
|
||||
$this->config->host,
|
||||
$this->config->port,
|
||||
$this->config->timeout,
|
||||
null, // reserved
|
||||
0, // retry_interval
|
||||
$this->config->readWriteTimeout
|
||||
);
|
||||
|
||||
if (! $success) {
|
||||
throw new RedisConnectionException("Failed to connect to Redis server");
|
||||
}
|
||||
|
||||
// Authenticate if password is provided
|
||||
if ($this->config->password) {
|
||||
if (! $this->client->auth($this->config->password)) {
|
||||
throw new RedisConnectionException("Redis authentication failed");
|
||||
}
|
||||
}
|
||||
|
||||
// Select database
|
||||
if ($this->config->database > 0) {
|
||||
if (! $this->client->select($this->config->database)) {
|
||||
throw new RedisConnectionException("Failed to select Redis database {$this->config->database}");
|
||||
}
|
||||
}
|
||||
|
||||
// Set additional options
|
||||
foreach ($this->config->options as $option => $value) {
|
||||
$this->client->setOption($option, $value);
|
||||
}
|
||||
|
||||
$this->connected = true;
|
||||
} catch (RedisException $e) {
|
||||
$this->connected = false;
|
||||
|
||||
throw new RedisConnectionException(
|
||||
"Failed to connect to Redis ({$this->name}): " . $e->getMessage(),
|
||||
previous: $e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection when the object is destroyed
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->connected && $this->client) {
|
||||
try {
|
||||
$this->client->close();
|
||||
} catch (RedisException) {
|
||||
// Ignore disconnection errors during destruction
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user