Fix REDIS HAS GONE AWAY issues.

This commit is contained in:
Greyscale 2020-12-06 19:00:12 +01:00
parent 4e4ea66fb6
commit 744956888b
3 changed files with 41 additions and 22 deletions

View file

@ -289,11 +289,12 @@ class App
return $monolog;
});
$container->set(Redis::class, function (EnvironmentService $environmentService) {
$container->set(Redis::class, function (Logger $logger, EnvironmentService $environmentService) {
return new Redis(
$logger,
$environmentService->get('REDIS_HOST', 'redis'),
$environmentService->get('REDIS_PORT', 6379),
$environmentService->get('REDIS_TIMEOUT', 0.0)
$environmentService->get('REDIS_TIMEOUT', 1.0)
);
});

View file

@ -2,25 +2,43 @@
namespace Benzine\Redis;
class Redis extends \Redis
use Monolog\Logger;
/**
* A wrapper around \Redis for my own sanity.
*
* @package Benzine\Redis
*/
class Redis
{
private string $host;
private int $port;
private int $timeout;
private float $timeout;
private \Redis $redis;
private Logger $logger;
/** @var Lua\AbstractLuaExtension[] */
private array $scripts;
public function __construct($host, $port = 6379, $timeout = 0.0)
public function __construct(Logger $logger, string $host, int $port = 6379, float $timeout = 0.0)
{
$this->logger = $logger;
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
parent::__construct();
$this->redis = new \Redis();
}
public function __call($name, $arguments)
{
$this->runBeforeRedisCommand();
if(method_exists($this->redis, $name)){
return call_user_func_array([$this->redis, $name], $arguments);
}
foreach ($this->scripts as $script) {
foreach ($script->getFunctionNames() as $functionName) {
if (strtolower($name) == strtolower($functionName)) {
@ -35,7 +53,7 @@ class Redis extends \Redis
public function isAvailable(): bool
{
try {
$this->ping('am I human?');
$this->redis->ping('am I human?');
return true;
} catch (\RedisException $redisException) {
@ -43,25 +61,24 @@ class Redis extends \Redis
}
}
public function get($key): void
{
if (!$this->isConnected()) {
parent::pconnect($this->host, $this->port, $this->timeout);
$this->initialiseExtensions();
}
parent::get($key); // TODO: Change the autogenerated stub
}
public function initialiseExtensions(): void
{
$this->scripts[] = new Lua\SetIfHigher($this);
$this->scripts[] = new Lua\SetIfLower($this);
$this->scripts[] = new Lua\ZAddIfHigher($this);
$this->scripts[] = new Lua\ZAddIfLower($this);
$this->scripts[] = new Lua\SetIfHigher($this->redis);
$this->scripts[] = new Lua\SetIfLower($this->redis);
$this->scripts[] = new Lua\ZAddIfHigher($this->redis);
$this->scripts[] = new Lua\ZAddIfLower($this->redis);
}
public function connect($host, $port = 6379, $timeout = 0.0, $reserved = null, $retryInterval = 0, $readTimeout = 0.0): void
{
throw new \RedisException('Do not directly call connect()');
}
private function runBeforeRedisCommand(): void
{
if (!$this->redis->isConnected()) {
$this->redis->pconnect($this->host, $this->port, $this->timeout);
$this->initialiseExtensions();
}
}
}

View file

@ -2,6 +2,7 @@
namespace Benzine\Services;
use Benzine\Redis\Redis;
use Benzine\Workers\WorkerWorkItem;
use Gone\UUID\UUID;
use Monolog\Logger;
@ -9,11 +10,11 @@ use Monolog\Logger;
class QueueService
{
public const MAX_QUEUE_AGE = 60 * 60 * 24;
protected \Redis $redis;
protected Redis $redis;
protected Logger $logger;
public function __construct(
\Redis $redis,
Redis $redis,
Logger $logger
) {
$this->redis = $redis;