celery / django-celery-results

Celery result back end with django
Other
668 stars 206 forks source link

Task Result not getting added to the db and matching query does not exist #349

Closed MaisumAbbas closed 1 year ago

MaisumAbbas commented 1 year ago

Hi, I am trying to run my django application using docker which involves celery. I am able to set everything on local and it works perfectly fine. However, when I run it docker, and my task gets executed, it throws me the following error:

myapp.models.mymodel.DoesNotExist: mymodel matching query does not exist.

I am particularly new to celery and docker so not sure what am I doing wrong.

Celery is set up correctly, I have made sure of that. Following are the broker_url and backend:

CELERY_BROKER_URL = 'redis://redis:6379/0' 
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = 'django-db'

This is my docker-compose.yml file:

version: "3.8"

services:
  redis:
    image: redis:alpine
    container_name: rz01
    ports:
      - "6379:6379"
    networks:
      - npm-nw
      - myapp-network

  myapphere:
    build: .
    command: >
      sh -c "python manage.py makemigrations &&
             python manage.py migrate &&
             gunicorn myapphere.wsgi:application -b 0.0.0.0:8000 --workers=1 --timeout 10000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    restart: unless-stopped
    env_file: .env
    networks:
      - npm-nw
      - myapp-network

  celery:
    build: .
    restart: always
    container_name: cl01
    command: celery -A myapphere worker -l info
    depends_on:
      - redis
    networks:
      - npm-nw
      - myapp-network

networks:
  myapp-network:
  npm-nw:
    external: false

I have tried few things from different stackoverflow posts like apply_async. I have also made sure that my model existed.

.On further investigating the issue, I have noticed that the celery task does not get created in the database in the first place. Don't know why, may be I have to change the following with something else:

CELERY_RESULT_BACKEND = 'django-db'

I am also posting my celery.py code just in case:

from __future__ import absolute_import, unicode_literals

import os

from celery import Celery

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

app = Celery('myproj')

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

app.autodiscover_tasks()

This is in init.py file of main folder find django according to the documentation:

from __future__ import absolute_import, unicode_literals

from .celery import app as celery_app

__all__ = ('celery_app',)

And yes, I have listed down the required apps in INSTALLED_APPS.

In summary, I can say that I am facing two issues here:

  1. Task Result does not get populated at all.
  2. My other model which I am trying to access returns empty queryset even though the record existed but still I am getting the error and the cause of it is the first issue.

I am not sure what's the issue is, I have also tried following the other issues listed here and try their solutions but didn't work. Please let me know what's the issue is and how can it be resolved.

And one more thing, this works fine on localhost, just this needs to be changed from CELERY_BROKER_URL = 'redis://redis:6379/0' to CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'

MaisumAbbas commented 1 year ago

I needed to add

volumes:
      - .:/code

in the celery container so it can be able to access the db. This resolved my issue.