|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* The software is based on the Axon Framework project which is |
|
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
|
18
|
|
|
* see <http://www.axonframework.org/>. |
|
19
|
|
|
* |
|
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
21
|
|
|
* and is licensed under the MIT license. For more information, see |
|
22
|
|
|
* <http://www.governor-framework.org/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Governor\Framework\CommandHandling\Distributed; |
|
26
|
|
|
|
|
27
|
|
|
use Governor\Framework\CommandHandling\Callbacks\ClosureCommandCallback; |
|
28
|
|
|
use Governor\Framework\CommandHandling\Callbacks\ResultCallback; |
|
29
|
|
|
use Governor\Framework\Common\ReceiverInterface; |
|
30
|
|
|
use Governor\Framework\CommandHandling\CommandBusInterface; |
|
31
|
|
|
use Governor\Framework\Serializer\SerializerInterface; |
|
32
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
33
|
|
|
use Governor\Framework\Common\Logging\NullLogger; |
|
34
|
|
|
use Psr\Log\LoggerInterface; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Receiver that forwards incoming distributed commands to the local command bus. |
|
38
|
|
|
* |
|
39
|
|
|
* @author "David Kalosi" <[email protected]> |
|
40
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
|
41
|
|
|
*/ |
|
42
|
|
|
class CommandReceiver implements ReceiverInterface, LoggerAwareInterface |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var RedisTemplate |
|
46
|
|
|
*/ |
|
47
|
|
|
private $template; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var CommandBusInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
private $localSegment; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var SerializerInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private $serializer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var LoggerInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
private $logger; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param RedisTemplate $template |
|
66
|
|
|
* @param CommandBusInterface $localSegment |
|
67
|
|
|
* @param SerializerInterface $serializer |
|
68
|
|
|
*/ |
|
69
|
|
|
function __construct(RedisTemplate $template, CommandBusInterface $localSegment, SerializerInterface $serializer) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$this->template = $template; |
|
72
|
|
|
$this->localSegment = $localSegment; |
|
73
|
|
|
$this->serializer = $serializer; |
|
74
|
|
|
$this->logger = new NullLogger(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// TODO daemonize: interrupts, shutdown etc |
|
78
|
|
|
public function run() |
|
79
|
|
|
{ |
|
80
|
|
|
while (true) { |
|
81
|
|
|
try { |
|
82
|
|
|
$this->processCommand(); |
|
83
|
|
|
} catch (\Exception $ex) { |
|
84
|
|
|
$this->logger->error( |
|
85
|
|
|
'Exception on node {node} while processing command: {message}', |
|
86
|
|
|
[ |
|
87
|
|
|
'node' => $this->template->getNodeName(), |
|
88
|
|
|
'message' => $ex->getMessage() |
|
89
|
|
|
] |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function processCommand() |
|
96
|
|
|
{ |
|
97
|
|
|
$data = $this->template->dequeueCommand(); |
|
98
|
|
|
|
|
99
|
|
|
if (null === $data) { |
|
100
|
|
|
$this->logger->info('Timeout while waiting for commands, re-entering loop'); |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$dispatchMessage = DispatchMessage::fromBytes($this->serializer, $data[1]); |
|
105
|
|
|
$self = $this; |
|
106
|
|
|
|
|
107
|
|
View Code Duplication |
$successCallback = function ($result) use ($dispatchMessage, $self) { |
|
|
|
|
|
|
108
|
|
|
$message = new ReplyMessage( |
|
109
|
|
|
$dispatchMessage->getCommandIdentifier(), |
|
110
|
|
|
$self->serializer, |
|
111
|
|
|
$result |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$self->template->writeCommandReply($dispatchMessage->getCommandIdentifier(), $message->toBytes()); |
|
115
|
|
|
}; |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
$failureCallback = function (\Exception $cause) use ($dispatchMessage, $self) { |
|
|
|
|
|
|
118
|
|
|
$message = new ReplyMessage( |
|
119
|
|
|
$dispatchMessage->getCommandIdentifier(), |
|
120
|
|
|
$self->serializer, |
|
121
|
|
|
$cause, |
|
122
|
|
|
false |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$self->template->writeCommandReply($dispatchMessage->getCommandIdentifier(), $message->toBytes()); |
|
126
|
|
|
}; |
|
127
|
|
|
|
|
128
|
|
|
$this->localSegment->dispatch( |
|
129
|
|
|
$dispatchMessage->getCommandMessage(), |
|
130
|
|
|
$dispatchMessage->isExpectReply() ? new ClosureCommandCallback( |
|
131
|
|
|
$successCallback, $failureCallback |
|
132
|
|
|
) : null |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Sets a logger instance on the object |
|
138
|
|
|
* |
|
139
|
|
|
* @param LoggerInterface $logger |
|
140
|
|
|
* @return null |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setLogger(LoggerInterface $logger) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->logger = $logger; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.