I’m facing a problem where I can’t set up periodic tasks in my application in django framework using Celery.
I’ve literally tried everything i could find adressing this issue on the internet, transfered the code from my MacOS laptop to the PC on Windows, but nothing seems to help… I used the documentation from celery to replicate every step for the periodic tasks to work, tried using other versions of Celery, Django-Celery-beat, django-celery-results, but nothing helped, so I don’t know which part of code I’m wrong at.
Would very much appreciate any help!
My apps packages versions:
Package Version
celery 5.3.6
Django 4.0.10
django-celery-beat 2.5.0
django-celery-results 2.5.1
django-debug-toolbar 4.2.0
honcho 1.1.0
mysqlclient 2.2.1
oauthlib 3.2.2
Pillow 10.1.0
pip 23.3.2
redis 5.0.1
Python 3.8.8
My django project structure:
django-project
├── .vscode
│ └── settings.json
├── myapp
│ ├── admin.py
│ ├── apps.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-38.pyc
│ │ ├── 0002_skin_cartitem.cpython-38.pyc
│ │ ├── 0003_remove_skin_game_skin_game_id_alter_skin_price.cpython-38.pyc
│ │ ├── 0004_remove_skin_game_id_skin_game_name.cpython-38.pyc
│ │ └── __init__.cpython-38.pyc
│ ├── models.py
│ ├── signals.py
│ ├── tasks.py
│ ├── templates
│ │ ├── base.html
│ │ ├── home.html
│ │ ├── login.html
│ │ └── skin_list.html
│ ├── tests.py
│ ├── urls.py
│ ├── views.py
│ ├── __init__.py
│ └── __pycache__
├── manage.py
├── django-project
│ ├── asgi.py
│ ├── celeryapp.py
│ ├── config.py
│ ├── pyenv.cfg
│ ├── settings.py
│ ├── urls.py
I didn’t include some files to make project structure visualy readable.
Settings.py:
"""
Django settings for Marketplace_backend project.
Generated by 'django-admin startproject' using Django 4.2.7.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from pathlib import Path
import os
from django-project.config import *
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Debug=True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
'debug_toolbar',
'social_django',
'django_celery_beat',
"django_celery_results",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"debug_toolbar.middleware.DebugToolbarMiddleware",
'social_django.middleware.SocialAuthExceptionMiddleware',
]
ROOT_URLCONF = 'Marketplace_backend.urls'
# Celery Configuration
# CELERY_BROKER_URL = 'redis://localhost:6379'
# CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Moscow'
from celery.schedules import crontab
# CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_BEAT_SCHEDULE = {
'test': {
'task': 'myapp.tasks.test',
'schedule': crontab(minute="*/1"),
},
}
# CELERY_INCLUDE = ('myapp.tasks',)
# CELERY_IMPORTS = ('myapp.tasks',)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
'app_marketplace.context_processors.user_social_data',
],
},
},
]
INTERNAL_IPS = [
# ...
"127.0.0.1",
# ...
]
WSGI_APPLICATION = 'django-project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'app_database',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
celeryapp.py:
from celery import Celery
from celery.schedules import crontab
import os
from django.conf import settings
app = Celery('django-project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django-project.settings')
app.config_from_object('django.conf:settings', namespace="CELERY")
app.autodiscover_tasks()
init.py in django-project inner folder:
from __future__ import absolute_import, unicode_literals
from .celeryapp import app as celery_app
__all__ = ('celery_app',)
# Tried this line - didn't help.
from myapp.tasks import test
tasks.py in myapp:
# Someone suggested these two lines (2,3) - didn't help
import django
django.setup()
from celery import shared_task
from .models import *
from .views import *
@shared_task(name="test")
def test():
return 'test'
I start everything via honcho Procfile:
web: python manage.py runserver
celery: celery -A django-project worker --pool=solo -l info
beat: celery -A django-project beat -l info
I get this error, when celery beat tries to execute periodic task:
20:04:00 beat.1 | [2024-01-01 20:04:00,000: INFO/MainProcess] Scheduler: Sending due task
20:04:00 beat.1 | test (myapp.tasks.test)
20:04:00 celery.1 | [2024-01-01 20:04:00,003: ERROR/MainProcess] Received unregistered task of type 'myapp.tasks.test'.
20:04:00 celery.1 | The message has been ignored and discarded.
20:04:00 celery.1 |
20:04:00 celery.1 | Did you remember to import the module containing this task?
20:04:00 celery.1 | Or maybe you're using relative imports?
20:04:00 celery.1 |
20:04:00 celery.1 | Please see
20:04:00 celery.1 | https://docs.celeryq.dev/en/latest/internals/protocol.html
20:04:00 celery.1 | for more information.
20:04:00 celery.1 |
20:04:00 celery.1 | The full contents of the message body was:
20:04:00 celery.1 | b'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)
20:04:00 celery.1 |
20:04:00 celery.1 | The full contents of the message headers:
20:04:00 celery.1 | {'lang': 'py', 'task': 'myapp.tasks.test', 'id': '332beab6-a3de-4e4a-84f3-59d6041a1b47', 'shadow': None, 'eta': None, 'expires': None, 'group': None, 'group_index': None, 'retries': 0, 'timelimit': [None, None], 'root_id': '332beab6-a3de-4e4a-84f3-59d6041a1b47', 'parent_id': None, 'argsrepr': '()', 'kwargsrepr': '{}', 'origin': 'gen24368@DESKTOP-KNKGMAO', 'ignore_result': False, 'replaced_task_nesting': 0, 'stamped_headers': None, 'stamps': {}}
20:04:00 celery.1 |
20:04:00 celery.1 | The delivery info for this task is:
20:04:00 celery.1 | {'exchange': '', 'routing_key': 'celery'}
20:04:00 celery.1 | Traceback (most recent call last):
20:04:00 celery.1 | File "c:\users\yantb\desktop\django-project\django-project\lib\site-packages\celery\worker\consumer\consumer.py", line 658, in on_task_received
20:04:00 celery.1 | strategy = strategies[type_]
20:04:00 celery.1 | KeyError: 'app_marketplace.tasks.test'