|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Diagnostics; |
|
12
|
|
|
|
|
13
|
|
|
use Predis\ClientInterface; |
|
14
|
|
|
use Predis\PredisException; |
|
15
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
|
16
|
|
|
use ZendDiagnostics\Result\Failure; |
|
17
|
|
|
use ZendDiagnostics\Result\Success; |
|
18
|
|
|
use ZendDiagnostics\Result\Warning; |
|
19
|
|
|
|
|
20
|
|
|
class RedisServiceCheck implements CheckInterface |
|
21
|
|
|
{ |
|
22
|
|
|
private const KEY_PREFIX = 'redis_check_'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ClientInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $redis; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* RedisServiceCheck constructor. |
|
31
|
|
|
* @param ClientInterface $redis |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function __construct(ClientInterface $redis = null) |
|
34
|
|
|
{ |
|
35
|
4 |
|
$this->redis = $redis; |
|
36
|
4 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
4 |
|
public function check() |
|
42
|
|
|
{ |
|
43
|
4 |
|
if ($this->redis === null) { |
|
44
|
1 |
|
return new Warning('Redis is not configured. Nothing to test...'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
$key = self::KEY_PREFIX.random_int(0, PHP_INT_MAX); |
|
48
|
3 |
|
$value = random_bytes(random_int(10, 255)); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
3 |
|
$this->redis->set($key, $value); |
|
52
|
2 |
|
sleep(2); |
|
53
|
2 |
|
$response = $this->redis->get($key); |
|
54
|
2 |
|
$this->redis->del([$key]); |
|
55
|
|
|
|
|
56
|
2 |
|
if ($response === $value) { |
|
57
|
1 |
|
return new Success("Redis is working. Tested SET, GET and DEL"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return new Failure("Redis is not working properly. GET didn't return expected value."); |
|
61
|
1 |
|
} catch (PredisException $e) { |
|
62
|
1 |
|
return new Failure("Redis is not working properly. Exception: {$e->getMessage()}"); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getLabel() |
|
70
|
|
|
{ |
|
71
|
|
|
return 'Predis Redis'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|