Skip to content

Commit

Permalink
feat: add celery
Browse files Browse the repository at this point in the history
  • Loading branch information
helllllllder committed Jul 12, 2023
1 parent b396749 commit 66fde2a
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 9 deletions.
5 changes: 5 additions & 0 deletions chats/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ("celery_app",)
22 changes: 22 additions & 0 deletions chats/celery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from celery import Celery

# Set the default Django settings module for the "celery" program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chats.settings")

app = Celery("chats")

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace="CELERY" means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")

# Load task modules from all registered Django apps.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print("Request: {0!r}".format(self.request))
27 changes: 22 additions & 5 deletions chats/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"rest_framework",
"rest_framework.authtoken",
"corsheaders",
"django_celery_beat",
"django_celery_results",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -105,24 +107,30 @@

WSGI_APPLICATION = "chats.wsgi.application"

# channels
ASGI_APPLICATION = "chats.asgi.application"

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = dict(default=env.db(var="DATABASE_URL"))

REDIS_URL = env.str("CHANNEL_LAYERS_REDIS", default="redis://localhost:6379/1")

# channels

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.pubsub.RedisPubSubChannelLayer",
"CONFIG": {
"hosts": [
env.str("CHANNEL_LAYERS_REDIS", default="redis://127.0.0.1:6379/1")
],
"hosts": [REDIS_URL],
},
},
}

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": env.str("CHANNEL_LAYERS_REDIS", default="redis://127.0.0.1:6379/1"),
"LOCATION": REDIS_URL,
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
}
}
Expand Down Expand Up @@ -353,3 +361,12 @@

CHATS_FLOWS_TAG = env.str("CHATS_FLOWS_TAG", default="chats")
CHATS_CACHE_TIME = env.int("CHATS_CACHE_TIME", default=1 * 60 * 60)

# Celery

CELERY_BROKER_URL = env.str("CELERY_BROKER_URL", default=REDIS_URL)
CELERY_RESULT_BACKEND = env.str("CELERY_RESULT_BACKEND", default="django-db")
CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_TASK_SERIALIZER = "json"
CELERY_RESULT_SERIALIZER = "json"
CELERY_TIMEZONE = TIME_ZONE
Loading

0 comments on commit 66fde2a

Please sign in to comment.