Working example of celery with mongo DB

celery, mongodb

Solution

To use MongoDB as your backend store you have to explicitly configure Celery to use MongoDB as the backend.

http://docs.celeryproject.org/en/latest/getting-started/brokers/mongodb.html#broker-mongodb

As you said the documentation does not show a complete working example. I just started playing with Celery but have been using MongoDB. I created a short working tutorial using MongoDB and Celery http://skillachie.com/?p=953

However these snippets should contain all you need to get a hello world going with Celery and MongoDB

celeryconfig.py

 from celery.schedules import crontab

CELERY_RESULT_BACKEND = "mongodb"
CELERY_MONGODB_BACKEND_SETTINGS = {
    "host": "127.0.0.1",
    "port": 27017,
    "database": "jobs", 
    "taskmeta_collection": "stock_taskmeta_collection",
}

#used to schedule tasks periodically and passing optional arguments 
#Can be very useful. Celery does not seem to support scheduled task but only periodic
CELERYBEAT_SCHEDULE = {
    'every-minute': {
        'task': 'tasks.add',
        'schedule': crontab(minute='*/1'),
        'args': (1,2),
    },
}

tasks.py

from celery import Celery
import time 

#Specify mongodb host and datababse to connect to
BROKER_URL = 'mongodb://localhost:27017/jobs'

celery = Celery('EOD_TASKS',broker=BROKER_URL)

#Loads settings for Backend to store results of jobs 
celery.config_from_object('celeryconfig')

@celery.task
def add(x, y):
    time.sleep(30)
    return x + y

Problem

I'm new to celery, and am working on running asynchronous tasks using Celery. - I want to save the results of my tasks to MongoDB. - I want to use the AMQP broker. Celery project examples didn't help me much. Can anyone point me to some working examples?

Original source