Django and celery: tasks not imported

I am using Django with Celery to run two tasks in the background related to parsing / email.

Structure:

project /api /core tasks.py settings.py 
File

settings.py contains:

 BROKER_URL = 'django://' BROKER_BACKEND = "djkombu.transport.DatabaseTransport" #celery BROKER_HOST = "localhost" BROKER_PORT = 5672 BROKER_USER = "guest" BROKER_PASSWORD = "guest" BROKER_VHOST = "/" sys.path.append(os.path.dirname(os.path.basename(__file__))) CELERY_IMPORTS = ['project.core.tasks'] import djcelery djcelery.setup_loader() # .... INSTALLED_APPS = ( #... 'kombu.transport.django', 'djcelery', ) 

tasks.py contains:

 from celery.task import Task from celery.registry import tasks class ParseEmails(Task): #... class ImportGMailContactsFromGoogleAccount(Task): #... tasks.register(ParseEmails) tasks.register(ImportGMailContactsFromGoogleAccount) 

Also added to wsgi.py

 os.environ["CELERY_LOADER"] = "django" 

Now I have this application hosted on a WebFactional server. On my localhost, this works fine, but on the WebFaction server, where the Django application is deployed to the Apache server, I get:

 2013-01-23 17:25:00,067: ERROR/MainProcess] Task project.core.tasks.ImportGMailContactsFromGoogleAccount[df84e03f-9d22-44ed-a305-24c20407f87c] raised exception: Task of kind 'project.core.tasks.ImportGMailContactsFromGoogleAccount' is not registered, please make sure it imported. 

But tasks are displayed as registered. If I run

 python2.7 manage.py celeryd -l info 

I get:

  -------------- celery@web303.webfaction.com v3.0.13 (Chiastic Slide) ---- **** ----- --- * *** * -- [Configuration] -- * - **** --- . broker: django://localhost// - ** ---------- . app: default:0x1e55350 (djcelery.loaders.DjangoLoader) - ** ---------- . concurrency: 8 (processes) - ** ---------- . events: OFF (enable -E to monitor this worker) - ** ---------- - *** --- * --- [Queues] -- ******* ---- . celery: exchange:celery(direct) binding:celery --- ***** ----- [Tasks] . project.core.tasks.ImportGMailContactsFromGoogleAccount . project.core.tasks.ParseEmails 

I thought it might be a relative import error, but I assumed that changes to settings.py and wsgi.py would prevent this.

I think that several versions of Python supported by WebFactional may be relevant, however, I have installed all the libraries for Python 2.7 and I am also running Django for 2.7, so there should be no problem with this.

Running in localhost using celeryd -l info Tasks also appear in the list when the worker starts, but it does not display an error when the task is called - it works fine.

thanks

+4
source share
1 answer

I had the same problem in the new Ubuntu 12.04 / Apache / mod_wsgi / Django 1.5 / Celery 3.0.13 working environment. Everything works fine on my Mac Os X 10.8 laptop and my old server (which has Celery 3.0.12), but not on the new server.

There seems to be a problem in the celery: https://github.com/celery/celery/issues/1150

My original solution changed my task based on the Task class to @task decorator based on something like this:

 class CreateInstancesTask(Task): def run(self, pk): management.call_command('create_instances', verbosity=0, pk=pk) tasks.register(CreateInstancesTask) 

like that:

 @task() def create_instances_task(pk): management.call_command('create_instances', verbosity=0, pk=pk) 

Now this task seems to work, but of course I need to do some more tests ...

+2
source

All Articles