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(redis): Add Redis and django-rq #53

Merged
merged 21 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2e288b4
build(deps): Add redis and django-rq as dependencies
ERosendo Feb 2, 2023
24a8ce9
feat(settings): Add redis settings
ERosendo Feb 2, 2023
badb0f4
feat(settings): Add rq settings
ERosendo Feb 2, 2023
0619ffd
chore(docker compose): Add redis and rq worker as services
ERosendo Feb 2, 2023
02812b5
feat(ci): Update make file to add rq worker
ERosendo Feb 2, 2023
2b1a060
Merge branch 'main' into 35-add-redis-and-task-queue
ERosendo Feb 2, 2023
1ec718d
Merge branch 'fix-docker-compose-file' into 35-add-redis-and-task-queue
ERosendo Feb 2, 2023
518bf59
feat(core): Add endpoint to trigger a failing rq task
ERosendo Feb 2, 2023
77fe98f
feat(settings): Adds the RQ integration to the sentry.init method
ERosendo Feb 2, 2023
8fc044a
style: Change code styling using black
ERosendo Feb 2, 2023
07e1d01
chore(dockerfile): Update rqworker command to start workers with sche…
ERosendo Feb 2, 2023
defdea0
style(core): Update the code styling of the tasks.py file
ERosendo Feb 2, 2023
5c2ca2e
chore(docker-compose): Update the image used to create the redis service
ERosendo Feb 2, 2023
95eaf5f
feat(rq): Add link in the Admin dashboard to monitor the status of th…
ERosendo Feb 2, 2023
5d26565
fix(redis): Tweak default value for REDIS_HOST
ERosendo Feb 2, 2023
7f92526
feat(urls): Add endpoint to access the RQ dashboard
ERosendo Feb 2, 2023
361b929
fix(settings): Update the list of options for RedisCache
ERosendo Feb 2, 2023
68bafe6
Merge branch 'main' into 35-add-redis-and-task-queue
ERosendo Feb 2, 2023
dddf30d
docs(.env.example): Change order in the .env.example file
ERosendo Feb 2, 2023
206293c
Merge branch '35-add-redis-and-task-queue' of https://github.com/free…
ERosendo Feb 2, 2023
ac86541
fix(makefile): Remove celery from the docker build command
ERosendo Feb 2, 2023
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ DB_NAME=""
DB_USER=""
DB_PASSWORD=""
DB_HOST=""

REDIS_HOST=""
REDIS_PORT=""
ERosendo marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions bc/core/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django_rq import job


@job
def fail_task() -> float:
division_by_zero = 1 / 0
return division_by_zero
3 changes: 2 additions & 1 deletion bc/core/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.urls import path

from .views import health_check, sentry_fail
from .views import health_check, rq_fail, sentry_fail

urlpatterns = [
path("monitoring/health-check/", health_check, name="health_check"),
path("sentry/error/", sentry_fail, name="sentry_fail"),
path("sentry/rq-fail/", rq_fail, name="rq_fail"),
]
6 changes: 6 additions & 0 deletions bc/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.http import HttpRequest, HttpResponse, JsonResponse

from .tasks import fail_task
from .utils import check_postgresql


Expand All @@ -25,3 +26,8 @@ def health_check(request: HttpRequest) -> JsonResponse:
},
status=status,
)


def rq_fail(request: HttpRequest) -> HttpResponse:
fail_task.delay()
return HttpResponse("Successfully failed RQ.")
2 changes: 2 additions & 0 deletions bc/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
from .third_party.courtlistener import *
from .third_party.mastodon import *
from .third_party.pacer import *
from .third_party.redis import *
from .third_party.rq import *
from .third_party.sentry import *
16 changes: 16 additions & 0 deletions bc/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import environ

from .third_party.redis import REDIS_DATABASES, REDIS_HOST, REDIS_PORT

env = environ.FileAwareEnv()

DEVELOPMENT = env.bool("DEVELOPMENT", default=True)
Expand Down Expand Up @@ -30,6 +32,8 @@
"bc.channel",
"bc.subscription",
"bc.web",
# other apps
"django_rq",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -82,6 +86,18 @@
},
}

####################
# Cache & Sessions #
####################
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": f"{REDIS_HOST}:{REDIS_PORT}",
"OPTIONS": {"db": REDIS_DATABASES["CACHE"]},
},
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woah. I didn't know this was possible. Awesome.


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
Expand Down
12 changes: 12 additions & 0 deletions bc/settings/third_party/redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import environ

