Skip to content

Commit f522789

Browse files
committed
Celery for single tenant app
1 parent 501b55e commit f522789

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

config/settings/base.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
import environ
7+
from celery.schedules import crontab
78

89
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent
910
# pi23_gswdt/
@@ -81,6 +82,7 @@
8182
"rest_framework.authtoken",
8283
"corsheaders",
8384
"drf_spectacular",
85+
"django_celery_results",
8486
]
8587

8688
LOCAL_APPS = [
@@ -268,7 +270,7 @@
268270
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-broker_url
269271
CELERY_BROKER_URL = env("CELERY_BROKER_URL")
270272
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#std:setting-result_backend
271-
CELERY_RESULT_BACKEND = CELERY_BROKER_URL
273+
CELERY_RESULT_BACKEND = "django-db"
272274
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#result-extended
273275
CELERY_RESULT_EXTENDED = True
274276
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#result-backend-always-retry
@@ -337,3 +339,26 @@
337339
}
338340
# Your stuff...
339341
# ------------------------------------------------------------------------------
342+
REDIS_URL = env("CELERY_BROKER_URL", default="redis://redis:6379/0")
343+
344+
CACHES = {
345+
"default": {
346+
"BACKEND": "django_redis.cache.RedisCache",
347+
"LOCATION": REDIS_URL,
348+
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient", "PICKLE_VERSION": 5},
349+
"IGNORE_EXCEPTIONS": True,
350+
"TIMEOUT": 3600,
351+
},
352+
}
353+
354+
CELERY_CACHE_BACKEND = "default"
355+
CELERY_BEAT_SCHEDULE = {
356+
"task__test_celery": {
357+
"task": "task__test_celery",
358+
"args": (1, 2, 3),
359+
"kwargs": {"a": 1, "b": 2, "c": 3},
360+
"schedule": crontab(
361+
**{"minute": "*/3", "hour": "*", "day_of_week": "*", "day_of_month": "*", "month_of_year": "*"}
362+
),
363+
},
364+
}

config/settings/local.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
1414
ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"]
1515

16-
# CACHES
17-
# ------------------------------------------------------------------------------
18-
# https://docs.djangoproject.com/en/dev/ref/settings/#caches
19-
CACHES = {
20-
"default": {
21-
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
22-
"LOCATION": "",
23-
}
24-
}
16+
# # CACHES
17+
# # ------------------------------------------------------------------------------
18+
# # https://docs.djangoproject.com/en/dev/ref/settings/#caches
19+
# CACHES = {
20+
# "default": {
21+
# "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
22+
# "LOCATION": "",
23+
# }
24+
# }
2525

2626
# EMAIL
2727
# ------------------------------------------------------------------------------

pi23_gswdt/bank/tasks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from celery.utils.log import get_task_logger
2+
from django.conf import settings
3+
from django_redis_task_lock import lock
4+
5+
from config.celery_app import app
6+
7+
LOGGER = get_task_logger(__name__)
8+
9+
10+
@app.task(name="task__test_celery")
11+
@lock(debug=settings.DEBUG)
12+
def task__test_celery(*args, **kwargs):
13+
LOGGER.info("Executing the task with args: [%s] and kwargs: [%s]", args, kwargs)
14+
return f"Executing the task with args: [{args}] and kwargs: [{kwargs}]"

requirements/base.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ djangorestframework==3.14.0 # https://github.com/encode/django-rest-framework
2323
django-cors-headers==4.2.0 # https://github.com/adamchainz/django-cors-headers
2424
# DRF-spectacular for api documentation
2525
drf-spectacular==0.26.5 # https://github.com/tfranzel/drf-spectacular
26+
django-celery-results==2.5.1 # https://github.com/celery/django-celery-results
27+
django-redis-task-lock==0.6 # https://github.com/Lenders-Cooperative/django-redis-task-lock
2628

2729
django-constance==2.9.1 # https://github.com/jazzband/django-constance
2830
django-constance[database]

0 commit comments

Comments
 (0)