Celery with Redis on Heroku

Once you'ce created a Django app, you can install Celery like so:

$ heroku addons:create redisgreen


It will say something like:

Adding laughing-truly-3765 to jobappli-production... done
Setting REDISGREEN_URL and restarting jobappli-production... done, v10
Your RedisGreen server has been built and is now available at redis://x:dsdsdsdsdsd@quick-cinnamon-1620.redisgreen.net:11042/
Use `heroku addons:docs redisgreen` to view documentation.


Note, that REDISGREEN_URL is available in the environment vars:

$ heroku config

Meaning, in your settings.py you refer to Celery like so:

import os

def get_env_variable(var_name):   
""" Get the environment variable or return exception """   
try:       
return os.environ[var_name]   
except KeyError:       
pass

# Django celery
# make sure celery does one thing at a time
CELERYD_CONCURRENCY = 1
BROKER_URL = get_env_variable('REDISGREEN_URL')
CELERY_RESULT_BACKEND = get_env_variable('REDISGREEN_URL')
CELERY_TASK_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json', ]

Now you have to make sure that Celery is also added to your Procfile:

web: sh -c 'cd src/ && gunicorn jobappli.wsgi --log-file -'
worker: sh -c 'cd src/ && celery worker --app=jobappli.celery.app'

And of course, scale your worker to 1:

$ heroku ps:scale worker=1