-
Notifications
You must be signed in to change notification settings - Fork 434
/
DbalConsumerHelperTrait.php
157 lines (128 loc) · 5.39 KB
/
DbalConsumerHelperTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace Enqueue\Dbal;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Uuid;
trait DbalConsumerHelperTrait
{
private $redeliverMessagesLastExecutedAt;
private $removeExpiredMessagesLastExecutedAt;
abstract protected function getContext(): DbalContext;
abstract protected function getConnection(): Connection;
protected function fetchMessage(array $queues, int $redeliveryDelay): ?DbalMessage
{
if (empty($queues)) {
throw new \LogicException('Queues must not be empty.');
}
$now = time();
$deliveryId = Uuid::uuid4();
$endAt = microtime(true) + 0.2; // add 200ms
$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('published_at', 'asc')
->setParameter('queues', $queues, Connection::PARAM_STR_ARRAY)
->setParameter('delayedUntil', $now, ParameterType::INTEGER)
->setMaxResults(1);
$update = $this->getConnection()->createQueryBuilder()
->update($this->getContext()->getTableName())
->set('delivery_id', ':deliveryId')
->set('redeliver_after', ':redeliverAfter')
->andWhere('id = :messageId')
->andWhere('delivery_id IS NULL')
->setParameter('deliveryId', $deliveryId->getBytes(), Type::GUID)
->setParameter('redeliverAfter', $now + $redeliveryDelay, Type::BIGINT)
;
while (microtime(true) < $endAt) {
$result = $select->execute()->fetch();
if (empty($result)) {
return null;
}
$update
->setParameter('messageId', $result['id'], Type::GUID)
;
if ($update->execute()) {
$deliveredMessage = $this->getConnection()->createQueryBuilder()
->select('*')
->from($this->getContext()->getTableName())
->andWhere('delivery_id = :deliveryId')
->setParameter('deliveryId', $deliveryId->getBytes(), Type::GUID)
->setMaxResults(1)
->execute()
->fetch()
;
// the message has been removed by a 3rd party, such as truncate operation.
if (false == $deliveredMessage) {
continue;
}
if ($deliveredMessage['redelivered'] || empty($deliveredMessage['time_to_live']) || $deliveredMessage['time_to_live'] > time()) {
return $this->getContext()->convertMessage($deliveredMessage);
}
}
}
return null;
}
protected function redeliverMessages(): void
{
if (null === $this->redeliverMessagesLastExecutedAt) {
$this->redeliverMessagesLastExecutedAt = microtime(true);
} elseif ((microtime(true) - $this->redeliverMessagesLastExecutedAt) < 1) {
return;
}
$update = $this->getConnection()->createQueryBuilder()
->update($this->getContext()->getTableName())
->set('delivery_id', ':deliveryId')
->set('redelivered', ':redelivered')
->andWhere('redeliver_after < :now')
->andWhere('delivery_id IS NOT NULL')
->setParameter(':now', time(), Type::BIGINT)
->setParameter('deliveryId', null, Type::GUID)
->setParameter('redelivered', true, Type::BOOLEAN)
;
try {
$update->execute();
$this->redeliverMessagesLastExecutedAt = microtime(true);
} catch (DeadlockException $e) {
// maybe next time we'll get more luck
}
}
protected function removeExpiredMessages(): void
{
if (null === $this->removeExpiredMessagesLastExecutedAt) {
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
} elseif ((microtime(true) - $this->removeExpiredMessagesLastExecutedAt) < 1) {
return;
}
$delete = $this->getConnection()->createQueryBuilder()
->delete($this->getContext()->getTableName())
->andWhere('(time_to_live IS NOT NULL) AND (time_to_live < :now)')
->andWhere('delivery_id IS NULL')
->andWhere('redelivered = false')
->setParameter(':now', time(), Type::BIGINT)
;
try {
$delete->execute();
} catch (DeadlockException $e) {
// maybe next time we'll get more luck
}
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
}
private function deleteMessage(string $deliveryId): void
{
if (empty($deliveryId)) {
throw new \LogicException(sprintf('Expected record was removed but it is not. Delivery id: "%s"', $deliveryId));
}
$this->getConnection()->delete(
$this->getContext()->getTableName(),
['delivery_id' => Uuid::fromString($deliveryId)->getBytes()],
['delivery_id' => Type::GUID]
);
}
}