Problems configuring deployment with Heroku/gunicorn/django
Asked Answered
D

5

10

I'm trying to run my django application on heroku.

Folder structure:

app/
  Procfile
  docs/
  ...
  project/
    manage.py
    wsgi.py
    <django apps>

Procfile

web: gunicorn --pythonpath="$PWD/project" wsgi:application --log-file=-

Error I'm getting:

2015-02-16T16:05:00.646316+00:00 heroku[web.1]: Starting process with command `gunicorn --pythonpath="$PWD/project" wsgi:application --log-file=-`
2015-02-16T16:05:02.697633+00:00 app[web.1]: [2015-02-16 16:05:02 +0000] [3] [INFO] Listening at: http://0.0.0.0:44846 (3)
2015-02-16T16:05:02.709567+00:00 app[web.1]: [2015-02-16 16:05:02 +0000] [9] [INFO] Booting worker with pid: 9
2015-02-16T16:05:02.696968+00:00 app[web.1]: [2015-02-16 16:05:02 +0000] [3] [INFO] Starting gunicorn 19.1.1
2015-02-16T16:05:02.697790+00:00 app[web.1]: [2015-02-16 16:05:02 +0000] [3] [INFO] Using worker: sync
2015-02-16T16:05:02.793753+00:00 app[web.1]: [2015-02-16 16:05:02 +0000] [10] [INFO] Booting worker with pid: 10
2015-02-16T16:05:03.157305+00:00 app[web.1]: Traceback (most recent call last):
2015-02-16T16:05:03.157311+00:00 app[web.1]:   File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
2015-02-16T16:05:03.157351+00:00 app[web.1]:     sys.exit(run())
2015-02-16T16:05:03.157383+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
2015-02-16T16:05:03.157461+00:00 app[web.1]:     WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2015-02-16T16:05:03.157463+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 185, in run
2015-02-16T16:05:03.157506+00:00 app[web.1]:     super(Application, self).run()
2015-02-16T16:05:03.157527+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 71, in run
2015-02-16T16:05:03.157604+00:00 app[web.1]:     Arbiter(self).run()
2015-02-16T16:05:03.157607+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 196, in run
2015-02-16T16:05:03.157635+00:00 app[web.1]:     self.halt(reason=inst.reason, exit_status=inst.exit_status)
2015-02-16T16:05:03.157656+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 292, in halt
2015-02-16T16:05:03.157730+00:00 app[web.1]:     self.stop()
2015-02-16T16:05:03.157744+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 343, in stop
2015-02-16T16:05:03.157814+00:00 app[web.1]:     time.sleep(0.1)
2015-02-16T16:05:03.157836+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 209, in handle_chld
2015-02-16T16:05:03.157887+00:00 app[web.1]:     self.reap_workers()
2015-02-16T16:05:03.157908+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 459, in reap_workers
2015-02-16T16:05:03.158009+00:00 app[web.1]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2015-02-16T16:05:03.158075+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2015-02-16T16:05:03.904714+00:00 heroku[web.1]: Process exited with status 1
2015-02-16T16:05:03.914410+00:00 heroku[web.1]: State changed from starting to crashed

Update 1 My wsgi.py file

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config")
os.environ.setdefault("DJANGO_CONFIGURATION", "Production")

from configurations.wsgi import get_wsgi_application
application = get_wsgi_application()

Here I am just adding some text because SO has this silly minimum amount of text that must be written in a question. I mean, I do get it that quality needs to be kept, but if the problem is self explanatory why force people to write unneeded text? Thanks and have a great day!

Dorothi answered 16/2, 2015 at 16:8 Comment(4)
How did your WSGI file get there, rather than inside project/project which is where Django puts it? And you should show the content of that file.Althing
I moved wsgi.py to the project main folder, do you think that this is the issue?Dorothi
There's too much we don't know here. What is "config" as your DJANGO_SETTINGS_MODULE? Have you really called your settings file config.py rather than settings.py? And is it in the same directory as wsgi.py?Althing
I'm using pydanny's cookiecutter project template as in here: github.com/pydanny/cookiecutter-django the settings.py file(s) are in their own folder called config and are names common.py for the base settings and then local.py or production.py are used in locale or production environmentsDorothi
D
5

Adding --preload to the gunicorn command in the Procfile will make your Traceback a lot more readable and show you the actual errors.

Exmaple:

gunicorn project.wsgi:application --preload --workers 1
Donielle answered 16/12, 2015 at 22:30 Comment(0)
F
3

I had a similar problem, after reading he docs for gunicorn, I was able to add

--log-level debug

to the

web: gunicorn project.wsgi:application --log-file -

such that its

web: gunicorn project.wsgi:application --log-file - --log-level debug

In my case the path was also an issue.

Fasciation answered 12/7, 2017 at 17:9 Comment(0)
D
2

Finally found the solution, it was a missing dependency of django-organizations. I find it crazy that there is no way (at least not that I could find) to see any useful error output from heroku. I finally did

heroku run bash --app <app_name>

and then ran the wsgi.py file line by line, finally getting some meaningful error on

from configurations.wsgi import get_wsgi_application  # noqa

It was then clear that it's a missing module error, installed it and everything runs perfectly fine.

Dorothi answered 16/2, 2015 at 23:15 Comment(1)
Thanks you so much! I was using flask and I forgot to put one of the requirements in the requirements.txt file. Dunno why it gave this cryptic error message instead of the usual "module not found"Dacha
F
1

Change your Procfile to:

web: gunicorn project.wsgi:application --log-file=-

You're missing the project module in the python path to the wsgi.py file.

Fukien answered 16/2, 2015 at 16:19 Comment(2)
I Did it, it wasn't finding my config file so changed the line to: gunicorn project.wsgi:application --pythonpath="$PWD/project" --log-file - Still getting gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>Dorothi
Why are you specifying the pythonpath. It doesn't look like the it's required in Heroku's docs.Fukien
T
0

I solved it. I followed these steps: Remove all the unused libraries. Delete requirements.txt file. Create a new requirements.txt file. Commit to Git and then deploy on Heroku.

Transcendent answered 23/3, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.