1

I'm referring to Django Celery documents.

I created celery.py in my proj/proj just as the document said. and then included __init__.py

celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

__init__.py

from __future__ import absolute_import
from .celery import app as celery_app

I installed pip install django-celery , then migrated python manage.py migrate djcelery It made some of the tables in my DB.

tasks.py

from __future__ import absolute_import
from celery import shared_task
import requests
import json

@shared_task
def post_notification(data,url):
    headers = {'content-type': 'application/json'}
    requests.post(url, data=json.dumps(data), headers=headers)

After that I called my task in my views as

task = post_notification.delay(data,url)
print task.id #it prints an id
print task.status # prints PENDING

But nothing gets logged into any of my tables.

I've read my threads on SO,Thread1 , Thread2 and many more given on these threads, but nothing happens.

It provides me the ID & status of the task but how do I save the task in the DB? Usually it should get logged into celery_taskmeta, but there's nothing in there.

Though the task gets execute but I want to save the task in DB as well. How can I do it? Is there something I'm missing?

Community
  • 1
  • 1
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • You shouldn't evaluate `app.conf` like that (`app.conf.update`), since it will load the django settings module early. You should set the `CELERY_RESULT_BACKEND` setting in your settings.py. Also did you remember to add `djcelery` to INSTALLED_APPS? If you want to set the backend as a default outside of your settings then you can use `app = Celery(..., backend='djcelery.backends.database:DatabaseBackend`). Note that the results are only updated when the worker writes the RETRY/FAILURE/SUCCESS states, the PENDING state is not saved to the db, instead all unknown ids will be PENDING. – asksol Feb 19 '14 at 15:39

2 Answers2

0

try this in celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from celery.schedules import crontab

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

app = Celery('app_name')

# 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)
app.conf.CELERY_TIMEZONE = 'UTC'
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)
Avinash Garg
  • 1,374
  • 14
  • 18
0

Add following in settings.py file

BROKER_URL = 'amqp://guest:guest@localhost//'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

And start the worker.

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267