From 570767bdc66c81e36e292b8fc51381bec840870d Mon Sep 17 00:00:00 2001 From: Luisa Date: Thu, 23 Apr 2026 10:12:49 -0300 Subject: [PATCH] feat: configure Celery + Redis async task infrastructure (Issue #10) --- apps/ai_analysis/tasks.py | 11 +++++++++++ config/__init__.py | 3 +++ config/celery.py | 8 ++++++++ config/settings.py | 12 +++++++++++- docker-compose.yaml | 23 +++++++++++++++++++++++ requirements.txt | Bin 454 -> 512 bytes 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 apps/ai_analysis/tasks.py create mode 100644 config/celery.py diff --git a/apps/ai_analysis/tasks.py b/apps/ai_analysis/tasks.py new file mode 100644 index 0000000..5341a52 --- /dev/null +++ b/apps/ai_analysis/tasks.py @@ -0,0 +1,11 @@ +import logging +from celery import shared_task + +logger = logging.getLogger(__name__) + +@shared_task +def debug_task(): + logger.info("Celery worker está funcionando!") + return "ok" + +# para usar essa taks, chamamos a task junto com .delay() \ No newline at end of file diff --git a/config/__init__.py b/config/__init__.py index e69de29..e31568a 100644 --- a/config/__init__.py +++ b/config/__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/config/celery.py b/config/celery.py new file mode 100644 index 0000000..1cec215 --- /dev/null +++ b/config/celery.py @@ -0,0 +1,8 @@ +import os +from celery import Celery + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") + +app = Celery("homematch") +app.config_from_object("django.conf:settings", namespace="CELERY") +app.autodiscover_tasks() diff --git a/config/settings.py b/config/settings.py index dcf6702..355ae4f 100644 --- a/config/settings.py +++ b/config/settings.py @@ -118,4 +118,14 @@ R2_ACCESS_KEY_ID = config("R2_ACCESS_KEY_ID") R2_ACCOUNT_ID = config("R2_ACCOUNT_ID") R2_SECRET_ACCESS_KEY = config("R2_SECRET_ACCESS_KEY") -R2_BUCKET_NAME = config("R2_BUCKET_NAME") \ No newline at end of file +R2_BUCKET_NAME = config("R2_BUCKET_NAME") + +# Celery +CELERY_BROKER_URL = config("CELERY_BROKER_URL", default="redis://redis:6379/0") +CELERY_RESULT_BACKEND = config("CELERY_RESULT_BACKEND", default="redis://redis:6379/0") +CELERY_ACCEPT_CONTENT = ["json"] +CELERY_TASK_SERIALIZER = "json" + +# O redis vai enfileirar as tarefas e atribuir aos workers do celery. +# O celery vai ter seus workers que vão ter suas funções já pré definidas nos arquivos tasks.py +# Quando uma tarefa é terminada, o redis vai armazenar seu resultado \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 2b86d90..9fc61dd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,6 +15,16 @@ services: interval: 5s retries: 5 + redis: + image: redis:7-alpine + ports: + - "6379:6379" + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + retries: 5 + + web: build: . command: python manage.py runserver 0.0.0.0:8000 @@ -28,5 +38,18 @@ services: db: condition: service_healthy + celery_worker: + build: . + command: celery -A config worker -l info + volumes: + - .:/app + env_file: + - .env + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + volumes: postgres_data: \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 00b7ba56551afbb21448cb0c8ef45a2bc3af59eb..a261beb3ce8195d82e98600b1fc5431cb3edf979 100644 GIT binary patch delta 66 zcmX@c+`zKo7^4+00~bRwLn=cK5En62GS~v4DT5w^F%X+EfMkn+GARt148?Fc1F$>* DsW=L% delta 7 OcmZo*ImW!<7$X1+CIZC(