Skip to content

Commit

Permalink
Fix worker redis connection
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <arapgodsmack@gmail.com>
  • Loading branch information
KipSigei committed Jul 18, 2024
1 parent d70be69 commit dac82d4
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def get_db() -> Generator:

def get_redis_client() -> Generator:
try:
client = redis.from_url(str(settings.REDIS_URL))
client = redis.from_url(
str(settings.REDIS_URL), socket_timeout=30, socket_connect_timeout=30
)
yield client
finally:
client.close()
Expand Down
2 changes: 1 addition & 1 deletion app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from urllib.parse import quote_plus

from cryptography.fernet import Fernet
from pydantic import AnyHttpUrl, HttpUrl, RedisDsn, validator
from pydantic import AnyHttpUrl, HttpUrl, validator
from pydantic_settings import BaseSettings


Expand Down
4 changes: 2 additions & 2 deletions app/jobs/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from tableauhyperapi import HyperProcess, Telemetry

from app.common_tags import HYPER_PROCESS_CACHE_KEY
from app.settings import settings
from app.core.config import settings
from app.utils.onadata_utils import start_csv_import_to_hyper


def csv_import_job(instance_id):
# Connect to redis cache
rc = RedisCacheBackend(settings.redis_url)
rc = RedisCacheBackend(settings.REDIS_URL)
caches.set(CACHE_KEY, rc)

# Check if Hyper Process has started
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
CRON_SCHEDULE = os.environ.get("CRON_SCHEDULE", "*/15 * * * *")
TASK_TIMEOUT = os.environ.get("TASK_TIMEOUT", "3600")
REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379/1")
REDIS_CONN = Redis.from_url(REDIS_URL)
REDIS_CONN = Redis.from_url(REDIS_URL, socket_timeout=30, socket_connect_timeout=30)
QUEUE = Queue(QUEUE_NAME, connection=REDIS_CONN)


Expand Down
8 changes: 5 additions & 3 deletions app/jobs/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
from rq import Connection, Queue, Worker
from sentry_sdk.integrations.rq import RqIntegration

from app.settings import settings
from app.core.config import settings

# Preload libraries

QUEUE_NAME = os.environ.get("QUEUE_NAME", "default")


redis_conn = Redis.from_url(settings.redis_url)
redis_conn = Redis.from_url(
settings.REDIS_URL, socket_timeout=30, socket_connect_timeout=30
)

# Provide queue names to listen to as arguments to this script,
# similar to rq worker
with Connection():
if settings.sentry_dsn:
if settings.SENTRY_DSN:
sentry_sdk.init(settings.sentry_dsn, integrations=[RqIntegration()])
queue = Queue(QUEUE_NAME, connection=redis_conn)

Expand Down
4 changes: 2 additions & 2 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import redis

from app.database.session import SessionLocal
from app.settings import settings
from app.core.config import settings


def get_db():
Expand All @@ -19,7 +19,7 @@ def get_db():

def get_redis_client():
redis_client = redis.Redis(
host=settings.redis_host, port=settings.redis_port, db=settings.redis_db
host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_DB
)
try:
yield redis_client
Expand Down

0 comments on commit dac82d4

Please sign in to comment.