Skip to content

Commit e52b476

Browse files
committed
[amqp-ext] move basic consume logic to context.
1 parent 3789423 commit e52b476

8 files changed

Lines changed: 245 additions & 0 deletions

‎docker/Dockerfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RUN echo "extension=rdkafka.so" > /etc/php/7.1/cli/conf.d/10-rdkafka.ini
1717
RUN echo "extension=rdkafka.so" > /etc/php/7.1/fpm/conf.d/10-rdkafka.ini
1818

1919
COPY ./php/cli.ini /etc/php/7.1/cli/conf.d/1-dev_cli.ini
20+
COPY ./php/amqp.so /usr/lib/php/20160303/amqp.so
2021
COPY ./bin/dev_entrypoiny.sh /usr/local/bin/entrypoint.sh
2122
RUN chmod u+x /usr/local/bin/entrypoint.sh
2223

‎docker/php/amqp.so‎

609 KB
Binary file not shown.

‎pkg/amqp-ext/AmqpContext.php‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Enqueue\AmqpTools\DelayStrategyAware;
66
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
77
use Interop\Amqp\AmqpBind as InteropAmqpBind;
8+
use Interop\Amqp\AmqpConsumer as InteropAmqpConsumer;
89
use Interop\Amqp\AmqpContext as InteropAmqpContext;
910
use Interop\Amqp\AmqpQueue as InteropAmqpQueue;
1011
use Interop\Amqp\AmqpTopic as InteropAmqpTopic;
@@ -41,6 +42,11 @@ class AmqpContext implements InteropAmqpContext, DelayStrategyAware
4142
*/
4243
private $receiveMethod;
4344