env = environ.FileAwareEnv()


REDIS_HOST = env("REDIS_HOST", default="redis://bc2-redis")
REDIS_PORT = env("REDIS_PORT", default=6379)

REDIS_DATABASES = {
"QUEUE": 0,
"CACHE": 1,
}
10 changes: 10 additions & 0 deletions bc/settings/third_party/rq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .redis import REDIS_DATABASES, REDIS_HOST, REDIS_PORT

RQ_SHOW_ADMIN_LINK = True

RQ_QUEUES = {
"default": {
"URL": f"{REDIS_HOST}:{REDIS_PORT}",
"DB": REDIS_DATABASES["QUEUE"],
},
}
3 changes: 2 additions & 1 deletion bc/settings/third_party/sentry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import environ
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.integrations.rq import RqIntegration

env = environ.FileAwareEnv()
SENTRY_DSN = env("SENTRY_DSN", default="")
Expand All @@ -13,6 +13,7 @@
dsn=SENTRY_DSN,
integrations=[
DjangoIntegration(),
RqIntegration(),
],
ignore_errors=[KeyboardInterrupt],
traces_sample_rate=SENTRY_SAMPLE_TRACE,
Expand Down
1 change: 1 addition & 0 deletions bc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
path("", include("bc.core.urls")),
path("", include("bc.channel.urls")),
path("", include("bc.subscription.urls")),
path("django-rq/", include("django_rq.urls")),
]
27 changes: 27 additions & 0 deletions docker/bigcasesbot/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ services:
networks:
- bc2_net_overlay

bc2-redis:
container_name: bc2-redis
image: redis:7-alpine
ports:
- "6379:6379"
networks:
- bc2_net_overlay


bc2-rq:
container_name: bc2-rq
image: ${DJANGO_DOCKER_IMAGE:-freelawproject/bigcases2:latest-rq-dev}
build:
dockerfile: docker/django/Dockerfile
context: ../../
target: rq
args:
- BUILD_ENV=dev
depends_on:
- bc2-postgresql
- bc2-doctor
- bc2-redis
networks:
- bc2_net_overlay


bc2-django:
container_name: bc2-django
image: ${DJANGO_DOCKER_IMAGE:-freelawproject/bigcases2:latest-web-dev}
Expand All @@ -38,6 +64,7 @@ services:
depends_on:
- bc2-postgresql
- bc2-doctor
- bc2-redis
volumes:
- ../..:/opt/bigcases2
ports:
Expand Down
5 changes: 5 additions & 0 deletions docker/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ RUN mkdir /var/log/bigcases2 \

WORKDIR /opt/bigcases2

# freelawproject/bigcases2:latest-rq
FROM python-base as rq

CMD python /opt/bigcases2/manage.py rqworker default --with-scheduler

#freelawproject/bigcases2:latest-web-dev
FROM python-base as web-dev

Expand Down
5 changes: 5 additions & 0 deletions docker/django/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ REPO ?= freelawproject/bigcases2
DOCKER_TAG_PROD = $(VERSION)-web-prod
WEB_PROD ?= latest-web-prod
WEB_DEV ?= latest-web-dev
RQ_TAG = $(VERSION)-rq
RQ_LATEST ?= latest-rq
RQ_DEV ?= latest-rq-dev


.PHONY: all image push multiarch_image multiarch_push x86_image x86_push
Expand All @@ -23,9 +26,11 @@ all: image

development:
docker build --target web-dev -t $(REPO):$(WEB_DEV) --build-arg BUILD_ENV=dev --file docker/django/Dockerfile .
docker build --target rq -t $(REPO):$(RQ_DEV) --build-arg BUILD_ENV=dev --file docker/django/Dockerfile .

image:
docker build --target web-prod -t $(REPO):$(DOCKER_TAG_PROD) -t $(REPO):$(WEB_PROD) --file docker/django/Dockerfile .
docker build --target celery -t $(REPO):$(RQ_TAG) -t $(REPO):$(RQ_LATEST) --file docker/django/Dockerfile .
ERosendo marked this conversation as resolved.
Show resolved Hide resolved

push: image
$(info Checking if valid architecture)
Expand Down
72 changes: 70 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ djangorestframework = "^3.14.0"
django-environ = "^0.9.0"
sentry-sdk = "^1.14.0"
django-csp = "^3.7"
redis = "^4.4.2"
django-rq = "^2.6.0"
gunicorn = "^20.1.0"

[tool.poetry.dev-dependencies]
Expand Down