0

When I try to execute the command celery -A novopagamento worker -l info I receive the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/celery/app/utils.py", line 365, in find_app
    found = sym.app
AttributeError: module 'novopagamento' has no attribute 'app'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/celery/__main__.py", line 16, in main
    _main()
  File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 322, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 273, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/usr/local/lib/python3.6/site-packages/celery/bin/base.py", line 501, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/usr/local/lib/python3.6/site-packages/celery/app/utils.py", line 370, in find_app
    found = sym.celery
AttributeError: module 'novopagemento' has no attribute 'celery'

My project's structure:

novopagamento
├──novopagamento
|  ├──__init__.py
|  ├──settings.py
|  └──celery.py
├──api
|  └──tasks.py

My celery file:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

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

app = Celery('novopagemento')

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

app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

My task file:

from __future__ import absolute_import, unicode_literals

from celery import shared_task


@shared_task
def task_number_one():
    #code

My settings file:

import os

# Other Celery settings
from celery.schedules import crontab

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# .
# .
# .

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',  # Django REST framework
    'rest_framework.authtoken',
    'rest_framework_swagger',  # Swagger
    'celery', # Celery
    'api',
]

# .
# .
# .

# CELERY
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Makassar'
CELERY_BEAT_SCHEDULE = {
    'task-number-one': {
        'task': 'api.tasks.task_number_one',
        'schedule': crontab(minute=0, hour='*/3,10-19')
    },
}

if it's interesting, there is my docker-compose.yml:

db:
  image: postgres
worker:
  image: redis:4
web:
  build: .
  command: python novopagamento/manage.py migrate
  command: python novopagamento/manage.py runserver 0.0.0.0:8000
  stdin_open: true
  tty: true
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - db
    - worker

I followed the steps described in this article:

Celery 4 Periodic Task in Django

Thanks for your patience and collaboration. ;)

mdcg
  • 165
  • 3
  • 12
  • If you change `app` to `celery` in your celery file, does it work? Maybe you copied / pasted something looking for a `celery` variable instead of `app` variable? If this helps, maybe [similar question here](https://stackoverflow.com/questions/13555386/starting-celery-attributeerror-module-object-has-no-attribute-celery). – Jarad Aug 16 '18 at 17:05
  • I've already changed the variable names, I've instantiated Celery in tasks.py itself, nothing works. – mdcg Aug 16 '18 at 17:48
  • I've never seen `'celery'` set in INSTALLED_APPS before but maybe that's possible. In my working app, I have `'django_celery_results'` set in INSTALLED_APPS after following celery's docs. Perhaps you're use-case is different. – Jarad Aug 16 '18 at 18:29
  • Same problem here. Did you find a solution? – Jesus Almaral - Hackaprende Sep 26 '19 at 01:50

3 Answers3

3

Long shot but in novopagamento/novopagamento/__init__.py, did you set this?

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ('celery_app',)
Jarad
  • 17,409
  • 19
  • 95
  • 154
0

You need to create a celery object in your task.py. Change the broker to what ever broker your using

celery = Celery('tasks', broker='amqp://guest@localhost//')
Taylor
  • 1,223
  • 1
  • 15
  • 30
  • Maybe this will work for another case, but still, thank you. :) – mdcg Oct 19 '18 at 18:18
  • @mdcg did you ever find the solution for this problem? my docker containers for worker and beat are giving the same error. Doing a bit of research it might be to do with some entry point for celery. Hope you solved this problem and you share your finding with me! – tomoc4 Nov 01 '19 at 03:13
  • @tomoc4 I could find the solution to the problem yes, including I created a post in Medium explaining how to solve. Unfortunately it is in Portuguese, which is my native language, but I ask you to pay attention to "docker-compose.yml", there is a very important detail for the execution of celery. You can access the post by clicking here: [link](https://medium.com/@mdcg.dev/configurando-um-sistem-em-django-para-executar-tarefas-ass%C3%ADncronas-utilizando-celery-redis-e-53a30d0d2ec2) – mdcg Nov 01 '19 at 14:19
0

Configuration might creating this problem.. Replace:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'

To :

BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis'

make sure redis server is running.

Roshan Bagdiya
  • 2,048
  • 21
  • 41
  • It was a problem with the Docker Image. If I were using it on a local machine, that would be a very important detail. Thank you very much. – mdcg Oct 19 '18 at 18:20