Repository navigation
Expand file tree
/
Copy pathFsConsumer.php
More file actions
234 lines (194 loc) · 5.76 KB
/
Copy pathFsConsumer.php
File metadata and controls
234 lines (194 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Enqueue\Fs;
use Interop\Queue\InvalidMessageException;
use Interop\Queue\PsrConsumer;
use Interop\Queue\PsrMessage;
class FsConsumer implements PsrConsumer
{
/**
* @var FsDestination
*/
private $destination;
/**
* @var FsContext
*/
private $context;
/**
* @var int
*/
private $preFetchCount;
/**
* @var FsMessage[]
*/
private $preFetchedMessages;
/**
* @var int microseconds
*/
private $pollingInterval = 100000;
/**
* @param FsContext $context
* @param FsDestination $destination
* @param int $preFetchCount
*/
public function __construct(FsContext $context, FsDestination $destination, $preFetchCount)
{
$this->context = $context;
$this->destination = $destination;
$this->preFetchCount = $preFetchCount;
$this->preFetchedMessages = [];
}
/**
* Set polling interval in milliseconds.
*
* @param int $msec
*/
public function setPollingInterval($msec)
{
$this->pollingInterval = $msec * 1000;
}
/**
* Get polling interval in milliseconds.
*
* @return int
*/
public function getPollingInterval()
{
return (int) $this->pollingInterval / 1000;
}
/**
* {@inheritdoc}
*
* @return FsDestination
*/
public function getQueue()
{
return $this->destination;
}
/**
* {@inheritdoc}
*
* @return FsMessage|null
*/
public function receive($timeout = 0)
{
$timeout /= 1000;
$startAt = microtime(true);
while (true) {
$message = $this->receiveNoWait();
if ($message) {
return $message;
}
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
return;
}
usleep($this->pollingInterval);
if ($timeout && (microtime(true) - $startAt) >= $timeout) {
return;
}
}
}
/**
* {@inheritdoc}
*/
public function receiveNoWait()
{
if ($this->preFetchedMessages) {
return array_shift($this->preFetchedMessages);
}
$this->context->workWithFile($this->destination, 'c+', function (FsDestination $destination, $file) {
$count = $this->preFetchCount;
while ($count) {
$frame = $this->readFrame($file, 1);
//guards
if ($frame && false == ('|' == $frame[0] || ' ' == $frame[0])) {
throw new \LogicException(sprintf('The frame could start from either " " or "|". The malformed frame starts with "%s".', $frame[0]));
}
if (0 !== $reminder = strlen($frame) % 64) {
throw new \LogicException(sprintf('The frame size is "%d" and it must divide exactly to 64 but it leaves a reminder "%d".', strlen($frame), $reminder));
}
ftruncate($file, fstat($file)['size'] - strlen($frame));
rewind($file);
$rawMessage = substr(trim($frame), 1);
if ($rawMessage) {
try {
$fetchedMessage = FsMessage::jsonUnserialize($rawMessage);
$expireAt = $fetchedMessage->getHeader('x-expire-at');
if ($expireAt && $expireAt - microtime(true) < 0) {
// message has expired, just drop it.
return;
}
$this->preFetchedMessages[] = $fetchedMessage;
} catch (\Exception $e) {
throw new \LogicException(sprintf("Cannot decode json message '%s'", $rawMessage), null, $e);
}
} else {
return;
}
--$count;
}
});
if ($this->preFetchedMessages) {
return array_shift($this->preFetchedMessages);
}
}
/**
* {@inheritdoc}
*/
public function acknowledge(PsrMessage $message)
{
// do nothing. fs transport always works in auto ack mode
}
/**
* {@inheritdoc}
*/
public function reject(PsrMessage $message, $requeue = false)
{
InvalidMessageException::assertMessageInstanceOf($message, FsMessage::class);
// do nothing on reject. fs transport always works in auto ack mode
if ($requeue) {
$this->context->createProducer()->send($this->destination, $message);
}
}
/**
* @return int
*/
public function getPreFetchCount()
{
return $this->preFetchCount;
}
/**
* @param int $preFetchCount
*/
public function setPreFetchCount($preFetchCount)
{
$this->preFetchCount = $preFetchCount;
}
/**
* @param resource $file
* @param int $frameNumber
*
* @return string
*/
private function readFrame($file, $frameNumber)
{
$frameSize = 64;
$offset = $frameNumber * $frameSize;
fseek($file, -$offset, SEEK_END);
$frame = fread($file, $frameSize);
if ('' == $frame) {
return '';
}
if (false !== strpos($frame, '|{')) {
return $frame;
}
$previousFrame = $this->readFrame($file, $frameNumber + 1);
if ('|' === substr($previousFrame, -1) && '{' === $frame[0]) {
$matched = [];
if (false === preg_match('/\ *?\|$/', $previousFrame, $matched)) {
throw new \LogicException('Something went completely wrong.');
}
return $matched[0].$frame;
}
return $previousFrame.$frame;
}
}