From 68f0dd13db57b9fe22730148b8c7e9da2056f55a Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Tue, 22 Jul 2025 18:03:24 +0330 Subject: [PATCH] add celery --- celery.sh | 8 ++++++++ main/__init__.py | 3 +++ main/celery.py | 23 +++++++++++++++++++++++ main/settings/settings.py | 24 +++++++++++++++++++++++- utils/tasks.py | 5 +++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100755 celery.sh create mode 100644 main/celery.py create mode 100644 utils/tasks.py diff --git a/celery.sh b/celery.sh new file mode 100755 index 0000000..6d5b414 --- /dev/null +++ b/celery.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +while ! nc -z $DB_HOST 5432 ; do + echo "APP Waiting for the DB Server" + sleep 3 +done +#python3 manage.py collectstatic --noinput +#python3 manage.py migrate +celery -A main worker --loglevel=info \ No newline at end of file diff --git a/main/__init__.py b/main/__init__.py index e69de29..9e0d95f 100644 --- a/main/__init__.py +++ b/main/__init__.py @@ -0,0 +1,3 @@ +from .celery import app as celery_app + +__all__ = ('celery_app',) \ No newline at end of file diff --git a/main/celery.py b/main/celery.py new file mode 100644 index 0000000..7929e8f --- /dev/null +++ b/main/celery.py @@ -0,0 +1,23 @@ +""" +https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html +""" +import os + +from celery import Celery + +from django.conf import settings + +# this code copied from manage.py +# set the default Django settings module for the 'celery' app. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings') + +# you can change the name here +app = Celery("main") + +# read config from Django settings, the CELERY namespace would make celery +# config keys has `CELERY` prefix +app.config_from_object('django.conf:settings', namespace='CELERY') + +# discover and load tasks.py from all registered Django apps +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + diff --git a/main/settings/settings.py b/main/settings/settings.py index 53423ee..dbba9c9 100644 --- a/main/settings/settings.py +++ b/main/settings/settings.py @@ -78,6 +78,13 @@ REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 200, "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", + # 'DEFAULT_THROTTLE_CLASSES': [ + # 'rest_framework.throttling.AnonRateThrottle', + # ], + 'DEFAULT_THROTTLE_RATES': { + # 'otp_min': '1/min', + # 'otp_day': '20/day', + } } @@ -152,7 +159,7 @@ JALALI_DATE_DEFAULTS = { CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": config('BASE_REDIS_URL'), + "LOCATION": config('REDIS_BASE_URL'), "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } @@ -264,3 +271,18 @@ MINIO_HTTP_CLIENT: urllib3.poolmanager.PoolManager = urllib3.PoolManager( status_forcelist=[500, 502, 503, 504] ) ) + + +REDIS_BASE_URL = config('REDIS_BASE_URL') + + + +CELERY={ + "broker_url" :REDIS_BASE_URL, + "result_backend" : REDIS_BASE_URL, +} + +CELERY_TASK_TRACK_STARTED = True +CELERY_TASK_TIME_LIMIT = 30 * 60 +CELERY_BROKER_URL = REDIS_BASE_URL +CELERY_RESULT_BACKEND = REDIS_BASE_URL \ No newline at end of file diff --git a/utils/tasks.py b/utils/tasks.py new file mode 100644 index 0000000..d5a3c39 --- /dev/null +++ b/utils/tasks.py @@ -0,0 +1,5 @@ +from main import celery_app + +@celery_app.task +def sample_task(param): + print(param)