-
Notifications
You must be signed in to change notification settings - Fork 436
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
MongoDB Subscription Consumer feature #565
Merged
makasim
merged 5 commits into
php-enqueue:master
from
rosamarsky:mongodb-subscription-consumer
Oct 18, 2018
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63ee2fc
MongoDB Subscription Consumer feature
rosamarsky e57a76c
Replaced MongodbMessage::fromArrrayDbResult() with MongodbContext::co…
rosamarsky d049817
Replaced constructor with owned method createMessage() in MongodbCont…
rosamarsky d0ea85f
Added usleep 200ms when no messages are recieved
rosamarsky 4a62957
Added Subscription consumer to docs
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 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 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 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,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Enqueue\Mongodb; | ||
|
||
use Interop\Queue\Consumer; | ||
use Interop\Queue\SubscriptionConsumer; | ||
|
||
class MongodbSubscriptionConsumer implements SubscriptionConsumer | ||
{ | ||
/** | ||
* @var MongodbContext | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* an item contains an array: [MongodbConsumer $consumer, callable $callback];. | ||
* | ||
* @var array | ||
*/ | ||
private $subscribers; | ||
|
||
/** | ||
* @param MongodbContext $context | ||
*/ | ||
public function __construct(MongodbContext $context) | ||
{ | ||
$this->context = $context; | ||
$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; | ||
} | ||
|
||
$result = $this->context->getCollection()->findOneAndDelete( | ||
[ | ||
'queue' => ['$in' => array_keys($currentQueueNames)], | ||
'$or' => [ | ||
['delayed_until' => ['$exists' => false]], | ||
['delayed_until' => ['$lte' => time()]], | ||
], | ||
], | ||
[ | ||
'sort' => ['priority' => -1, 'published_at' => 1], | ||
'typeMap' => ['root' => 'array', 'document' => 'array'], | ||
] | ||
); | ||
|
||
if ($result) { | ||
list($consumer, $callback) = $this->subscribers[$result['queue']]; | ||
|
||
$message = $this->context->convertMessage($result); | ||
|
||
if (false === call_user_func($callback, $message, $consumer)) { | ||
return; | ||
} | ||
|
||
unset($currentQueueNames[$result['queue']]); | ||
} else { | ||
$currentQueueNames = []; | ||
} | ||
|
||
if ($timeout && microtime(true) >= $endAt) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param MongodbConsumer $consumer | ||
*/ | ||
public function subscribe(Consumer $consumer, callable $callback): void | ||
{ | ||
if (false == $consumer instanceof MongodbConsumer) { | ||
throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', MongodbConsumer::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 MongodbConsumer $consumer | ||
*/ | ||
public function unsubscribe(Consumer $consumer): void | ||
{ | ||
if (false == $consumer instanceof MongodbConsumer) { | ||
throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', MongodbConsumer::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 = []; | ||
} | ||
} |
This file contains 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
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()