I have deployed a Django application on Heroku server. I have pushed my project without migrations and database manually by Heroku CLI. Then, pushing by Gitlab ci (which does the migrations before push) gives me app crashed error on Heroku side. I have encountered many "app crashed" errors before and could solve them by inspecting the logs. But this time I cannot understand what is the issue.
I am sure that files are completely pushed and the application is released.
Here is my Procfile:
web: gunicorn --pythonpath Code Code.wsgi --log-file -
My project is in the "Code" folder and my Django project name is Code.
Error part of the heroku logs:
2019-06-08T08:02:50.549319+00:00 app[api]: Deployed web (c1f5c903bedb) by user arminbehnamnia#gmail.com
2019-06-08T08:02:50.549319+00:00 app[api]: Release v6 created by user arminbehnamnia#gmail.com
2019-06-08T08:02:51.268875+00:00 heroku[web.1]: Restarting
2019-06-08T08:02:51.277247+00:00 heroku[web.1]: State changed from up to starting
2019-06-08T08:02:52.494158+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-06-08T08:02:52.517991+00:00 app[web.1]: [2019-06-08 08:02:52 +0000] [4] [INFO] Handling signal: term
2019-06-08T08:02:52.519983+00:00 app[web.1]: [2019-06-08 08:02:52 +0000] [11] [INFO] Worker exiting (pid: 11)
2019-06-08T08:02:52.529529+00:00 app[web.1]: [2019-06-08 08:02:52 +0000] [10] [INFO] Worker exiting (pid: 10)
2019-06-08T08:02:52.823141+00:00 app[web.1]: [2019-06-08 08:02:52 +0000] [4] [INFO] Shutting down: Master
2019-06-08T08:02:52.958760+00:00 heroku[web.1]: Process exited with status 0
2019-06-08T08:03:09.777009+00:00 heroku[web.1]: Starting process with command `python3`
2019-06-08T08:03:11.647048+00:00 heroku[web.1]: State changed from starting to crashed
2019-06-08T08:03:11.654524+00:00 heroku[web.1]: State changed from crashed to starting
2019-06-08T08:03:11.625687+00:00 heroku[web.1]: Process exited with status 0
.
.
.
2019-06-08T08:17:16.898569+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=makan-system.herokuapp.com request_id=de4fbb8e-cd14-4263-bb6f-a8d0f956a519 fwd="69.55.54.121" dyno= connect= service= status=503 bytes= protocol=https
My .gitlab-ci.yml file:
stages:
- test
- build
- push
tests:
image: docker:latest
services:
- docker:dind
stage: test
before_script:
- docker login -u armin_gm -p $PASSWORD registry.gitlab.com
script:
- docker build . -t test_django
- docker ps
- docker run --name=testDjango test_django python /makanapp/Code/manage.py test registration
when: on_success
build:
image: docker:latest
services:
- docker:dind
stage: build
before_script:
- docker login -u armin_gm -p $PASSWORD registry.gitlab.com
script:
- docker build -t registry.gitlab.com/armin_gm/asd_project_98_6 .
- docker push registry.gitlab.com/armin_gm/asd_project_98_6
push_to_heroku:
image: docker:latest
stage: push
services:
- docker:dind
script:
# This is for gitlab
- docker login -u armin_gm -p $PASSWORD registry.gitlab.com
#- docker pull registry.gitlab.com/armin_gm/asd_project_98_6:latest
- docker build . -t push_to_django
- docker ps
# This is for heroku
- docker login --username=arminbehnamnia#gmail.com --password=$AUTH_TOKEN registry.heroku.com
- docker tag push_to_django:latest registry.heroku.com/makan-system/web:latest
- docker push registry.heroku.com/makan-system/web:latest
- docker run --rm -e HEROKU_API_KEY=$AUTH_TOKEN wingrunr21/alpine-heroku-cli container:release web --app makan-system
My Dockerfile:
# Official Python image
FROM python:latest
ENV PYTHONUNBUFFERED 1
# create root directory for project, set the working directory and move all files
RUN mkdir /makanapp
WORKDIR /makanapp
ADD . /makanapp/
# Web server will listen to this port
EXPOSE 8000
# Install all libraries we saved to requirements.txt file
#RUN apt-get -y update
#RUN apt-get -y install python3-dev python3-setuptools
RUN pip install -r requirements.txt
RUN python ./Code/manage.py makemigrations
RUN python ./Code/manage.py migrate --run-syncdb
I install the heroku toolbelt from the website and use pip to install django toolbelt on both mac and ubuntu vm copy.
Both of them only return one line when using foreman start:
Procfile:
web: gunicorn hero.wsgi
foreman start:
23:44:46 web.1 | started with pid 4382
Ctrl+C and I got:
23:47:36 system | sending SIGTERM to all processes
23:47:36 web.1 | terminated by SIGTERM
I have read about the https://github.com/ddollar/foreman/wiki/Missing-Output
and did:
script.py:
PYTHONUNBUFFERED=True
and run it with python -u script.py
What am I doing wrong? Any advice will be appreciated.
I am trying to deploy the simplest possible "hello world" celery configuration on heroku for my Django app. My Procfile is as follows:
web: gunicorn myapp.wsgi
worker: celery -A myapp worker -l info -B -b amqp://XXXXX:XXXXX#red-thistle-3.bigwig.lshift.net:PPPP/XXXXX
This is the RABBITMQ_BIGWIG_RX_URL that I'm giving to the celery worker. I have the corresponding RABBITMQ_BIGWIG_TX_URL in my settings file as the BROKER_URL.
If I use these broker URLs in my local dev environment, everything works fine and I can actually use the Heroku RabbitMQ system. However, when I deploy my app to Heroku it isn't working.
This Procfile seems to work (although Celery is giving me memory leak issues).
web: gunicorn my_app.wsgi
celery: celery worker -A my_app -l info --beat -b amqp://XXXXXXXX:XXXXXXXXXXXXXXXXXXXX#red-thistle-3.bigwig.lshift.net:PPPP/XXXXXXXXX
Introduction:
I am following the Getting Started with Django on Heroku quick start guide.
I intend to apply the Two Scoops of Django book philosophy about working with virtualenvs and projects. Audrey Roy and Daniel Greenfeld (authors) like to put all the environments in one directory and all the projects in another.
I also intend to apply the Two Scoops of Django book philosophy about placing what is generated by the django-admin.py startproject management command inside another directory which serves as the git repository root (a.k.a. three-tiered approach).
The layout at the highest level:
repository_root/
django_project_root/
configuration_root/
Locally:
OS X 10.8.4
pip==1.4.1
virtualenv==1.10.1
virtualenvwrapper==4.1.1
wsgiref==0.1.2
.bashrc contains:
export WORKON_HOME=$HOME/Envs
export PROJECT_HOME=$HOME/Projects
~/Envs
~/Projects
Inside hellodjango_venv:
Django==1.5.2
dj-database-url==0.2.2
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.1
static==0.4
From Terminal:
mac-pol:~ oubiga$ mkdir -p Projects/hellodjango_rep/hellodjango
mac-pol:~ oubiga$ cd Projects/hellodjango_rep/hellodjango
mac-pol:hellodjango oubiga$ mkvirtualenv hellodjango_venv
New python executable in hellodjango_venv/bin/python2.7
Also creating executable in hellodjango_venv/bin/python
Installing Setuptools..................................
...
..................................................done.
(hellodjango_venv)mac-pol:hellodjango oubiga$ pip install django-toolbelt
...
Successfully installed django-toolbelt django psycopg2 gunicorn dj-database-url dj-static static
Cleaning up...
(hellodjango_venv)mac-pol:hellodjango oubiga$ django-admin.py startproject hellodjango .
(hellodjango_venv)mac-pol:hellodjango oubiga$ touch Procfile && open Procfile
Edit Procfile:
web: gunicorn hellodjango.wsgi
Start the process:
(hellodjango_venv)mac-pol:hellodjango oubiga$ foreman start
01:30:37 web.1 | started with pid 638
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Starting gunicorn 18.0
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Listening at: http://0.0.0.0:5000 (638)
01:30:37 web.1 | 2013-09-04 01:30:37 [638] [INFO] Using worker: sync
01:30:37 web.1 | 2013-09-04 01:30:37 [641] [INFO] Booting worker with pid: 641
CTRL+C
So far, so good.
At the moment my Projects tree is:
~/Projects/
hellodjango_rep/
hellodjango/ cwd
manage.py
Procfile
hellodjango/
__init__.py
settings/
urls.py
wsgi.py
and my Virtualenvs tree is:
~/Envs/
get_env_details
initialize
postactivate
postdeactivate
postmkproject
postmkvirtualenv
postrmproject
postrmvirtualenv
preactivate
predeactivate
premkproject
premkvirtualenv
prermproject
prermvirtualenv
hellodjango_venv/
bin/
include/
lib/
But, do you remember the three-tiered approach? How can I achieve it?
I am quite sure hellodjango_rep/ later on will contain at least: .git, .gitignore and requirements.txt
Research:
On the #django IRC channel, Jacob Kaplan-Moss answered:
...the Procfile needs to be at the top-level of your git repo...
Neil Middleton, Engineer at Heroku, answered this question "Procfile declares types -> (none) in Heroku" in SO:
...your Procfile needs to be in the root of your git repository that
is pushed to Heroku...
From Heroku Dev Center:
...Process types are declared via a file named Procfile placed in the
root of your app...
As a First Attempt:
I move Procfile up one folder level.
(hellodjango_venv)mac-pol:hellodjango oubiga$ cd ..
At the moment my Projects tree is:
~/Projects/
hellodjango_rep/ cwd
Procfile
hellodjango/
manage.py
hellodjango/
__init__.py
settings/
urls.py
wsgi.py
I start the process again:
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
I'll get ImportError: No module named hellodjango.wsgi
Debugging:
The ImportError seems to come from /Users/oubiga/Envs/hellodjango_venv/lib/python2.7/site-packages/gunicorn/util.py
def import_app(module):
parts = module.split(":", 1)
if len(parts) == 1:
module, obj = module, "application"
else:
module, obj = parts[0], parts[1]
try:
__import__(module) # <-- line 354
except ImportError:
if module.endswith(".py") and os.path.exists(module):
raise ImportError("Failed to find application, did "
"you mean '%s:%s'?" % (module.rsplit(".", 1)[0], obj))
else:
raise
...
No module named hellodjango.wsgi
As a Second Attempt:
Procfile comes back at the previous level. Procfile and manage.py belong together again.
(hellodjango_venv)mac-pol:hellodjango_rep oubiga$ foreman start
I'll get ERROR: Procfile does not exist. It's quite obvious why it happens.
Hypothesis:
From Django documentation:
...manage.py is automatically created in each Django project.
manage.py is a thin wrapper around django-admin.py that takes care of
two things for you before delegating to django-admin.py:
- It puts your project’s package on sys.path.
- It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file...
I'm not sure if the problem is related to this.
So:
Is it possible to have a Procfile and a manage.py file in different folder level ?
What is wrong with this approach?
Thank you in advance.
Firstly I have to say, that I haven't done so wide research and have used Heroku about 3 times. But:
(Your Seccond Attempt):
Procfile really should be in the top level directory. If you move the Procfile deeper gunicorn can't find it.
(Your First Attempt):
And having your Procfile on the top level directory you should state path to wsgi. Which is not possible yet. So you shoud make __init__.py file in the first "hellodjango" directory, to mark it as "module". Then you shoud change the path in Procfile to: hellodjango.hellodjango.wsgi
Hope this helps.
I ran into this using a Two Scoops layout, and my solution was to keep the standard layout (Procfile in repository_root, manage.py in project_root) and to make Procfile a script that changes directory into project_root before running gunicorn.
e.g., in Procfile:
web: sh -c 'cd ./mysite/ && exec gunicorn mysite.wsgi --log-file -'
You could equally have Procfile call an external script:
web: bash scripts/heroku_run
I prefer this to adding additional module paths as it can mess up imports in settings.py and urls.py, etc.
I'm working through the Heroku's Django tutorial and I got all the way down to 'Using a different WSIG server'.
When I try to use gunicorn I get the following error:
requirements.txt
Django==1.4.1
distribute==0.6.28
dj-database-url==0.2.1
psycopg2==2.4.5
gunicorn==0.14.6
Procfile
web: gunicorn djtut.wsgi -b 0.0.0.0:$PORT
(venv) C:\Users\xxxx\Documents\Python\djtut>foreman check
valid procfile detected (web)
(venv) C:\Users\xxxx\Documents\Python\djtut>foreman start
10:53:05 web.1 | started with pid 5652
10:53:06 web.1 | exited with code 1
10:53:06 web.1 | Traceback (most recent call last):
10:53:06 system | sending SIGKILL to all processes
10:53:06 | File "C:\Users\xxxx\Documents\Python\djtut\venv\Scripts\
gunicorn-script.py", line 9, in <module>
(venv) C:\Users\xxxxx\Documents\Python\djtut>
Works fine using the dev server on Heroku. I'm on Windows 7. Any ideas? I suspect it is an OS issue?
thanks,
AP
Unfortunately, Gunicorn doesn't work on Windows.