Sunday 17 November 2019

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&" (The & 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&"
  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&" 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.

No comments:

Post a Comment

Featured post

Popular Posts