Jonas Vandermosten
Jonas Vandermosten

Reputation: 163

Setting up Django with Celery

I am trying to set up celery with Django on my development server, running on windows. Following are changes i made. I followed steps by this link http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

Filestructure

Noisebox
-Noisebox
--__init__.py
--celery.py
--settings
-rehearsalbooking
--tasks.py

init.py

from __future__ import absolute_import, unicode_literals

from .celery import app as celery_app

__all__ = ['celery_app']

celery.py

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

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

app = Celery('noisebox')
app.autodiscover_tasks()


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

tasks.py

from celery import shared_task

from .models import Room
from django.utils import timezone


@shared_task
def calculate_timeslots(room_id):
    room = Room.objects.get(id=room_id)
    room.create_timeslots_until_license_expires(timezone.now())
    return True

When i run the task, the task gets sent to the celery server, but is not executed.

    (noisebox-env) C:\Users\jonas>celery worker

 -------------- celery@DESKTOP-MH2SD2S v4.1.0 (latentcall)
---- **** -----
--- * ***  * -- Windows-10-10.0.16299-SP0 2018-02-22 14:22:38
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         default:0x29efc50 (.default.Loader)
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[2018-02-22 14:22:39,439: ERROR/MainProcess] Received unregistered task of type 'rehearsalbooking.tasks.calculate_timeslots'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
'[[1], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (78b)
Traceback (most recent call last):
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\worker\consumer\consumer.py", line 561, in on_task_received
    strategy = strategies[type_]
KeyError: 'rehearsalbooking.tasks.calculate_timeslots'

and when i run the command to check the worker for noisebox

celery -A noisebox worker -l info

Traceback (most recent call last):
  File "c:\users\jonas\appdata\local\programs\python\python36-32\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\jonas\appdata\local\programs\python\python36-32\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\jonas\Envs\noisebox-env\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\__main__.py", line 14, in main
    _main()
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 279, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 481, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 503, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\app\utils.py", line 355, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\bin\base.py", line 506, in symbol_by_name
    return imports.symbol_by_name(name, imp=imp)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:\users\jonas\envs\noisebox-env\lib\site-packages\celery\utils\imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "c:\users\jonas\envs\noisebox-env\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'noisebox'

It tells me the app is not installed. Any thoughts on what might have gone wrong in this setup?

Upvotes: 1

Views: 1044

Answers (1)

bdoubleu
bdoubleu

Reputation: 6107

It's possible that there's an uncaught error when Django starts. For example, if celery doesn't have the correct permissions to a log file.

If you run django.setup() in celery.py file you'll be able to see any errors at start.

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

import django

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

django.setup()

app = Celery('noisebox')
app.autodiscover_tasks()


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

Upvotes: 0

Related Questions