-
Notifications
You must be signed in to change notification settings - Fork 441
Dbal Subscription Consumer feature #563
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
Merged
makasim
merged 6 commits into
php-enqueue:master
from
rosamarsky:dbal-subscription-consumer
Oct 18, 2018
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02659a5
Dbal Subscription Consumer feature
rosamarsky 314e790
Renamed named constructor of DbalMessage
rosamarsky b3d26f6
Replaced DbalMessage::fromArrrayDbResult() with DbalContext::convertM…
rosamarsky fa41a17
Replaced constructor with owned method createMessage() in DbalContext.
rosamarsky 4eab59c
Added Subscription consumer to docs
rosamarsky 1c9f591
Added usleep 200ms when no messages are recieved
rosamarsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Dbal; | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
use Interop\Queue\Consumer; | ||
use Interop\Queue\SubscriptionConsumer; | ||
|
||
class DbalSubscriptionConsumer implements SubscriptionConsumer | ||
{ | ||
/** | ||
* @var DbalContext | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* an item contains an array: [DbalConsumer $consumer, callable $callback];. | ||
* | ||
* @var array | ||
*/ | ||
private $subscribers; | ||
|
||
/** | ||
* @var \Doctrine\DBAL\Connection | ||
*/ | ||
private $dbal; | ||
|
||
/** | ||
* @param DbalContext $context | ||
*/ | ||
public function __construct(DbalContext $context) | ||
{ | ||
$this->context = $context; | ||
$this->dbal = $this->context->getDbalConnection(); | ||
$this->subscribers = []; | ||
} | ||
|
||
public function consume(int $timeout = 0): void | ||
{ | ||
if (empty($this->subscribers)) { | ||
throw new \LogicException('No subscribers'); | ||
} | ||
|
||
$timeout = (int) ceil($timeout / 1000); | ||
$endAt = time() + $timeout; | ||
|
||
$queueNames = []; | ||
foreach (array_keys($this->subscribers) as $queueName) { | ||
$queueNames[$queueName] = $queueName; | ||
} | ||
|
||
$currentQueueNames = []; | ||
while (true) { | ||
if (empty($currentQueueNames)) { | ||
$currentQueueNames = $queueNames; | ||
} | ||
|
||
$message = $this->fetchPrioritizedMessage($currentQueueNames) ?: $this->fetchMessage($currentQueueNames); | ||
|
||
if ($message) { | ||
$this->dbal->delete($this->context->getTableName(), ['id' => $message['id']], ['id' => Type::GUID]); | ||
|
||
$dbalMessage = $this->context->convertMessage($message); | ||
|
||
/** | ||
* @var DbalConsumer | ||
* @var callable $callback | ||
*/ | ||
list($consumer, $callback) = $this->subscribers[$message['queue']]; | ||
|
||
if (false === call_user_func($callback, $dbalMessage, $consumer)) { | ||
return; | ||
} | ||
|
||
unset($currentQueueNames[$message['queue']]); | ||
} else { | ||
$currentQueueNames = []; | ||
} | ||
|
||
if ($timeout && microtime(true) >= $endAt) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param DbalConsumer $consumer | ||
*/ | ||
public function subscribe(Consumer $consumer, callable $callback): void | ||
{ | ||
if (false == $consumer instanceof DbalConsumer) { | ||
throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', DbalConsumer::class, get_class($consumer))); | ||
} | ||
|
||
$queueName = $consumer->getQueue()->getQueueName(); | ||
if (array_key_exists($queueName, $this->subscribers)) { | ||
if ($this->subscribers[$queueName][0] === $consumer && $this->subscribers[$queueName][1] === $callback) { | ||
return; | ||
} | ||
|
||
throw new \InvalidArgumentException(sprintf('There is a consumer subscribed to queue: "%s"', $queueName)); | ||
} | ||
|
||
$this->subscribers[$queueName] = [$consumer, $callback]; | ||
} | ||
|
||
/** | ||
* @param DbalConsumer $consumer | ||
*/ | ||
public function unsubscribe(Consumer $consumer): void | ||
{ | ||
if (false == $consumer instanceof DbalConsumer) { | ||
throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', DbalConsumer::class, get_class($consumer))); | ||
} | ||
|
||
$queueName = $consumer->getQueue()->getQueueName(); | ||
|
||
if (false == array_key_exists($queueName, $this->subscribers)) { | ||
return; | ||
} | ||
|
||
if ($this->subscribers[$queueName][0] !== $consumer) { | ||
return; | ||
} | ||
|
||
unset($this->subscribers[$queueName]); | ||
} | ||
|
||
public function unsubscribeAll(): void | ||
{ | ||
$this->subscribers = []; | ||
} | ||
|
||
private function fetchMessage(array $queues): ?array | ||
{ | ||
$query = $this->dbal->createQueryBuilder(); | ||
$query | ||
->select('*') | ||
->from($this->context->getTableName()) | ||
->andWhere('queue IN (:queues)') | ||
->andWhere('priority IS NULL') | ||
->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)') | ||
->addOrderBy('published_at', 'asc') | ||
->setMaxResults(1) | ||
; | ||
|
||
$sql = $query->getSQL().' '.$this->dbal->getDatabasePlatform()->getWriteLockSQL(); | ||
|
||
$result = $this->dbal->executeQuery( | ||
$sql, | ||
[ | ||
'queues' => array_keys($queues), | ||
'delayedUntil' => time(), | ||
], | ||
[ | ||
'queues' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, | ||
'delayedUntil' => \Doctrine\DBAL\ParameterType::INTEGER, | ||
] | ||
)->fetch(); | ||
|
||
return $result ?: null; | ||
} | ||
|
||
private function fetchPrioritizedMessage(array $queues): ?array | ||
{ | ||
$query = $this->dbal->createQueryBuilder(); | ||
$query | ||
->select('*') | ||
->from($this->context->getTableName()) | ||
->andWhere('queue IN (:queues)') | ||
->andWhere('priority IS NOT NULL') | ||
->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)') | ||
->addOrderBy('published_at', 'asc') | ||
->addOrderBy('priority', 'desc') | ||
->setMaxResults(1) | ||
; | ||
|
||
$sql = $query->getSQL().' '.$this->dbal->getDatabasePlatform()->getWriteLockSQL(); | ||
|
||
$result = $this->dbal->executeQuery( | ||
$sql, | ||
[ | ||
'queues' => array_keys($queues), | ||
'delayedUntil' => time(), | ||
], | ||
[ | ||
'queues' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, | ||
'delayedUntil' => \Doctrine\DBAL\ParameterType::INTEGER, | ||
] | ||
)->fetch(); | ||
|
||
return $result ?: null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Dbal\Tests; | ||
|
||
use Enqueue\Dbal\DbalMessage; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->createMessage