Sunday 17 November 2019

Instructions on how to deploy a minimal heroku/django/celery/redis project on heroku

Once, you have a minimal django-celery-redis project setup on local, here is how you deploy it on heroku:
  1. Add to your tasks.py:

     import os
    
     app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
        CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
     
  2. Make sure your requirements.txt is like this:
     django
     gunicorn
     django-heroku
     celery
     redis
     
  3. Add to your Procfile: "worker: celery worker --app=hello.tasks.app"
  4. Make sure it still runs on local
  5. enter into terminal: "export REDIS_URL=redis://"
  6. run "heroku local&"
  7. run python
     import hello.tasks
     hello.tasks.add.delay(1,2)
Should return something like:

    <AsyncResult: e1debb39-b61c-47bc-bda3-ee037d34a6c4>
  1. "heroku apps:create minimal-django-celery-redis"
  2. "heroku addons:create heroku-redis -a minimal-django-celery-redis"
  3. "git add ."
  4. "git commit -m "Demo""
  5. "git push heroku master"
  6. "heroku open&"
  7. "heroku ps:scale worker=1"
  8. "heroku run python"

      import hello.tasks
      hello.tasks.add.delay(1, 2)
     
  9. You should see the task running in the application logs: "heroku logs -t -p worker"

Instructions on how to make a minimal heroku/django/celery/redis project

The following information is already somewhere on Stackoverflow but I thought I'd post it in on my blog for posterity.


The following is the steps i took to make a minimal heroku/django/celery/redis project in conjunction with the instructions here along with other sources I found on the web. Hopefully someone will find this useful.
  1. In your terminal, use the "heroku login" command to log in to the Heroku CLI.
  2. "git clone https://github.com/heroku/python-getting-started.git" to copy a basic django skeleton project to your local.
  3. rename python-getting-started to whatever.
  4. cd into this directory.
  5. run the following command: "pip install -r requirements.txt" Note: Postgres must be properly installed in order for this step to work properly.
  6. run the following command: "python manage.py collectstatic"
  7. Install redis on Mac: "brew install redis"
  8. Start redis server: "redis-server&amp;" (The &amp; at the end is to run it as a background process)
  9. Test if Redis server is running: "redis-cli ping". If it replies “PONG”, then it’s good to go!
  10. Install celery: "pip install celery"
  11. Make a tasks.py file in your application directory with the following code:
     from celery import Celery
    
     app = Celery('tasks', broker='redis://localhost:6379/0')
    
     @app.task
     def add(x, y):
         return x + y
     
  12. "cd .." back into root directory.
  13. Run celery: "celery worker -A=location of tasks&amp;"
  14. run: "python manage.py shell" in your root directory.
  15. As your tasks celery server has been started, you can now use it to run your task just by importing tasks.py script, e.g from Python interpreter interactive mode:
     
    import hello.tasks
    hello.tasks.add.delay(1, 1)
This should return an Async message.
  1. Push your local to heroku master.
** Note: If you run "celery worker -A=location of tasks.py&amp;" and it gives the message:
consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61]           
Connection refused.
 
Try restarting the redis server with the command: "brew services restart redis"

There you have it. A minimal heroku/django/celery/redis project! You can download it here.Instructions on how to deploy this to heroku.

** Note: In the working project the "celery worker" command is already included in the Procfile.

Featured post

Popular Posts