Search code examples
djangopostgresqldocker-composecelerydjango-celery

Celery tasks are not adding object to db Django/Postgres


I have trouble with celery using it in docker, it receives tasks and in terminal sends that everything works fine, but it creates nothing in the database(I use Postgres). I think that problem somewhere in docker, but not sure. Consol doesn't give any errors. Can't find anything in the internet about it, please help me with this problem

enter image description here

my docker-compose file:

version: "3"
services:
  app:
    build:
      context: .
    ports:
       - "8000:8000"
    volumes:
       - ./app:/app
    command: >
       sh -c "python manage.py wait_for_db &&
              python manage.py makemigrations &&
              python manage.py migrate &&
              python manage.py test&&
              python manage.py runserver 0.0.0.0:8000"

    environment:
       - DB_HOST=db
       - DB_NAME=app
       - DB_USER=postgres
       - DB_PASS=supersecretpassword
       - CELERY_BROKER=redis://redis:6379/0
       - CELERY_BACKEND=redis://redis:6379/0
    depends_on:
       - db
  db:
    image: postgres:13-alpine
    environment:
       - POSTGRES_DB=app
       - POSTGRES_USER=postgres
       - POSTGRES_PASSWORD=supersecretpassword

redis:
   ports:
     - "6379:6379"
   image: redis:5-alpine

celery-beat:
  build: .
  user: root
  command: celery -A app beat -l INFO
  environment:
    - DB_HOST=db
    - CELERY_BROKER=redis://redis:6379/0
    - CELERY_BACKEND=redis://redis:6379/0
  depends_on:
    - db
    - redis
    - celery

celery:
  build: .
  user: root
  command: celery -A app worker -l INFO --pool=solo
  environment:
    - DB_HOST=db
    - CELERY_BROKER=redis://redis:6379/0
    - CELERY_BACKEND=redis://redis:6379/0
  depends_on:
    - db
    - redis

my celery.py

import os
from celery import Celery
from celery.schedules import crontab
from app.settings import INSTALLED_APPS

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

app = Celery('app')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()


app.conf.beat_schedule = {
   'add-every-5-seconds': {
      'task': 'transactions.tasks.create_transaction',
      'schedule': 5.0,
    },
}

and my task

@shared_task
def create_transaction():
   transaction = Transaction.objects.create()
   return transaction.id

Solution

  • Finelly I found out the reason. The problem was in wrong Celery configuration in settings.py. Thanks to Niel Godfrey Ponciano for pointing me to that thing!