taskqx 0.7.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- taskqx/__init__.py +118 -0
- taskqx/__main__.py +5 -0
- taskqx/admin.py +29 -0
- taskqx/broker/__init__.py +12 -0
- taskqx/broker/_time.py +25 -0
- taskqx/broker/redis.py +1665 -0
- taskqx/broker/redis_admin.py +395 -0
- taskqx/broker/redis_calls.py +387 -0
- taskqx/broker/redis_components.py +111 -0
- taskqx/broker/redis_maintenance.py +117 -0
- taskqx/broker/redis_observability.py +120 -0
- taskqx/broker/redis_scripts.py +177 -0
- taskqx/broker/redis_state_machine.py +214 -0
- taskqx/broker/sqlite.py +1385 -0
- taskqx/broker/sqlite_admin.py +469 -0
- taskqx/broker/sqlite_components.py +124 -0
- taskqx/broker/sqlite_maintenance.py +218 -0
- taskqx/broker/sqlite_migrations.py +57 -0
- taskqx/broker/sqlite_state_machine.py +363 -0
- taskqx/capabilities.py +46 -0
- taskqx/cli.py +227 -0
- taskqx/config.py +57 -0
- taskqx/consistency.py +50 -0
- taskqx/errors.py +37 -0
- taskqx/health.py +27 -0
- taskqx/interactive.py +40 -0
- taskqx/middleware.py +39 -0
- taskqx/naming.py +35 -0
- taskqx/observability.py +158 -0
- taskqx/pagination.py +43 -0
- taskqx/payloads.py +208 -0
- taskqx/protocols.py +180 -0
- taskqx/py.typed +1 -0
- taskqx/retry.py +139 -0
- taskqx/scheduler.py +134 -0
- taskqx/serialization.py +78 -0
- taskqx/shell.py +303 -0
- taskqx/submission/__init__.py +19 -0
- taskqx/submission/base.py +62 -0
- taskqx/submission/observability.py +71 -0
- taskqx/submission/redis.py +180 -0
- taskqx/submission/routing.py +93 -0
- taskqx/submission/service.py +110 -0
- taskqx/submission/sqlite.py +161 -0
- taskqx/tui.py +1048 -0
- taskqx/types.py +280 -0
- taskqx/worker.py +300 -0
- taskqx-0.7.0.dev0.dist-info/METADATA +275 -0
- taskqx-0.7.0.dev0.dist-info/RECORD +52 -0
- taskqx-0.7.0.dev0.dist-info/WHEEL +4 -0
- taskqx-0.7.0.dev0.dist-info/entry_points.txt +2 -0
- taskqx-0.7.0.dev0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
"""Typed Redis Lua invocation layouts.
|
|
2
|
+
|
|
3
|
+
Keeping these builders beside the scripts makes the Python/Lua boundary explicit:
|
|
4
|
+
every ``KEYS[n]`` and ``ARGV[n]`` position is constructed in one place and can
|
|
5
|
+
be asserted without a Redis server.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import base64
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from typing import Any, Protocol
|
|
13
|
+
|
|
14
|
+
from ..submission.base import PreparedSubmission
|
|
15
|
+
from ._time import timestamp as _timestamp
|
|
16
|
+
from .redis_scripts import (
|
|
17
|
+
BATCH_SUBMIT,
|
|
18
|
+
CLAIM,
|
|
19
|
+
CLEANUP_ACKED,
|
|
20
|
+
DUE_DELAYED,
|
|
21
|
+
EXPIRE,
|
|
22
|
+
EXTEND_LEASE,
|
|
23
|
+
FINISH,
|
|
24
|
+
PEL_RECOVER,
|
|
25
|
+
RECLAIM_LEASE,
|
|
26
|
+
REPLAY_DEAD_LETTER,
|
|
27
|
+
REPLAY_EXPIRED,
|
|
28
|
+
SUBMIT,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True, slots=True)
|
|
33
|
+
class RedisScriptCall:
|
|
34
|
+
"""A fully specified ``EVAL`` call, excluding the Redis client itself."""
|
|
35
|
+
|
|
36
|
+
script: str
|
|
37
|
+
keys: tuple[str, ...]
|
|
38
|
+
args: tuple[str, ...]
|
|
39
|
+
|
|
40
|
+
async def execute(self, redis: Any) -> Any:
|
|
41
|
+
return await redis.eval(self.script, len(self.keys), *self.keys, *self.args)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class RedisKeyspace(Protocol):
|
|
45
|
+
"""Stable internal key-building contract shared by Redis script callers."""
|
|
46
|
+
|
|
47
|
+
def _message_key(self, queue: str, message_id: str) -> str: ...
|
|
48
|
+
def _queue_key(self, queue: str, kind: str) -> str: ...
|
|
49
|
+
def _message_index_key(self) -> str: ...
|
|
50
|
+
def _group_name(self) -> str: ...
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def submit_call(
|
|
54
|
+
backend: RedisKeyspace, submission: PreparedSubmission, dedup_key: str
|
|
55
|
+
) -> RedisScriptCall:
|
|
56
|
+
"""Build the single-message submit call (eight keys, fifteen args)."""
|
|
57
|
+
expires_at = (
|
|
58
|
+
submission.expires_at_ms / 1000 if submission.expires_at_ms is not None else 0
|
|
59
|
+
)
|
|
60
|
+
now = str(_timestamp(submission.created_at))
|
|
61
|
+
return RedisScriptCall(
|
|
62
|
+
SUBMIT,
|
|
63
|
+
(
|
|
64
|
+
dedup_key,
|
|
65
|
+
backend._message_key(submission.queue, submission.message_id),
|
|
66
|
+
backend._queue_key(submission.queue, "stream"),
|
|
67
|
+
backend._queue_key(submission.queue, "expiry"),
|
|
68
|
+
backend._queue_key(submission.queue, "eq"),
|
|
69
|
+
backend._queue_key(submission.queue, "stats"),
|
|
70
|
+
backend._queue_key(submission.queue, "ready"),
|
|
71
|
+
backend._queue_key(submission.queue, "delayed"),
|
|
72
|
+
),
|
|
73
|
+
(
|
|
74
|
+
dedup_key,
|
|
75
|
+
submission.message_id,
|
|
76
|
+
str(submission.dedup_ttl_ms or 0),
|
|
77
|
+
base64.b64encode(submission.envelope).decode("ascii"),
|
|
78
|
+
submission.queue,
|
|
79
|
+
submission.status,
|
|
80
|
+
str(submission.max_attempts),
|
|
81
|
+
now,
|
|
82
|
+
str(expires_at),
|
|
83
|
+
submission.serializer_name,
|
|
84
|
+
submission.serializer_version,
|
|
85
|
+
now,
|
|
86
|
+
str(
|
|
87
|
+
submission.available_at_ms / 1000
|
|
88
|
+
if submission.available_at_ms is not None
|
|
89
|
+
else 0
|
|
90
|
+
),
|
|
91
|
+
submission.workflow_id or "",
|
|
92
|
+
submission.parent_id or "",
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def batch_submit_call(
|
|
98
|
+
backend: RedisKeyspace, submissions: list[PreparedSubmission], dedup_keys: list[str]
|
|
99
|
+
) -> RedisScriptCall:
|
|
100
|
+
"""Build one atomic batch-submit call (eight keys / fifteen args each)."""
|
|
101
|
+
keys: list[str] = []
|
|
102
|
+
args: list[str] = [str(len(submissions))]
|
|
103
|
+
for submission, dedup_key in zip(submissions, dedup_keys, strict=True):
|
|
104
|
+
call = submit_call(backend, submission, dedup_key)
|
|
105
|
+
keys.extend(call.keys)
|
|
106
|
+
args.extend(call.args)
|
|
107
|
+
return RedisScriptCall(BATCH_SUBMIT, tuple(keys), tuple(args))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def claim_call(
|
|
111
|
+
backend: RedisKeyspace,
|
|
112
|
+
*,
|
|
113
|
+
queue: str,
|
|
114
|
+
message_id: str,
|
|
115
|
+
now: float,
|
|
116
|
+
consumer_id: str,
|
|
117
|
+
delivery_id: str,
|
|
118
|
+
token: str,
|
|
119
|
+
lease_until: float,
|
|
120
|
+
entry_id: str,
|
|
121
|
+
) -> RedisScriptCall:
|
|
122
|
+
return RedisScriptCall(
|
|
123
|
+
CLAIM,
|
|
124
|
+
(
|
|
125
|
+
backend._message_key(queue, message_id),
|
|
126
|
+
backend._queue_key(queue, "leases"),
|
|
127
|
+
backend._queue_key(queue, "eq"),
|
|
128
|
+
backend._queue_key(queue, "expiry"),
|
|
129
|
+
backend._queue_key(queue, "stream"),
|
|
130
|
+
backend._queue_key(queue, "ready"),
|
|
131
|
+
),
|
|
132
|
+
(
|
|
133
|
+
str(now),
|
|
134
|
+
message_id,
|
|
135
|
+
consumer_id,
|
|
136
|
+
delivery_id,
|
|
137
|
+
token,
|
|
138
|
+
str(lease_until),
|
|
139
|
+
backend._group_name(),
|
|
140
|
+
entry_id,
|
|
141
|
+
),
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def finish_call(
|
|
146
|
+
backend: RedisKeyspace,
|
|
147
|
+
*,
|
|
148
|
+
queue: str,
|
|
149
|
+
message_id: str,
|
|
150
|
+
action: str,
|
|
151
|
+
delivery_id: str,
|
|
152
|
+
token: str,
|
|
153
|
+
now: float,
|
|
154
|
+
reason: str,
|
|
155
|
+
error_type: str,
|
|
156
|
+
retry_available_at: float | None,
|
|
157
|
+
max_attempts: int | None,
|
|
158
|
+
ack_tombstone_ttl: float,
|
|
159
|
+
) -> RedisScriptCall:
|
|
160
|
+
return RedisScriptCall(
|
|
161
|
+
FINISH,
|
|
162
|
+
tuple(
|
|
163
|
+
backend._queue_key(queue, kind)
|
|
164
|
+
if kind != "message"
|
|
165
|
+
else backend._message_key(queue, message_id)
|
|
166
|
+
for kind in (
|
|
167
|
+
"message",
|
|
168
|
+
"leases",
|
|
169
|
+
"expiry",
|
|
170
|
+
"stream",
|
|
171
|
+
"eq",
|
|
172
|
+
"stats",
|
|
173
|
+
"dlq",
|
|
174
|
+
"ready",
|
|
175
|
+
"delayed",
|
|
176
|
+
"retention",
|
|
177
|
+
)
|
|
178
|
+
),
|
|
179
|
+
(
|
|
180
|
+
action,
|
|
181
|
+
delivery_id,
|
|
182
|
+
token,
|
|
183
|
+
str(now),
|
|
184
|
+
message_id,
|
|
185
|
+
reason,
|
|
186
|
+
error_type,
|
|
187
|
+
backend._group_name(),
|
|
188
|
+
str(retry_available_at) if retry_available_at is not None else "0",
|
|
189
|
+
str(max_attempts or 0),
|
|
190
|
+
str(ack_tombstone_ttl),
|
|
191
|
+
),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def extend_lease_call(
|
|
196
|
+
backend: RedisKeyspace,
|
|
197
|
+
*,
|
|
198
|
+
queue: str,
|
|
199
|
+
message_id: str,
|
|
200
|
+
delivery_id: str,
|
|
201
|
+
token: str,
|
|
202
|
+
lease_until: float,
|
|
203
|
+
) -> RedisScriptCall:
|
|
204
|
+
return RedisScriptCall(
|
|
205
|
+
EXTEND_LEASE,
|
|
206
|
+
(
|
|
207
|
+
backend._message_key(queue, message_id),
|
|
208
|
+
backend._queue_key(queue, "leases"),
|
|
209
|
+
backend._queue_key(queue, "stream"),
|
|
210
|
+
backend._queue_key(queue, "expiry"),
|
|
211
|
+
backend._queue_key(queue, "eq"),
|
|
212
|
+
),
|
|
213
|
+
(delivery_id, token, str(lease_until), message_id, backend._group_name()),
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def pel_recover_call(
|
|
218
|
+
backend: RedisKeyspace, *, queue: str, message_id: str, entry_id: str
|
|
219
|
+
) -> RedisScriptCall:
|
|
220
|
+
return RedisScriptCall(
|
|
221
|
+
PEL_RECOVER,
|
|
222
|
+
(
|
|
223
|
+
backend._message_key(queue, message_id),
|
|
224
|
+
backend._queue_key(queue, "stream"),
|
|
225
|
+
backend._queue_key(queue, "stats"),
|
|
226
|
+
),
|
|
227
|
+
(backend._group_name(), entry_id, message_id),
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def due_delayed_call(
|
|
232
|
+
backend: RedisKeyspace, *, queue: str, message_id: str, now: float
|
|
233
|
+
) -> RedisScriptCall:
|
|
234
|
+
return RedisScriptCall(
|
|
235
|
+
DUE_DELAYED,
|
|
236
|
+
tuple(
|
|
237
|
+
backend._queue_key(queue, kind)
|
|
238
|
+
if kind != "message"
|
|
239
|
+
else backend._message_key(queue, message_id)
|
|
240
|
+
for kind in ("message", "delayed", "ready", "stream", "eq", "expiry")
|
|
241
|
+
),
|
|
242
|
+
(str(now), message_id),
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def expire_call(
|
|
247
|
+
backend: RedisKeyspace, *, queue: str, message_id: str, now: float
|
|
248
|
+
) -> RedisScriptCall:
|
|
249
|
+
return RedisScriptCall(
|
|
250
|
+
EXPIRE,
|
|
251
|
+
tuple(
|
|
252
|
+
backend._queue_key(queue, kind)
|
|
253
|
+
if kind != "message"
|
|
254
|
+
else backend._message_key(queue, message_id)
|
|
255
|
+
for kind in (
|
|
256
|
+
"message",
|
|
257
|
+
"leases",
|
|
258
|
+
"expiry",
|
|
259
|
+
"stream",
|
|
260
|
+
"eq",
|
|
261
|
+
"ready",
|
|
262
|
+
"delayed",
|
|
263
|
+
)
|
|
264
|
+
),
|
|
265
|
+
(str(now), backend._group_name(), message_id),
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def reclaim_lease_call(
|
|
270
|
+
backend: RedisKeyspace, *, queue: str, message_id: str, now: float
|
|
271
|
+
) -> RedisScriptCall:
|
|
272
|
+
return RedisScriptCall(
|
|
273
|
+
RECLAIM_LEASE,
|
|
274
|
+
tuple(
|
|
275
|
+
backend._queue_key(queue, kind)
|
|
276
|
+
if kind != "message"
|
|
277
|
+
else backend._message_key(queue, message_id)
|
|
278
|
+
for kind in (
|
|
279
|
+
"message",
|
|
280
|
+
"leases",
|
|
281
|
+
"expiry",
|
|
282
|
+
"stream",
|
|
283
|
+
"dlq",
|
|
284
|
+
"stats",
|
|
285
|
+
"eq",
|
|
286
|
+
"ready",
|
|
287
|
+
)
|
|
288
|
+
),
|
|
289
|
+
(str(now), backend._group_name(), message_id),
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def cleanup_acked_call(
|
|
294
|
+
backend: RedisKeyspace, *, queue: str, message_id: str, now: float
|
|
295
|
+
) -> RedisScriptCall:
|
|
296
|
+
"""Build conditional ACK payload pruning for a due tombstone."""
|
|
297
|
+
|
|
298
|
+
return RedisScriptCall(
|
|
299
|
+
CLEANUP_ACKED,
|
|
300
|
+
(
|
|
301
|
+
backend._message_key(queue, message_id),
|
|
302
|
+
backend._queue_key(queue, "retention"),
|
|
303
|
+
),
|
|
304
|
+
(str(now), message_id),
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def replay_dead_letter_call(
|
|
309
|
+
backend: RedisKeyspace,
|
|
310
|
+
*,
|
|
311
|
+
source_queue: str,
|
|
312
|
+
target_queue: str,
|
|
313
|
+
message_id: str,
|
|
314
|
+
envelope: str,
|
|
315
|
+
attempt: str,
|
|
316
|
+
expires_at: float,
|
|
317
|
+
now: float,
|
|
318
|
+
old_dedup_key: str,
|
|
319
|
+
new_dedup_key: str,
|
|
320
|
+
keep: bool,
|
|
321
|
+
replacement_ttl: int,
|
|
322
|
+
) -> RedisScriptCall:
|
|
323
|
+
return RedisScriptCall(
|
|
324
|
+
REPLAY_DEAD_LETTER,
|
|
325
|
+
(
|
|
326
|
+
backend._message_key(source_queue, message_id),
|
|
327
|
+
backend._queue_key(source_queue, "dlq"),
|
|
328
|
+
backend._queue_key(target_queue, "stream"),
|
|
329
|
+
backend._queue_key(target_queue, "expiry"),
|
|
330
|
+
backend._queue_key(target_queue, "ready"),
|
|
331
|
+
old_dedup_key,
|
|
332
|
+
new_dedup_key,
|
|
333
|
+
backend._message_key(target_queue, message_id),
|
|
334
|
+
backend._message_index_key(),
|
|
335
|
+
)
|
|
336
|
+
,
|
|
337
|
+
(
|
|
338
|
+
message_id,
|
|
339
|
+
envelope,
|
|
340
|
+
target_queue,
|
|
341
|
+
attempt,
|
|
342
|
+
str(expires_at),
|
|
343
|
+
str(now),
|
|
344
|
+
"1" if keep else "0",
|
|
345
|
+
str(replacement_ttl),
|
|
346
|
+
),
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def replay_expired_call(
|
|
351
|
+
backend: RedisKeyspace,
|
|
352
|
+
*,
|
|
353
|
+
source_queue: str,
|
|
354
|
+
target_queue: str,
|
|
355
|
+
message_id: str,
|
|
356
|
+
envelope: str,
|
|
357
|
+
expires_at: float,
|
|
358
|
+
now: float,
|
|
359
|
+
old_dedup_key: str,
|
|
360
|
+
new_dedup_key: str,
|
|
361
|
+
keep: bool,
|
|
362
|
+
replacement_ttl: int,
|
|
363
|
+
) -> RedisScriptCall:
|
|
364
|
+
return RedisScriptCall(
|
|
365
|
+
REPLAY_EXPIRED,
|
|
366
|
+
(
|
|
367
|
+
backend._message_key(source_queue, message_id),
|
|
368
|
+
backend._queue_key(source_queue, "eq"),
|
|
369
|
+
backend._queue_key(target_queue, "stream"),
|
|
370
|
+
backend._queue_key(target_queue, "expiry"),
|
|
371
|
+
backend._queue_key(target_queue, "ready"),
|
|
372
|
+
old_dedup_key,
|
|
373
|
+
new_dedup_key,
|
|
374
|
+
backend._message_key(target_queue, message_id),
|
|
375
|
+
backend._message_index_key(),
|
|
376
|
+
)
|
|
377
|
+
,
|
|
378
|
+
(
|
|
379
|
+
message_id,
|
|
380
|
+
envelope,
|
|
381
|
+
str(expires_at),
|
|
382
|
+
str(now),
|
|
383
|
+
"1" if keep else "0",
|
|
384
|
+
str(replacement_ttl),
|
|
385
|
+
target_queue,
|
|
386
|
+
),
|
|
387
|
+
)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Redis backend 的 Delivery 与 Consumer 运行时组件。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from collections.abc import AsyncIterator
|
|
7
|
+
from datetime import datetime, timedelta
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
from ..errors import ValidationError
|
|
13
|
+
from ..types import ConsumerOptions, FinishOutcome, TaskMessage
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from .redis import RedisBroker
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RedisDelivery:
|
|
20
|
+
"""Redis lease token 保护的一次投递上下文。"""
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
broker: RedisBroker,
|
|
25
|
+
message: TaskMessage,
|
|
26
|
+
delivery_id: str,
|
|
27
|
+
token: str,
|
|
28
|
+
consumer_id: str,
|
|
29
|
+
attempt: int,
|
|
30
|
+
claimed_at: datetime,
|
|
31
|
+
lease_until: datetime,
|
|
32
|
+
) -> None:
|
|
33
|
+
self._broker, self._lease_token = broker, token
|
|
34
|
+
self._lease_seconds = (lease_until - claimed_at).total_seconds()
|
|
35
|
+
self.message, self.delivery_id, self.consumer_id, self.attempt = (
|
|
36
|
+
message,
|
|
37
|
+
delivery_id,
|
|
38
|
+
consumer_id,
|
|
39
|
+
attempt,
|
|
40
|
+
)
|
|
41
|
+
self.claimed_at, self.lease_until = claimed_at, lease_until
|
|
42
|
+
|
|
43
|
+
async def ack(self) -> FinishOutcome:
|
|
44
|
+
return await self._broker._finish(self, "ack")
|
|
45
|
+
|
|
46
|
+
async def retry(
|
|
47
|
+
self,
|
|
48
|
+
*,
|
|
49
|
+
reason: str | None = None,
|
|
50
|
+
delay: timedelta | None = None,
|
|
51
|
+
max_attempts: int | None = None,
|
|
52
|
+
) -> FinishOutcome:
|
|
53
|
+
return await self._broker._finish(
|
|
54
|
+
self, "retry", reason, delay=delay, max_attempts=max_attempts
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
async def reject(
|
|
58
|
+
self, *, reason: str, error: BaseException | None = None
|
|
59
|
+
) -> FinishOutcome:
|
|
60
|
+
if not reason:
|
|
61
|
+
raise ValidationError("reject 必须提供非空 reason")
|
|
62
|
+
return await self._broker._finish(self, "reject", reason, error)
|
|
63
|
+
|
|
64
|
+
async def extend_lease(self, *, seconds: float | None = None) -> datetime:
|
|
65
|
+
self.lease_until = await self._broker._extend(self, seconds)
|
|
66
|
+
return self.lease_until
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class RedisConsumer:
|
|
70
|
+
"""Redis Stream Consumer Group 的异步投递迭代器。"""
|
|
71
|
+
|
|
72
|
+
def __init__(
|
|
73
|
+
self,
|
|
74
|
+
broker: RedisBroker,
|
|
75
|
+
queue: str,
|
|
76
|
+
consumer_id: str,
|
|
77
|
+
options: ConsumerOptions,
|
|
78
|
+
) -> None:
|
|
79
|
+
self._broker, self.queue, self.consumer_id, self.options = (
|
|
80
|
+
broker,
|
|
81
|
+
queue,
|
|
82
|
+
consumer_id,
|
|
83
|
+
options,
|
|
84
|
+
)
|
|
85
|
+
self._closed = False
|
|
86
|
+
|
|
87
|
+
async def start(self) -> None:
|
|
88
|
+
await self._broker.start()
|
|
89
|
+
|
|
90
|
+
async def close(self) -> None:
|
|
91
|
+
self._closed = True
|
|
92
|
+
|
|
93
|
+
async def __aenter__(self) -> Self:
|
|
94
|
+
await self.start()
|
|
95
|
+
return self
|
|
96
|
+
|
|
97
|
+
async def __aexit__(self, *_: object) -> None:
|
|
98
|
+
await self.close()
|
|
99
|
+
|
|
100
|
+
def __aiter__(self) -> AsyncIterator[RedisDelivery]:
|
|
101
|
+
return self
|
|
102
|
+
|
|
103
|
+
async def __anext__(self) -> RedisDelivery:
|
|
104
|
+
while not self._closed:
|
|
105
|
+
delivery = await self._broker._claim(
|
|
106
|
+
self.queue, self.consumer_id, self.options.lease_seconds
|
|
107
|
+
)
|
|
108
|
+
if delivery is not None:
|
|
109
|
+
return delivery
|
|
110
|
+
await asyncio.sleep(self.options.poll_interval)
|
|
111
|
+
raise StopAsyncIteration
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Redis delayed, expiry, lease-reclaim and PEL-recovery maintenance."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from ..types import MessageStatus
|
|
8
|
+
from ._time import timestamp as _timestamp
|
|
9
|
+
from .redis_calls import (
|
|
10
|
+
cleanup_acked_call,
|
|
11
|
+
due_delayed_call,
|
|
12
|
+
expire_call,
|
|
13
|
+
reclaim_lease_call,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from .redis import RedisBroker
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class RedisMaintenance:
|
|
21
|
+
"""Run committed Redis maintenance transitions and delegate their observation."""
|
|
22
|
+
|
|
23
|
+
def __init__(self, broker: RedisBroker) -> None:
|
|
24
|
+
self._broker = broker
|
|
25
|
+
|
|
26
|
+
async def run(self, queue: str | None = None) -> int:
|
|
27
|
+
if queue is None:
|
|
28
|
+
return 0
|
|
29
|
+
broker = self._broker
|
|
30
|
+
await broker._ensure_group(queue)
|
|
31
|
+
now = await broker._now()
|
|
32
|
+
reclaimed = await broker._recover_uncommitted_pel(queue)
|
|
33
|
+
for message_id in await broker._redis.zrangebyscore(
|
|
34
|
+
broker._queue_key(queue, "delayed"), "-inf", _timestamp(now)
|
|
35
|
+
):
|
|
36
|
+
moved = int(
|
|
37
|
+
await due_delayed_call(
|
|
38
|
+
broker,
|
|
39
|
+
queue=queue,
|
|
40
|
+
message_id=message_id,
|
|
41
|
+
now=_timestamp(now),
|
|
42
|
+
).execute(broker._redis)
|
|
43
|
+
)
|
|
44
|
+
reclaimed += int(bool(moved))
|
|
45
|
+
if moved == 1:
|
|
46
|
+
await broker._emit_maintenance_event(
|
|
47
|
+
queue, message_id, "due", MessageStatus.READY.value
|
|
48
|
+
)
|
|
49
|
+
elif moved == 2:
|
|
50
|
+
await broker._emit_maintenance_event(
|
|
51
|
+
queue,
|
|
52
|
+
message_id,
|
|
53
|
+
"expired",
|
|
54
|
+
MessageStatus.EXPIRED.value,
|
|
55
|
+
reason="expired_from:delayed",
|
|
56
|
+
metric_name="expired_total",
|
|
57
|
+
)
|
|
58
|
+
for message_id in await broker._redis.zrangebyscore(
|
|
59
|
+
broker._queue_key(queue, "expiry"), "-inf", _timestamp(now)
|
|
60
|
+
):
|
|
61
|
+
moved = int(
|
|
62
|
+
await expire_call(
|
|
63
|
+
broker,
|
|
64
|
+
queue=queue,
|
|
65
|
+
message_id=message_id,
|
|
66
|
+
now=_timestamp(now),
|
|
67
|
+
).execute(broker._redis)
|
|
68
|
+
)
|
|
69
|
+
if moved:
|
|
70
|
+
await broker._emit_maintenance_event(
|
|
71
|
+
queue,
|
|
72
|
+
message_id,
|
|
73
|
+
"expired",
|
|
74
|
+
MessageStatus.EXPIRED.value,
|
|
75
|
+
reason="expired_by_maintenance",
|
|
76
|
+
metric_name="expired_total",
|
|
77
|
+
)
|
|
78
|
+
for message_id in await broker._redis.zrangebyscore(
|
|
79
|
+
broker._queue_key(queue, "leases"), "-inf", _timestamp(now)
|
|
80
|
+
):
|
|
81
|
+
result = int(
|
|
82
|
+
await reclaim_lease_call(
|
|
83
|
+
broker,
|
|
84
|
+
queue=queue,
|
|
85
|
+
message_id=message_id,
|
|
86
|
+
now=_timestamp(now),
|
|
87
|
+
).execute(broker._redis)
|
|
88
|
+
)
|
|
89
|
+
reclaimed += int(bool(result))
|
|
90
|
+
transitions = {
|
|
91
|
+
1: ("reclaimed", MessageStatus.READY.value, "reclaimed_total"),
|
|
92
|
+
2: (
|
|
93
|
+
"dead_lettered",
|
|
94
|
+
MessageStatus.DEAD_LETTERED.value,
|
|
95
|
+
"dead_lettered_total",
|
|
96
|
+
),
|
|
97
|
+
3: ("expired", MessageStatus.EXPIRED.value, "expired_total"),
|
|
98
|
+
}
|
|
99
|
+
if result in transitions:
|
|
100
|
+
name, status, metric_name = transitions[result]
|
|
101
|
+
reason = "expired_from:leased" if result == 3 else "lease_timeout"
|
|
102
|
+
await broker._emit_maintenance_event(
|
|
103
|
+
queue, message_id, name, status, reason=reason, metric_name=metric_name
|
|
104
|
+
)
|
|
105
|
+
for message_id in await broker._redis.zrangebyscore(
|
|
106
|
+
broker._queue_key(queue, "retention"), "-inf", _timestamp(now)
|
|
107
|
+
):
|
|
108
|
+
cleaned = int(
|
|
109
|
+
await cleanup_acked_call(
|
|
110
|
+
broker,
|
|
111
|
+
queue=queue,
|
|
112
|
+
message_id=message_id,
|
|
113
|
+
now=_timestamp(now),
|
|
114
|
+
).execute(broker._redis)
|
|
115
|
+
)
|
|
116
|
+
reclaimed += cleaned
|
|
117
|
+
return reclaimed
|