| 1 |
from typing import Any |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from rq.exceptions import NoSuchJobError |
| 5 |
from rq.job import Job |
| 6 |
|
| 7 |
from django_rq.queues import get_connection, get_queue_by_index |
| 8 |
|
| 9 |
try: |
| 10 |
from redis.backoff import ExponentialWithJitterBackoff, NoBackoff # type: ignore[attr-defined] |
| 11 |
from redis.retry import Retry |
| 12 |
except ImportError: |
| 13 |
ExponentialWithJitterBackoff = None |
| 14 |
Retry = None # type: ignore[misc, assignment] |
| 15 |
|
| 16 |
|
| 17 |
def _is_buggy_retry(kwargs: dict[str, Any]) -> bool: |
| 18 |
return ( |
| 19 |
Retry is not None |
| 20 |
and (retry := kwargs.get('retry')) is not None |
| 21 |
and isinstance(retry, Retry) |
| 22 |
and isinstance(retry._backoff, ExponentialWithJitterBackoff) # type: ignore[attr-defined] |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
# redis-py >= 8 puts per-pool maintenance-notification objects in connection_kwargs, |
| 27 |
# and redis-py >= 8.1 adds a per-pool HImportRegistry; none define __eq__, so two |
| 28 |
# pools to the same Redis never compare equal |
| 29 |
_INCOMPARABLE_KWARGS = ('maint_notifications_pool_handler', 'maint_notifications_config', 'himport_registry') |
| 30 |
|
| 31 |
|
| 32 |
def _comparable_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]: |
| 33 |
return {key: value for key, value in kwargs.items() if key not in _INCOMPARABLE_KWARGS} |
| 34 |
|
| 35 |
|
| 36 |
def get_queue_index(name='default'): |
| 37 |
""" |
| 38 |
Returns the position of Queue for the named queue in QUEUES_LIST |
| 39 |
""" |
| 40 |
connection = get_connection(name) |
| 41 |
connection_kwargs = _comparable_kwargs(connection.connection_pool.connection_kwargs) |
| 42 |
|
| 43 |
for i in range(0, 100): |
| 44 |
try: |
| 45 |
q = get_queue_by_index(i) |
| 46 |
except AttributeError: |
| 47 |
continue |
| 48 |
if q.name == name: |
| 49 |
# assert that the connection is correct |
| 50 |
pool_kwargs = _comparable_kwargs(q.connection.connection_pool.connection_kwargs) |
| 51 |
if not _is_buggy_retry(pool_kwargs) or not _is_buggy_retry(connection_kwargs): |
| 52 |
assert pool_kwargs == connection_kwargs |
| 53 |
else: |
| 54 |
# patch the retry backoff since there is a bug in the default |
| 55 |
# backoff strategy |
| 56 |
# |
| 57 |
# fixed in https://github.com/redis/redis-py/pull/3668 |
| 58 |
with patch.object(pool_kwargs['retry'], '_backoff', NoBackoff()): |
| 59 |
with patch.object(connection_kwargs['retry'], '_backoff', NoBackoff()): |
| 60 |
assert pool_kwargs == connection_kwargs |
| 61 |
|
| 62 |
assert pool_kwargs['retry']._backoff.__dict__ == connection_kwargs['retry']._backoff.__dict__ |
| 63 |
|
| 64 |
return i |
| 65 |
|
| 66 |
return None |
| 67 |
|
| 68 |
|
| 69 |
def flush_registry(registry): |
| 70 |
"""Helper function to flush a registry and delete all jobs in it.""" |
| 71 |
connection = registry.connection |
| 72 |
for job_id in registry.get_job_ids(): |
| 73 |
connection.zrem(registry.key, job_id) |
| 74 |
try: |
| 75 |
job = Job.fetch(job_id, connection=connection) |
| 76 |
job.delete() |
| 77 |
except NoSuchJobError: |
| 78 |
pass |