Repository navigation
Expand file tree
/
Copy pathMongodbConsumerHelperTrait.php
More file actions
154 lines (134 loc) · 4.56 KB
/
Copy pathMongodbConsumerHelperTrait.php
File metadata and controls
154 lines (134 loc) · 4.56 KB
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
<?php
declare(strict_types=1);
namespace Enqueue\Mongodb;
/**
* Claim-based consumption helper, ported from the DBAL transport
* (DbalConsumerHelperTrait) and CakeDC cakephp-enqueue (CakeConsumerHelperTrait).
*
* Instead of deleting a document on pop (at-most-once: a crash between pop
* and finish loses the job), a worker atomically claims it via `delivery_id`.
* A crashed claim expires through `redeliver_after` and becomes available
* again. `acknowledge()` deletes by `delivery_id`, so a slow worker can never
* delete a document already re-claimed by someone else.
*/
trait MongodbConsumerHelperTrait
{
/**
* @var float|null
*/
private $redeliverMessagesLastExecutedAt = null;
/**
* @var float|null
*/
private $removeExpiredMessagesLastExecutedAt = null;
/**
* @return \Enqueue\Mongodb\MongodbContext
*/
abstract protected function getContext(): MongodbContext;
/**
* Atomically claim one ready document of the given queues.
*
* @param array<string> $queueNames Transport queue names to pick from
* @param int $redeliveryDelay Seconds after which an un-acked claim becomes available again
* @return array|null The claimed document, null when nothing is ready
*/
protected function claimMessage(array $queueNames, int $redeliveryDelay): ?array
{
if (empty($queueNames)) {
return null;
}
$now = time();
$deliveryId = bin2hex(random_bytes(16));
$document = $this->getContext()->getCollection()->findOneAndUpdate(
[
'queue' => ['$in' => array_values($queueNames)],
'delayed_until' => ['$lte' => $now],
'delivery_id' => null,
],
[
'$set' => [
'delivery_id' => $deliveryId,
'redeliver_after' => $now + $redeliveryDelay,
],
],
[
'sort' => ['priority' => -1, 'published_at' => 1],
'typeMap' => ['root' => 'array', 'document' => 'array'],
'returnDocument' => \MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
]
);
if (!$document) {
return null;
}
if (
!empty($document['time_to_live'])
&& empty($document['redelivered'])
&& $document['time_to_live'] <= $now
) {
$this->deleteMessage($deliveryId);
return null;
}
return $document;
}
/**
* Delete a claimed document by its delivery id.
*/
protected function deleteMessage(string $deliveryId): void
{
if ($deliveryId === '') {
return;
}
$this->getContext()->getCollection()->deleteOne([
'delivery_id' => $deliveryId,
]);
}
/**
* Release expired claims back to ready state.
*/
protected function redeliverMessages(): void
{
if ($this->redeliverMessagesLastExecutedAt === null) {
$this->redeliverMessagesLastExecutedAt = microtime(true);
} elseif (microtime(true) - $this->redeliverMessagesLastExecutedAt < 1) {
return;
}
try {
$this->getContext()->getCollection()->updateMany(
[
'redeliver_after' => ['$lt' => time()],
'delivery_id' => ['$ne' => null],
],
[
'$set' => [
'delivery_id' => null,
'redelivered' => true,
],
]
);
$this->redeliverMessagesLastExecutedAt = microtime(true);
} catch (\Exception $e) {
// maybe next time we'll get more luck
}
}
/**
* Delete expired unclaimed messages.
*/
protected function removeExpiredMessages(): void
{
if ($this->removeExpiredMessagesLastExecutedAt === null) {
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
} elseif (microtime(true) - $this->removeExpiredMessagesLastExecutedAt < 1) {
return;
}
try {
$this->getContext()->getCollection()->deleteMany([
'time_to_live' => ['$ne' => null, '$lte' => time()],
'redelivered' => false,
'delivery_id' => null,
]);
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
} catch (\Exception $e) {
// maybe next time we'll get more luck
}
}
}