Run Django background task on heroku

Viewed 590

So, I have a Django Project which has a background task for a method to run. I made following adjustments to procfile Initially

web: python manage.py collectstatic --no-input; gunicorn project.wsgi --log-file - --log-level debug

Now

web: python manage.py collectstatic --no-input; gunicorn project.wsgi --log-file - --log-level debug
worker: python manage.py process_tasks

Inspite of adding worker, when I deploy my project on heroku it does not run the background task. The background task gets created and can be seen registered in django admin but does not run. I hoped after reading various articles (one of them being https://medium.com/@201651034/background-tasks-in-django-and-heroku-58ac91bc881c) adding worker: python mnanage.py process_tasks would do the job but it didn't.

If I execute in my heroku cli: heroku run python manage.py process_tasks it only runs on the data which was initially present in database and not on any new data that I add after deployment.

Note: python manage.py process_tasks is what I use to get the background task to run on my local server.

So, if anyone could help me in running the background task after deployment on heroku.

1 Answers

Your Procfile seems right, you need to scale your worker dyno using heroku scale worker=1 from heroku CLI or you can also scale your worker dyno from heroku dashboard.

For scaling worker dyno through browser:-

  1. Visit https://dashboard.heroku.com/apps/<your-app-name>/resources
  2. Edit your worker dyno and scale it from there confirm your changes

In CLI use command heroku logs -t -p worker to see status and logs for worker dyno

Related