Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dbal] add priority support on transport level. #198

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/dbal/DbalConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function receiveMessage()
->where('queue = :queue')
->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)')
->orderBy('priority', 'desc')
->addOrderBy('id', 'asc')
->addOrderBy('id', 'desc')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this change it from a fifo queue to a lifo queue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I messed up everything. I must've fixed priority ordering not id.

->setMaxResults(1)
;

Expand Down
22 changes: 15 additions & 7 deletions pkg/dbal/DbalProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class DbalProducer implements PsrProducer
{
/**
* @var int|null
*/
private $priority;

/**
* @var DbalContext
*/
Expand All @@ -29,14 +34,19 @@ public function __construct(DbalContext $context)
/**
* {@inheritdoc}
*
* @param PsrDestination $destination
* @param PsrMessage $message
* @param DbalDestination $destination
* @param DbalMessage $message
*
* @throws Exception
*/
public function send(PsrDestination $destination, PsrMessage $message)
{
InvalidDestinationException::assertDestinationInstanceOf($destination, DbalDestination::class);
InvalidMessageException::assertMessageInstanceOf($message, DbalMessage::class);

if (null !== $this->priority && null === $message->getPriority()) {
$message->setPriority($this->priority);
}

$body = $message->getBody();
if (is_scalar($body) || null === $body) {
Expand Down Expand Up @@ -111,19 +121,17 @@ public function getDeliveryDelay()
*/
public function setPriority($priority)
{
if (null === $priority) {
return;
}
$this->priority = $priority;

throw new \LogicException('Not implemented');
return $this;
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
return null;
return $this->priority;
}

/**
Expand Down
91 changes: 0 additions & 91 deletions pkg/dbal/Tests/DbalSendPriorityMessagesTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Enqueue\Dbal\Tests\Spec;

use Enqueue\Dbal\DbalConnectionFactory;
use Enqueue\Dbal\DbalDestination;
use Enqueue\Dbal\DbalMessage;
use Interop\Queue\PsrContext;
use Interop\Queue\Spec\SendAndReceivePriorityMessagesFromQueueSpec;

/**
* @group functional
*/
class DbalSendAndReceivePriorityMessagesFromQueueTest extends SendAndReceivePriorityMessagesFromQueueSpec
{
/**
* @return PsrContext
*/
protected function createContext()
{
$factory = new DbalConnectionFactory([
'lazy' => true,
'connection' => [
'dbname' => getenv('SYMFONY__DB__NAME'),
'user' => getenv('SYMFONY__DB__USER'),
'password' => getenv('SYMFONY__DB__PASSWORD'),
'host' => getenv('SYMFONY__DB__HOST'),
'port' => getenv('SYMFONY__DB__PORT'),
'driver' => getenv('SYMFONY__DB__DRIVER'),
],
]);

$context = $factory->createContext();
$context->createDataBaseTable();

return $context;
}

/**
* {@inheritdoc}
*
* @return DbalMessage
*/
protected function createMessage(PsrContext $context, $priority)
{
/** @var DbalMessage $message */
$message = $context->createMessage('priority'.$priority);
$message->setPriority($priority);

return $message;
}

/**
* {@inheritdoc}
*
* @return DbalDestination
*/
protected function createQueue(PsrContext $context, $queueName)
{
return parent::createQueue($context, $queueName.time());
}
}