1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Command\Queue\Subscribe; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Command\Command; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Superbalist\PubSub\Redis\RedisPubSubAdapter; |
16
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
17
|
|
|
|
18
|
|
|
class PredisSubscribeCommandQueue implements SubscribeCommandQueue |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_FORMAT = 'predis'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RedisPubSubAdapter |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SerializerInterface |
29
|
|
|
*/ |
30
|
|
|
private $serializer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LoggerInterface |
34
|
|
|
*/ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $queue_name = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $format = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param RedisPubSubAdapter $client |
49
|
|
|
* @param SerializerInterface $serializer |
50
|
|
|
* @param LoggerInterface $logger |
51
|
|
|
* @param string $queue_name |
52
|
|
|
* @param string|null $format |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
RedisPubSubAdapter $client, |
56
|
|
|
SerializerInterface $serializer, |
57
|
|
|
LoggerInterface $logger, |
58
|
|
|
$queue_name, |
59
|
|
|
$format = null |
60
|
|
|
) { |
61
|
|
|
$this->client = $client; |
62
|
|
|
$this->serializer = $serializer; |
63
|
|
|
$this->logger = $logger; |
64
|
|
|
$this->queue_name = $queue_name; |
65
|
|
|
$this->format = $format ?: self::DEFAULT_FORMAT; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Publish command to queue. |
70
|
|
|
* |
71
|
|
|
* @param Command $command |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function publish(Command $command) |
76
|
|
|
{ |
77
|
|
|
$massage = $this->serializer->serialize($command, $this->format); |
78
|
|
|
$this->client->publish($this->queue_name, $massage); |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Subscribe on command queue. |
85
|
|
|
* |
86
|
|
|
* @param callable $handler |
87
|
|
|
*/ |
88
|
|
|
public function subscribe(callable $handler) |
89
|
|
|
{ |
90
|
|
|
$this->client->subscribe($this->queue_name, function ($message) use ($handler) { |
91
|
|
|
try { |
92
|
|
|
$command = $this->serializer->deserialize($message, Command::class, $this->format); |
93
|
|
|
} catch (\Exception $e) { // catch only deserialize exception |
94
|
|
|
// it's a critical error |
95
|
|
|
// it is necessary to react quickly to it |
96
|
|
|
$this->logger->critical( |
97
|
|
|
'Failed denormalize a command in the Redis queue', |
98
|
|
|
[$message, $e->getMessage()] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
// try denormalize in later |
102
|
|
|
$this->client->publish($this->queue_name, $message); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($command)) { |
106
|
|
|
call_user_func($handler, $command); |
107
|
|
|
} |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|