Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove django-celery-results and replace by redis #995

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/backend/settings/api/events/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django_celery_results",
"rest_framework",
"api",
"substrapp",
Expand Down
1 change: 0 additions & 1 deletion backend/backend/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
"django_celery_results",
"rest_framework",
"rest_framework_simplejwt.token_blacklist",
"substrapp",
Expand Down
17 changes: 13 additions & 4 deletions backend/backend/settings/deps/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,47 @@
"""

import os
import typing

from substrapp.compute_tasks.errors import CeleryRetryError

from .org import ORG_NAME


def build_broker_url(user: str, password: str, host: str, port: str) -> str:
def build_redis_url(user: str, password: str, host: str, port: str, db: typing.Optional[int] = None) -> str:
"""Builds a redis connection string

Args:
user (str): redis user
password (str): redis password
host (str): redis hostname
port (str): redis port
db (typing.Optional[str]): redis database. Accepted values are None (default value) or 0 to 15.

Returns:
str: a connection string of the form "redis://user:password@hostname:port//"
str: a connection string of the form "redis://user:password@hostname:port/db"
"""
conn_info = ""
conn_port = ""
conn_db = ""
if user and password:
conn_info = f"{user}:{password}@"
if port:
conn_port = f":{port}"
return f"redis://{conn_info}{host}{conn_port}//"
if db is not None:
conn_db = str(db)
return f"redis://{conn_info}{host}{conn_port}/{conn_db}/"


CELERY_BROKER_USER = os.environ.get("CELERY_BROKER_USER")
CELERY_BROKER_PASSWORD = os.environ.get("CELERY_BROKER_PASSWORD")
CELERY_BROKER_HOST = os.environ.get("CELERY_BROKER_HOST", "localhost")
CELERY_BROKER_PORT = os.environ.get("CELERY_BROKER_PORT", "5672")
CELERY_BROKER_URL = build_broker_url(CELERY_BROKER_USER, CELERY_BROKER_PASSWORD, CELERY_BROKER_HOST, CELERY_BROKER_PORT)
CELERY_BROKER_URL = build_redis_url(CELERY_BROKER_USER, CELERY_BROKER_PASSWORD, CELERY_BROKER_HOST, CELERY_BROKER_PORT)

CELERY_RESULT_BACKEND = build_redis_url(
CELERY_BROKER_USER, CELERY_BROKER_PASSWORD, CELERY_BROKER_HOST, CELERY_BROKER_PORT, db=1
)

CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_RESULT_SERIALIZER = "json"
Expand Down
1 change: 0 additions & 1 deletion backend/backend/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@
SITE_PORT = DEFAULT_PORT
DEFAULT_DOMAIN = os.environ.get("DEFAULT_DOMAIN", f"http://{SITE_HOST}:{SITE_PORT}")

CELERY_RESULT_BACKEND = "django-db"
CELERY_TASK_MAX_RETRIES = int(os.environ.get("CELERY_TASK_MAX_RETRIES", 0))
2 changes: 0 additions & 2 deletions backend/backend/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
SITE_PORT = os.environ.get("SITE_PORT", DEFAULT_PORT)
DEFAULT_DOMAIN = os.environ.get("DEFAULT_DOMAIN", f"http://{SITE_HOST}:{SITE_PORT}")

CELERY_RESULT_BACKEND = "django-db"

# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

Expand Down
1 change: 0 additions & 1 deletion backend/backend/settings/worker/events/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
INSTALLED_APPS = [
"django.contrib.auth",
"django.contrib.contenttypes",
"django_celery_results",
"rest_framework",
"substrapp",
"api",
Expand Down
1 change: 0 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ celery==5.3.6
checksumdir==1.2.0
cryptography==42.0.5
django-cors-headers==4.4.0
django-celery-results==2.5.1
djangorestframework==3.15.2
djangorestframework-simplejwt==5.2.2
drf-spectacular==0.25.1
Expand Down
1 change: 1 addition & 0 deletions changes/995.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
replaced `django-celery-results` by a connexion to the existing redis instance.
Loading