1

This configuration is correct. I was starting celery the wrong way :(, without specifying the project name. (celery worker -A hockey_manager -l info

I did upgrade to Django 1.9 from 1.6.5 and I can't make the celery configuration work again.

After almost two days of searching for a solution I didn't find anything working.

Celery doesn't detect my tasks. I tried with:

  • CELERY_IMPORTS
  • autodiscover_tasks
  • bind=True

Dependencies

amqp==2.0.3 
celery==3.1.23 
Django==1.9.8 
django-celery==3.1.17
kombu==3.0.35

PROJECT STRUCTURE

enter image description here

hockey_manager/__init__.py

from __future__ import absolute_import

# 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

hockey_manager/celery.py

from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hockey_manager.settings.common')

app = Celery('hockey_manager')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')

# load task modules from all registered Django app configs.
app.autodiscover_tasks(['hockey'])

# Using a string here means the worker will not have to
# pickle the object when using Windows.
# app.config_from_object('django.conf:settings')
# app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# Celery backend configs
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)

if __name__ == '__main__':
    app.start()


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

hockey_manager/settings/common.py

INSTALLED_APPS = [
    ...
    'hockey',
    'djcelery',
    'kombu.transport.django',
    ...
]

##
# Celery
##
# BROKER_POOL_LIMIT = 3
BROKER_URL = os.environ.get('CLOUDAMQP_URL')
#BROKER_URL = 'django://'

# List of modules to import when celery starts.
CELERY_IMPORTS = ('hockey.tasks', )

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)

CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

hockey/tasks.py

from __future__ import absolute_import
from hockey_manager.celery import app

@app.task(name='hockey.tasks.league_schedule_results')
def league_schedule_results(schedule, statistics):
    ...

Error from Celery

[2016-07-23 17:05:46,231: ERROR/MainProcess] Received unregistered task of type 'hockey.tasks.league_schedule_results'. The message has been ignored and discarded.

I also receive a deprecation warning from amqp starting from version 2.0:

AMQPDeprecationWarning: The .transport attribute on the connection was accessed before the connection was established. This is supported for now, but will be deprecated in amqp 2.2.0.

Since amqp 2.0 you have to explicitly call Connection.connect()
before using the connection.

  W_FORCE_CONNECT.format(attr=attr)))
Pietro
  • 1,815
  • 2
  • 29
  • 63

1 Answers1

6

use django-celery module.

here is link to example how to use django-celery with django 1.9.1+

Hiren patel
  • 971
  • 8
  • 25
  • I'm already using it. It's showed in my dependencies list. Version 3.1.17 – Pietro Jul 25 '16 at 09:48
  • I am using it with django 1.9.1 and i dont need celery.py file for configuration. I just gave you suggestion to try steps mentioned in link. May be following that steps , you will configure celery correctly with django 1.9.1 – Hiren patel Jul 25 '16 at 09:56
  • I'll give a try and let you know. Thanks – Pietro Jul 25 '16 at 09:58
  • I have no idea why, but it works :D. Thanks for the tip ;) Would be great to know the reason of this. – Pietro Jul 25 '16 at 11:26
  • I'm feeling such an idiot :D. The configuration I have is correct. The problem is that I was starting celery with: celery worker -l info, instead of: celery -A hockey_manager worker -l info – Pietro Jul 25 '16 at 18:16
  • I didn't use your suggested configuration because I didn't know how to access the Celery app to revoke a task. Any idea? ex: app.control.revoke(revoke_id, terminate=True) – Pietro Jul 25 '16 at 18:18
  • 1
    http://stackoverflow.com/questions/8920643/cancel-an-already-executing-task-with-celery – Hiren patel Jul 26 '16 at 04:46