45+
/**
46+
* @var array
47+
*/
48+
private $basicConsumeSubscribers;
49+
4450
/**
4551
* Callable must return instance of \AMQPChannel once called.
4652
*
@@ -60,6 +66,7 @@ public function __construct($extChannel, $receiveMethod)
6066
}
6167

6268
$this->buffer = new Buffer();
69+
$this->basicConsumeSubscribers = [];
6370
}
6471

6572
/**
@@ -289,4 +296,121 @@ public function getExtChannel()
289296

290297
return $this->extChannel;
291298
}
299+
300+
/**
301+
* Notify broker that the channel is interested in consuming messages from this queue.
302+
*
303+
* @param InteropAmqpConsumer $consumer
304+
* @param callable $callback A callback function to which the
305+
* consumed message will be passed. The
306+
* function must accept at a minimum
307+
* one parameter, an \Interop\Amqp\AmqpMessage object,
308+
* and an optional second parameter
309+
* the \Interop\Amqp\AmqpConsumer from which the message was
310+
* consumed. The \Interop\Amqp\AmqpContext::basicConsume() will
311+
* not return the processing thread back to
312+
* the PHP script until the callback
313+
* function returns FALSE.
314+
*/
315+
public function basicConsumeSubscribe(InteropAmqpConsumer $consumer, callable $callback)
316+
{
317+
$extQueue = new \AMQPQueue($this->getExtChannel());
318+
$extQueue->setName($consumer->getQueue()->getQueueName());
319+
320+
$extQueue->consume(null, Flags::convertConsumerFlags($consumer->getFlags()), $consumer->getConsumerTag());
321+
322+
$consumerTag = $extQueue->getConsumerTag();
323+
$consumer->setConsumerTag($consumerTag);
324+
$this->basicConsumeSubscribers[$consumerTag] = [$consumer, $callback];
325+
}
326+
327+
public function basicConsumeUnsubscribe(InteropAmqpConsumer $consumer)
328+
{
329+
// if ($consumer->getConsumerTag()) {
330+
// $consumerTag = $consumer->getConsumerTag();
331+
// $consumer->setConsumerTag(null);
332+
//
333+
// $extQueue = new \AMQPQueue($this->getExtChannel());
334+
// $extQueue->setName($consumer->getQueue()->getQueueName());
335+
//
336+
// $extQueue->cancel($consumerTag);
337+
// unset($this->basicConsumeSubscribers[$consumerTag]);
338+
// }
339+
}
340+
341+
/**
342+
* @param float|int $timeout milliseconds, consumes endlessly if zero set
343+
*/
344+
public function basicConsume($timeout = 0)
345+
{
346+
if (empty($this->basicConsumeSubscribers)) {
347+
throw new \LogicException('There is no subscribers. Consider calling basicConsumeSubscribe before consuming');
348+
}
349+
350+
/** @var \AMQPQueue $extQueue */
351+
$extConnection = $this->getExtChannel()->getConnection();
352+
353+
$originalTimeout = $extConnection->getReadTimeout();
354+
try {
355+
$extConnection->setReadTimeout($timeout / 1000);
356+
357+
reset($this->basicConsumeSubscribers);
358+
/** @var $consumer AmqpConsumer */
359+
list($consumer) = current($this->basicConsumeSubscribers);
360+
361+
$extQueue = new \AMQPQueue($this->getExtChannel());
362+
$extQueue->setName($consumer->getQueue()->getQueueName());
363+
$extQueue->consume(function (\AMQPEnvelope $extEnvelope, \AMQPQueue $q) {
364+
$message = $this->convertMessage($extEnvelope);
365+
$message->setConsumerTag($q->getConsumerTag());
366+
367+
/**
368+
* @var AmqpConsumer
369+
* @var callable $callback
370+
*/
371+
list($consumer, $callback) = $this->basicConsumeSubscribers[$q->getConsumerTag()];
372+
373+
return call_user_func($callback, $message, $consumer);
374+
}, AMQP_JUST_CONSUME);
375+
} catch (\AMQPQueueException $e) {
376+
if ('Consumer timeout exceed' == $e->getMessage()) {
377+
return null;
378+
}
379+
380+
throw $e;
381+
} finally {
382+
$extConnection->setReadTimeout($originalTimeout);
383+
}
384+
}
385+
386+
/**
387+
* @param \AMQPEnvelope $extEnvelope
388+
*
389+
* @return AmqpMessage
390+
*/
391+
private function convertMessage(\AMQPEnvelope $extEnvelope)
392+
{
393+
$message = new AmqpMessage(
394+
$extEnvelope->getBody(),
395+
$extEnvelope->getHeaders(),
396+
[
397+
'message_id' => $extEnvelope->getMessageId(),
398+
'correlation_id' => $extEnvelope->getCorrelationId(),
399+
'app_id' => $extEnvelope->getAppId(),
400+
'type' => $extEnvelope->getType(),
401+
'content_encoding' => $extEnvelope->getContentEncoding(),
402+
'content_type' => $extEnvelope->getContentType(),
403+
'expiration' => $extEnvelope->getExpiration(),
404+
'priority' => $extEnvelope->getPriority(),
405+
'reply_to' => $extEnvelope->getReplyTo(),
406+
'timestamp' => $extEnvelope->getTimeStamp(),
407+
'user_id' => $extEnvelope->getUserId(),
408+
]
409+
);
410+
$message->setRedelivered($extEnvelope->isRedelivery());
411+
$message->setDeliveryTag($extEnvelope->getDeliveryTag());
412+
$message->setRoutingKey($extEnvelope->getRoutingKey());
413+
414+
return $message;
415+
}
292416
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeBreakOnFalseSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeBreakOnFalseTest extends BasicConsumeBreakOnFalseSpec
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function createContext()
17+
{
18+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
19+
20+
return $factory->createContext();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeFromAllSubscribedQueuesSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeFromAllSubscribedQueuesTest extends BasicConsumeFromAllSubscribedQueuesSpec
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function createContext()
17+
{
18+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
19+
20+
return $factory->createContext();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeShouldAddConsumerTagOnSubscribeSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeShouldAddConsumerTagOnSubscribeTest extends BasicConsumeShouldAddConsumerTagOnSubscribeSpec
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function createContext()
17+
{
18+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
19+
20+
return $factory->createContext();
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeShouldRemoveConsumerTagOnUnsubscribeSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeShouldRemoveConsumerTagOnUnsubscribeTest extends BasicConsumeShouldRemoveConsumerTagOnUnsubscribeSpec
12+
{
13+
public function test()
14+
{
15+
$this->markTestSkipped('Seg fault.');
16+
}
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function createContext()
22+
{
23+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
24+
25+
return $factory->createContext();
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Enqueue\AmqpExt\Tests\Spec;
4+
5+
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Interop\Queue\Spec\Amqp\BasicConsumeUntilUnsubscribedSpec;
7+
8+
/**
9+
* @group functional
10+
*/
11+
class AmqpBasicConsumeUntilUnsubscribedTest extends BasicConsumeUntilUnsubscribedSpec
12+
{
13+
public function test()
14+
{
15+
$this->markTestSkipped('Sig fault');
16+
}
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function createContext()
22+
{
23+
$factory = new AmqpConnectionFactory(getenv('AMQP_DSN'));
24+
25+
return $factory->createContext();
26+
}
27+
}

0 commit comments

Comments
 (0)