Skip to content

Commit 7405306

Browse files
committed
gps
1 parent 7e3ac0a commit 7405306

11 files changed

Lines changed: 824 additions & 0 deletions

‎composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"enqueue/gearman": "*@dev",
2121
"enqueue/rdkafka": "*@dev",
2222
"kwn/php-rdkafka-stubs": "^1.0.2",
23+
"enqueue/gps": "*@dev",
2324
"enqueue/enqueue-bundle": "*@dev",
2425
"enqueue/job-queue": "*@dev",
2526
"enqueue/simple-client": "*@dev",
@@ -129,6 +130,10 @@
129130
"type": "path",
130131
"url": "pkg/rdkafka"
131132
},
133+
{
134+
"type": "path",
135+
"url": "pkg/gps"
136+
},
132137
{
133138
"type": "path",
134139
"url": "pkg/simple-client"

‎docker-compose.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ services:
1212
- gearmand
1313
- kafka
1414
- zookeeper
15+
- google-pubsub
1516
volumes:
1617
- './:/mqdev'
1718
environment:
@@ -40,6 +41,8 @@ services:
4041
- GEARMAN_DSN=gearman://gearmand:4730
4142
- RDKAFKA_HOST=kafka
4243
- RDKAFKA_PORT=9092
44+
- PUBSUB_EMULATOR_HOST=http://google-pubsub:8085
45+
- GCLOUD_PROJECT=mqdev
4346

4447
rabbitmq:
4548
image: enqueue/rabbitmq:latest
@@ -89,6 +92,10 @@ services:
8992
volumes:
9093
- '/var/run/docker.sock:/var/run/docker.sock'
9194

95+
google-pubsub:
96+
image: 'google/cloud-sdk:latest'
97+
entrypoint: 'gcloud beta emulators pubsub start --host-port=0.0.0.0:8085'
98+
9299
volumes:
93100
mysql-data:
94101
driver: local

‎phpunit.xml.dist‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<directory>pkg/rdkafka/Tests</directory>
7474
</testsuite>
7575

76+
<testsuite name="Google Pub/Sub transport">
77+
<directory>pkg/gps/Tests</directory>
78+
</testsuite>
79+
7680
<testsuite name="enqueue-bundle">
7781
<directory>pkg/enqueue-bundle/Tests</directory>
7882
</testsuite>

‎pkg/gps/GpsConnectionFactory.php‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Enqueue\Gps;
4+
5+
use Google\Cloud\PubSub\PubSubClient;
6+
use Interop\Queue\PsrConnectionFactory;
7+
8+
class GpsConnectionFactory implements PsrConnectionFactory
9+
{
10+
/**
11+
* @var array
12+
*/
13+
private $config;
14+
15+
/**
16+
* @param array $config
17+
*/
18+
public function __construct(array $config = [])
19+
{
20+
$this->config = array_replace($this->defaultConfig(), $config);
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*
26+
* @return GpsContext
27+
*/
28+
public function createContext()
29+
{
30+
if ($this->config['lazy']) {
31+
return new GpsContext(function () {
32+
return $this->establishConnection();
33+
});
34+
}
35+
36+
return new GpsContext($this->establishConnection());
37+
}
38+
39+
/**
40+
* @return PubSubClient
41+
*/
42+
private function establishConnection()
43+
{
44+
return new PubSubClient($this->config);
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
private function defaultConfig()
51+
{
52+
return [
53+
'lazy' => true,
54+
];
55+
}
56+
}

‎pkg/gps/GpsConsumer.php‎

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace Enqueue\Gps;
4+
5+
use Google\Cloud\Core\Exception\ServiceException;
6+
use Google\Cloud\PubSub\Message;
7+
use Google\Cloud\PubSub\Subscription;
8+
use Interop\Queue\PsrConsumer;
9+
use Interop\Queue\PsrMessage;
10+
11+
class GpsConsumer implements PsrConsumer
12+
{
13+
/**
14+
* @var GpsContext
15+
*/
16+
private $context;
17+
18+
/**
19+
* @var GpsQueue
20+
*/
21+
private $queue;
22+
23+
/**
24+
* @var Subscription
25+
*/
26+
private $subscription;
27+
28+
/**
29+
* @param GpsContext $context
30+
* @param GpsQueue $queue
31+
*/
32+
public function __construct(GpsContext $context, GpsQueue $queue)
33+
{
34+
$this->context = $context;
35+
$this->queue = $queue;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getQueue()
42+
{
43+
return $this->queue;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function receive($timeout = 0)
50+
{
51+
if ($timeout === 0) {
52+
while (true) {
53+
if ($message = $this->receiveMessage($timeout)) {
54+
return $message;
55+
}
56+
}
57+
} else {
58+
return $this->receiveMessage($timeout);
59+
}
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function receiveNoWait()
66+
{
67+
$messages = $this->getSubscription()->pull([
68+
'maxMessages' => 1,
69+
'returnImmediately' => true,
70+
]);
71+
72+
if ($messages) {
73+
return $this->convertMessage(current($messages));
74+
}
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function acknowledge(PsrMessage $message)
81+
{
82+
if (false == $message->getNativeMessage()) {
83+
throw new \LogicException('Native google pub/sub message required but it is empty');
84+
}
85+
86+
$this->getSubscription()->acknowledge($message->getNativeMessage());
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function reject(PsrMessage $message, $requeue = false)
93+
{
94+
if (false == $message->getNativeMessage()) {
95+
throw new \LogicException('Native google pub/sub message required but it is empty');
96+
}
97+
98+
$this->getSubscription()->acknowledge($message->getNativeMessage());
99+
}
100+
101+
/**
102+
* @return Subscription
103+
*/
104+
private function getSubscription()
105+
{
106+
if (null === $this->subscription) {
107+
$this->subscription = $this->context->getClient()->subscription($this->queue->getQueueName());
108+
}
109+
110+
return $this->subscription;
111+
}
112+
113+
/**
114+
* @param Message $message
115+
*
116+
* @return GpsMessage
117+
*/
118+
private function convertMessage(Message $message)
119+
{
120+
$gpsMessage = GpsMessage::jsonUnserialize($message->data());
121+
$gpsMessage->setNativeMessage($message);
122+
123+
return $gpsMessage;
124+
}
125+
126+
/**
127+
* @param int $timeout
128+
*
129+
* @return GpsMessage|null
130+
*/
131+
private function receiveMessage($timeout)
132+
{
133+
$timeout /= 1000;
134+
135+
try {
136+
$messages = $this->getSubscription()->pull([
137+
'maxMessages' => 1,
138+
'requestTimeout' => $timeout,
139+
]);
140+
141+
if ($messages) {
142+
return $this->convertMessage(current($messages));
143+
}
144+
} catch (ServiceException $e) {} // timeout
145+
}
146+
}

0 commit comments

Comments
 (0)