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 5cde5a5..704cd15 100644 --- a/config/settings.py +++ b/config/settings.py @@ -115,6 +115,15 @@ CORS_ALLOW_ALL_ORIGINS = True +# 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 # Cloudflare R2 # Default to None so the app starts without R2 in local dev. # AiVisionClient / boto3 will raise at the point of first use if unset. diff --git a/docker-compose.yaml b/docker-compose.yaml index dd30082..639b418 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 f75bb59..e69de29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,12 +0,0 @@ -Django==5.2 -djangorestframework==3.16.0 -djangorestframework-simplejwt==5.5.0 -django-filter==25.1 -django-cors-headers -psycopg2-binary==2.9.10 -python-decouple==3.8 -Markdown==3.8 -pillow==11.2.1 -boto3 -openai -google-generativeai \ No newline at end of file