Skip to content
Closed
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
22 changes: 20 additions & 2 deletions pkg/dbal/DbalConsumerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,34 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?DbalMessa

$endAt = microtime(true) + 0.2; // add 200ms

$selectQueryParameters = [
'queues' => $queues,
'delayedUntil' => $now,
];
$selectQueryParameterTypes = [
'queues' => Connection::PARAM_STR_ARRAY,
'delayedUntil' => DbalType::INTEGER,
];

$i = 0;
$orderByQueues = [];
foreach ($queues as $queue) {
$parameterName = 'queue'.$i;
$orderByQueues[] = sprintf('WHEN queue = :%s THEN %d', $parameterName, $i++);
$selectQueryParameters[$parameterName] = $queue;
$selectQueryParameterTypes[$parameterName] = DbalType::STRING;
}

$select = $this->getConnection()->createQueryBuilder()
->select('id')
->from($this->getContext()->getTableName())
->andWhere('queue IN (:queues)')
->andWhere('delayed_until IS NULL OR delayed_until <= :delayedUntil')
->andWhere('delivery_id IS NULL')
->addOrderBy('priority', 'asc')
->addOrderBy('CASE '.implode(' ', $orderByQueues).' ELSE -1 END')
->addOrderBy('published_at', 'asc')
->setParameter('queues', $queues, Connection::PARAM_STR_ARRAY)
->setParameter('delayedUntil', $now, DbalType::INTEGER)
->setParameters($selectQueryParameters, $selectQueryParameterTypes)
->setMaxResults(1);

$update = $this->getConnection()->createQueryBuilder()
Expand Down
8 changes: 6 additions & 2 deletions pkg/dbal/DbalSubscriptionConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public function consume(int $timeout = 0): void

$queueNames = [];
foreach (array_keys($this->subscribers) as $queueName) {
$queueNames[$queueName] = $queueName;
$queueNames[] = $queueName;
}
$queueNames = array_unique($queueNames);

$timeout /= 1000;
$now = time();
Expand All @@ -114,7 +115,10 @@ public function consume(int $timeout = 0): void
return;
}

unset($currentQueueNames[$message->getQueue()]);
$queueNames = array_filter($queueNames, static function ($queueName) use ($message) {
return $message->getQueue() !== $queueName;
});
$queueNames[] = $message->getQueue();
} else {
$currentQueueNames = [];

Expand Down