refactor: simplify Redis configuration initialization
- Use RedisConfig::fromEnvironment() in LoggerInitializer - Remove fallback logic in QueueInitializer, always use connection pool - Make RedisConfig constructor private - Clean up Redis connection error message
This commit is contained in:
@@ -121,7 +121,7 @@ final readonly class LoggerInitializer
|
|||||||
*/
|
*/
|
||||||
private function createQueue(Environment $env): Queue
|
private function createQueue(Environment $env): Queue
|
||||||
{
|
{
|
||||||
$redisConfig = new RedisConfig(host: $env->getString(EnvKey::REDIS_HOST, 'redis'), database: 2);
|
$redisConfig = RedisConfig::fromEnvironment($env);
|
||||||
$redisConnection = new RedisConnection($redisConfig, 'queue');
|
$redisConnection = new RedisConnection($redisConfig, 'queue');
|
||||||
|
|
||||||
return new RedisQueue($redisConnection, 'commands');
|
return new RedisQueue($redisConnection, 'commands');
|
||||||
|
|||||||
@@ -32,19 +32,9 @@ final readonly class QueueInitializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to use Redis connection pool if available, otherwise create direct connection
|
// Try to use Redis connection pool if available, otherwise create direct connection
|
||||||
$redisConnection = null;
|
|
||||||
if ($this->container->has(RedisConnectionPool::class)) {
|
$pool = $this->container->get(RedisConnectionPool::class);
|
||||||
$pool = $this->container->get(RedisConnectionPool::class);
|
$redisConnection = $pool->getQueueConnection();
|
||||||
$redisConnection = $pool->getQueueConnection();
|
|
||||||
} else {
|
|
||||||
// Fallback to direct connection creation
|
|
||||||
$redisConfig = new RedisConfig(
|
|
||||||
host: 'redis',
|
|
||||||
port: 6379,
|
|
||||||
database: 2 // Use DB 2 for queue
|
|
||||||
);
|
|
||||||
$redisConnection = new RedisConnection($redisConfig, 'queue');
|
|
||||||
}
|
|
||||||
|
|
||||||
return new RedisQueue(
|
return new RedisQueue(
|
||||||
connection: $redisConnection,
|
connection: $redisConnection,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use App\Framework\Config\EnvKey;
|
|||||||
*/
|
*/
|
||||||
final readonly class RedisConfig
|
final readonly class RedisConfig
|
||||||
{
|
{
|
||||||
public function __construct(
|
private function __construct(
|
||||||
public string $host = 'redis',
|
public string $host = 'redis',
|
||||||
public int $port = 6379,
|
public int $port = 6379,
|
||||||
public ?string $password = null,
|
public ?string $password = null,
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ final class RedisConnection implements RedisConnectionInterface
|
|||||||
$this->connected = false;
|
$this->connected = false;
|
||||||
|
|
||||||
throw new RedisConnectionException(
|
throw new RedisConnectionException(
|
||||||
"Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host} and Password: {$this->config->password} should be {$_ENV['REDIS_HOST']} and {$_ENV['REDIS_PASSWORD']}",
|
"Failed to connect to Redis ({$this->name}): " . $e->getMessage() . " with Host: {$this->config->host} and Password: {$this->config->password}",
|
||||||
previous: $e
|
previous: $e
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user