forked from php-enqueue/gps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpsConsumer.php
137 lines (113 loc) · 3.13 KB
/
GpsConsumer.php
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
<?php
declare(strict_types=1);
namespace Enqueue\Gps;
use Google\Cloud\Core\Exception\ServiceException;
use Google\Cloud\PubSub\Message as GoogleMessage;
use Google\Cloud\PubSub\Subscription;
use Interop\Queue\Consumer;
use Interop\Queue\Message;
use Interop\Queue\Queue;
class GpsConsumer implements Consumer
{
/**
* @var GpsContext
*/
private $context;
/**
* @var GpsQueue
*/
private $queue;
/**
* @var Subscription
*/
private $subscription;
public function __construct(GpsContext $context, GpsQueue $queue)
{
$this->context = $context;
$this->queue = $queue;
}
/**
* @return GpsQueue
*/
public function getQueue(): Queue
{
return $this->queue;
}
/**
* @return GpsMessage
*/
public function receive(int $timeout = 0): ?Message
{
if (0 === $timeout) {
while (true) {
if ($message = $this->receiveMessage($timeout)) {
return $message;
}
}
} else {
return $this->receiveMessage($timeout);
}
}
/**
* @return GpsMessage
*/
public function receiveNoWait(): ?Message
{
$messages = $this->getSubscription()->pull([
'maxMessages' => 1,
'returnImmediately' => true,
]);
if ($messages) {
return $this->convertMessage(current($messages));
}
return null;
}
/**
* @param GpsMessage $message
*/
public function acknowledge(Message $message): void
{
if (false == $message->getNativeMessage()) {
throw new \LogicException('Native google pub/sub message required but it is empty');
}
$this->getSubscription()->acknowledge($message->getNativeMessage());
}
/**
* @param GpsMessage $message
*/
public function reject(Message $message, bool $requeue = false): void
{
if (false == $message->getNativeMessage()) {
throw new \LogicException('Native google pub/sub message required but it is empty');
}
$this->getSubscription()->acknowledge($message->getNativeMessage());
}
private function getSubscription(): Subscription
{
if (null === $this->subscription) {
$this->subscription = $this->context->getClient()->subscription($this->queue->getQueueName());
}
return $this->subscription;
}
private function convertMessage(GoogleMessage $message): GpsMessage
{
$gpsMessage = GpsMessage::jsonUnserialize($message->data());
$gpsMessage->setNativeMessage($message);
return $gpsMessage;
}
private function receiveMessage(int $timeout): ?GpsMessage
{
$timeout /= 1000;
try {
$messages = $this->getSubscription()->pull([
'maxMessages' => 1,
'requestTimeout' => $timeout,
]);
if ($messages) {
return $this->convertMessage(current($messages));
}
} catch (ServiceException $e) {
} // timeout
return null;
}
}