testdriven.io django raise 500 on heroku when tried to access postgres - django

I am following tutorial in https://testdriven.io/courses/tdd-django/deployment/ .
Everything worked perfectly when I request get without accessing postgres,
http://ancient-waters-04623.herokuapp.com/ping/ will return status 200
I done the migration and seed data
$ heroku run python manage.py migrate
$ heroku run python manage.py loaddata movies.json
when i run http://ancient-waters-04623.herokuapp.com/api/movies/ in my browser, it will give me error 500
the logs:
$ heroku logs --app=ancient-waters-04623 --tail
2020-09-07T10:13:51.045281+00:00 heroku[router]: at=info method=GET path="/ping/" host=ancient-waters-04623.herokuapp.com request_id=1895c42e-27d8-4d67-b5e8-98d0e5c3a3bd fwd="210.186.32.252" dyno=web.1 connect=0ms service=4ms status=200 bytes=225 protocol=http
I tried connect the database via dbeaver, and it connected and has correct data loaded.
I'm newbie in heroku either django, any help will be appreciated.
Thanks in advance

I was also getting a 500 back when accessing
https://APP_NAME.herokuapp.com/api/movies/
I turned on debug mode in Dockerfile.prod, and was able to view the DRF Browsable API with the expected data.
This led me to verify...
if not DEBUG:
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": (
"rest_framework.renderers.JSONRenderer",
)
}
...in settings.py, where I determined I was missing the final ',' (see also: https://testdriven.io/courses/tdd-django/deployment/#H-1-browsable-api)
After fixing that and disabling debug mode again in Dockerfile.prod, I rebuilt the image, pushed it to the heroku container registry and released it. Now, I get the expected JSON response from
https://APP_NAME.herokuapp.com/api/movies/

Related

Error Heroku , desc="No web processes running" method=GET path="/favicon.ico"? [duplicate]

error H14 happen while deploying to heroku
this is my procfile:
web: gunicorn -w 4 -b 0.0.0.0:$PORT -k gevent main:app
log on heroku:
2017-01-23T10:42:58.904480+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=meetcapstone.herokuapp.com request_id=df88efb5-a81a-4ac0-86dc-4e03d71266bb fwd="81.218.117.137" dyno= connect= service= status=503 bytes=
2017-01-23T10:42:59.009135+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=meetcapstone.herokuapp.com request_id=21cea981-36b0-4410-891f-548bbc29f0ee fwd="81.218.117.137" dyno= connect= service= status=503 bytes=
requirements:
Flask==0.11.1
passlib==1.7.0
SQLAlchemy==1.1.5
Werkzeug==0.11.15
gunicorn==19.0.0
gevent==1.2.1
The issue here is that you're not running any web dynos. You can tell Heroku to do this via:
$ heroku ps:scale web=1
This will force Heroku to spin up a web dyno, thereby executing your gunicorn command.
After 3 hours of debugging, I've figured out why my app was causing this error:
My Procfile was incorrectly cased
gunicorn wasn't installed in my venv
IMO, this error should be raised on Heroku's end. As a beginner, this sort of error is difficult to trace.
Update:
To clarify, Procfile is correctly cased and procfile is not correctly cased. It should start with a capital "P".
More info on dyno configuration – more on initializing your heroku app.
I ran into the same problem but from a different cause. I had the hobby tier, but then canceled it and reverted back to the free tier. Doing this caused the error and how I fixed it was just re running the command from the cli:
heroku ps:scale web=1
Before this command:
heroku ps:scale web=1
I had to remove and add buildpacks again and empty commit it and redeploy it to heroku.
heroku buildpacks:clear
heroku buildpacks:add --index heroku/python
I was having an issue here too. My problem was that my Procfile was "Procfile.txt" .
What solved my issue was to remove the file extension from Procfile, then recommit
and push stuff to heroku
Login to your Heroku dashboard and open your projects.
Go to Settings.
Delete heroku/python from the list of buildpacks
Then click Add buildpack → Choose "Python" → Save Changes.
Activate your environment in your code.
Run heroku ps:scale web=1.
And you're done!
This isn't the problem with your code, but I've gotten this error message a couple of times now and the mistake that I've made that has caused it has been writing
web:gunicorn
instead of
web: gunicorn
That space can really cause a lot of issues.
My issue is that Heroku removed the free plans. To solve such an issue go to Heroku and select/change your free plan to for example "eco" plan.
I have a UAT version I only enable during client development.
I have a custom dyno script but it's turned to the free version. So the app was not starting as my script was not running. When I enabled the Dyno the toggle was still off :rolleyes:
I don't have the reputation to reply to the correct comment, but for me the issue was that I didn't have the run.gunicorn.sh file in my root directory, this resulted in the same "No web processes running" error.
If you don't have this file, create it with contents:
gunicorn -b :5000 --access-logfile - --error-logfile - build:app
Where 'build' is the name of your python file (build.py in this case) and app is the name of your app in the code.
Also make sure that gunicorn is included in requirements.txt, like others have already pointed out.
Yeah I was also using web heroku-php-apache2 dyno and reverted it back to free tier and that caused the dyno to sleep fortunately executing heroku ps:scale web=1 -a <app name> did the magic.
Change your Procfile file from
web:gunicorn to web gunicorn (remove the ':')
I fixed the issue by going to Configure Dynos and enabling the only dyno I had manually.
uff..that took some time,so the fixes i had to make were:
'Procfile' with upper case P.
web: gunicorn wsgi:app (with a space after web: in procfile)
Making sure the requirements.txt are in the root project folder.
I was missing dynos on the web gui. The cli command to scale did not work. I also may have had an incorrect run:web declaration with missing $PORT. To fix:
heroku.yml must have a web declaration using the $PORT var:
build:
docker:
web: Dockerfile
run:
web: uvicorn main:app --reload --host 0.0.0.0 --port $PORT
I then pushed to heroku.
After that it must have added the web dyno, I could then run:
heroku ps:scale web=1
And now the fastapi uvicorn runs.
Pay attention to the Procfile naming and location (https://devcenter.heroku.com/articles/procfile) The Procfile is always a "simple text file" that is named Procfile without a file extension.(Procfile.txt not acceptable!) The Procfile must live in your app's root directory. It does not function if placed anywhere else.
Faced the exact same problem turns out I had the Profile in .gitignore
I was placing my django Procfile in the directory with settings.py and not the root directory and that gave me the H14 error. I fixed the error with this and I didn't need to do anything else they say.
Procfile
web: gunicorn <django-root-name(containing wsgi)>.wsgi
There are many things that can go wrong here. Its a combination of poor shepherding by heroku and ambiguous use between flask & gunicorn.
Here is a good guide that will get you up and running:
To anyone who may come across this...
delete your Procfile
create 'Procfile' with upper case P.
in your Procfile type: web: gunicorn <nameOfRootFile>:app (with a space after web: in procfile) mine for example was web: gunicorn app:app another way I wrote it that worked was this: web: gunicorn -w 4 "app:create_app()" -t 120
Making sure the requirements.txt are in the root project folder. (you can run pip freeze > requirements.txt if you do not have the file created
deploy to heroku
heroku ps:scale web=1 (you can specify app name to like this heroku ps:scale web=1 -a appname
finally in terminal run heroku restart
heroku open
these are all the steps i took to get mine to work
web: gunicorn weather.wsgi --log-file -
this worked for me, just make sure your Procfile is in the right format, and specify the app you are connecting to, in my case it's the weather app. Enjoy
What worked for me was adding on the second line of the procfile:
heroku ps:scale web=1
The first line must contain:
web: gunicorn "filename":"main method name"

Any calls to Heroku redis causes flask app to timeout

I've been developing a web app for the usage of computers and I'm attempting to store some information about active computers on redis. I'm hosting my Flask web app on Heroku and using Heroku redis.
When I'm testing on my local machine using the Heroku Redis URL, there are no problems and everything seems to be fine.
The problem I'm having is when I push and host on heroku. As soon as there is any interaction with the redis database the workers give a H12 web request timeout and none of my pages will load.
at=error code=H12 desc="Request timeout" method=GET path="/" host=my_app ... dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
At first I thought it was a problem with connecting to the redis database, but on launch I can see in the logs that it connects without a problem. I also thought it might be because I'm initializing some lists and hashes, but after removing those and simply doing a "set" on launch caused the same issue. The weird thing is that if I remove any gets, sets, etc. but I leave the connection to the redis database, everything runs fine. However, as soon as I try a simple get or set I get the aforementioned error. I've provided my code for initializing the redis instance below and note that I am using a blueprint model as shown out below. (I've also tried moving the initialization of the redis instance to the init file but it still gives the same results).
- app
- application
- __init__
- models
models.py
import os
from urllib.parse import urlparse
import redis
url = urlparse(os.environ.get("REDIS_URL"))
my_redis = redis.Redis(host=url.hostname, port=url.port, username=url.username,
password=url.password, ssl=True, ssl_cert_reqs=None)
The issue is that you'll need to change REDIS_URL to REDIS_TLS_URL or set ssl=False. It's better to set REDIS_TLS_URL for security reason though.
If you'll need to work both on the Heroku env and your local env, set a persistent Heroku env var such as
heroku config:set HEROKU=1
Then on you app.py add
if 'HEROKU' in os.environ:
url = urlparse(os.environ.get("REDIS_TLS_URL"))
else:
url = urlparse(os.environ.get("REDIS_URL"))

Heroku H12 Request timeout on Django application

I am running a Django application on Heroku.
when i make a request longer than 30 seconds, it stopped and the request returns 503 Service Unavailable.
These are the heroku logs
2021-10-20T07:11:14.726613+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/ajax/download_yt/" host=yeti-mp3.herokuapp.com request_id=293289be-6484-4f11-a62d-bda6d0819351 fwd="79.3.90.79" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
I tried to do some research on that and i found that i could increase the gunicorn (i am using gunicorn version 20.0.4) timeout in the Procfile. I tried increasing to 60 seconds, but the request still stops after 30.
This is my current Procfile:
web: gunicorn yetiMP3.wsgi --timeout 60 --log-file -
these are the buildpacks i'm using on my heroku app:
heroku/python
https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
How can i increase the request timeout?
Did i miss some heroku configuration?
Eventually i found that it's a Heroku dyno configuration.
The Heroku router will drop any request after 30 seconds, no matter how high you set the gunicorn timer.
More details in this official heroku article

Docker + Django + PostgreSQL + Heroku = Failed to bind to $PORT within 60 seconds

So I am building a Dockerized Django project and I want to deploy it to Heroku, but I am having a lot of issues. My issues are exactly the same as this post:
Docker + Django + Postgres Add-on + Heroku
Except I cannot use CMD python3 manage.py runserver 0.0.0.0:$PORT since I receive an invalid port pair error.
I'm just running
heroku container:push web
heroku container:release web
heroku open
After going to the site it stays loading until it says an error occurred. My log shows the following:
System check identified no issues (0 silenced).
2019-05-03T11:38:47.708761+00:00 app[web.1]: May 03, 2019 - 11:38:47
2019-05-03T11:38:47.709011+00:00 app[web.1]: Django version 2.2.1, using settings 'loan_app.settings.heroku'
2019-05-03T11:38:47.709012+00:00 app[web.1]: Starting development server at http://0.0.0.0:8000/
2019-05-03T11:38:47.709014+00:00 app[web.1]: Quit the server with CONTROL-C.
2019-05-03T11:38:55.505334+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=albmej-loan-application.herokuapp.com request_id=9037f839-8421-46f2-943a-599ec3cc6cb6 fwd="129.161.215.240" dyno= connect= service= status=503 bytes= protocol=https
2019-05-03T11:39:45.091840+00:00 heroku[web.1]: State changed from starting to crashed
2019-05-03T11:39:45.012262+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-05-03T11:39:45.012440+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-05-03T11:39:45.082756+00:00 heroku[web.1]: Process exited with status 137
The app works locally through a virtual environment and using Docker but just not on Heroku. Not sure what else to try. You can find my code at: https://github.com/AlbMej/Online-Loan-Application
Maybe I have some glaring problems in my Dockerfile or docker-compose.yml
The Answer is not correct.
If you use container with Dockerfile, you do not need any Profile.
Just use the $PORT variable to let heroku to determine which port to use.
https://help.heroku.com/PPBPA231/how-do-i-use-the-port-environment-variable-in-container-based-apps
Quick solution is to change your Procfile to this:
web: python subfolder/manage.py runserver 0.0.0.0:$PORT
That would work, but keep in mind that you are using the development server on production, which is a really bad idea! But if you are just toying around, that's ok.
However, if you're using this as a production app with real data, you should use a real production server. Then your Procfile would look like this:
web: gunicorn yourapp.wsgi --log-file -

ALLOWED_HOSTS is set, but Django is still giving a 500 error when DEBUG=False

In Django 1.5, ALLOWED_HOSTS became a required setting for when DEBUG=False. However, in my app, I have ALLOWED_HOSTS = ['.mycompany.com'], and I'm hitting the app via a "http://mysubdomain.mycompany.com" URL, but Django is still giving a 500 error/response.
With DEBUG=True, everything works fine, so it appears that Django is just ignoring that setting. Is there something else that could cause Django to bork out when DEBUG=False?
Is there a way to make Django spit out some details, in the log, about what its problem is? By default, it just spits out something like the following in the log:
at=info method=GET path="/admin/login/?next=/admin/" host=mysubdomain.mycompany.com request_id=69e99b68-5b57-4f3f-a10d-898deb507ae4 fwd="123.456.789.101" dyno=web.1 connect=1ms service=161ms status=500 bytes=253
which is no help at all...