Skip to content

Commit

Permalink
Adds retries to redis client, closes #1734.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtcppv committed Jun 20, 2024
1 parent b8e1b0e commit 36319a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
14 changes: 12 additions & 2 deletions api/main/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import redis
from redis.backoff import ExponentialBackoff
from redis.retry import Retry
from redis.client import Redis
from redis.exceptions import (
BusyLoadingError,
ConnectionError,
TimeoutError,
)
import json
import os
import logging
Expand All @@ -16,8 +23,11 @@ class TatorCache:

@classmethod
def setup_redis(cls):
cls.rds = redis.Redis(
retry = Retry(ExponentialBackoff(), 3)
cls.rds = Redis(
host=os.getenv("REDIS_HOST"),
retry=retry,
retry_on_error=[BusyLoadingError, ConnectionError, TimeoutError],
health_check_interval=30,
ssl=REDIS_USE_SSL,
)
Expand Down
15 changes: 14 additions & 1 deletion api/main/worker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from redis import Redis
from redis.backoff import ExponentialBackoff
from redis.retry import Retry
from redis.client import Redis
from redis.exceptions import (
BusyLoadingError,
ConnectionError,
TimeoutError,
)
from rq import Queue, Worker

import os
Expand Down Expand Up @@ -28,8 +35,11 @@ def push_job(queue, function, *args, **kwargs):
push_job('async_jobs', print, 'Hello')
See [https://python-rq.org/docs/] for information on available kwargs
"""
retry = Retry(ExponentialBackoff(), 3)
redis = Redis(
host=os.getenv("REDIS_HOST"),
retry=retry,
retry_on_error=[BusyLoadingError, ConnectionError, TimeoutError],
ssl=REDIS_USE_SSL,
)
queue = Queue(queue, connection=redis)
Expand All @@ -41,8 +51,11 @@ def push_job(queue, function, *args, **kwargs):
parser = argparse.ArgumentParser(description="Processes a python-rq queue.")
parser.add_argument("queue", help="Name of queue to process.")
args = parser.parse_args()
retry = Retry(ExponentialBackoff(), 3)
redis = Redis(
host=os.getenv("REDIS_HOST"),
retry=retry,
retry_on_error=[BusyLoadingError, ConnectionError, TimeoutError],
ssl=REDIS_USE_SSL,
)
queue = Queue(args.queue, connection=redis)
Expand Down

0 comments on commit 36319a4

Please sign in to comment.