500 Internal Server Error Heroku - python

I have a little system built for learning purposes with Flask-SQLAlchemy and deployed on Heroku(python/postgresql:
http://monkey-me.herokuapp.com/
https://github.com/coolcatDev/monkey-me
My app works fine locally and I have unit tested its functionality and use cases through 14 successfully passed tests.
When I deploy everything seems to go perfectly. Its deployed, I define environment variable APP_SETTINGS>>>config.BaseConfig, I run the db_create.py script to initialize the db. I create some users:
username-userpassword:
Alex-passwordAlex
Jack-passwordJack
Sara-passwordSara
But one thing is missing...I go to the users page from the main navigation bar and I get a 5oo internal server error saying:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
My app code on app.py has debug mode on:
if __name__ == '__main__':
app.run(debug=True)
But no further error is displayed.
Notice that if I access the system on heroku and I am logged out, if I go to Users I am as I should redirected to the login page so thats how far I have gone with the debug...
If I set the environment variable LAUNCHY_DEBUG on heroku to either true or false everything goes crazy and new problems appear...like I cant delete a user, the profile images wont load....
If I remove the LAUNCHY_DEBUG var from heroku the new problems(images wont load, can't remove user..) persist among the original 500 error of the users page.
Thanks in advance for any suggestion with the debugging

Use the following to get more feedback written in logs:
import logging
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
That reveals the problem:
ProgrammingError: (ProgrammingError) column "user_bf.id" must appear in the GROUP BY clause or be used in an aggregate function
Modify query:
userList = (
users.query
.add_column(db.func.count(friendships.user_id).label("total"))
.add_column(user_bf.id.label("best_friend"))
.add_column(user_bf.userName.label("best_friend_name"))
.outerjoin(friendships, users.id == friendships.user_id)
.outerjoin(user_bf, users.best_friend)
.group_by(users.id, user_bf.id)
.order_by(test)
.paginate(page, 6, False)
)
Regarding the images disappearing:
Any file written on a filesystem hosted on heroku will be deleted on dynos end of lifecycle.

Related

Python Django /w Microsoft Graphs - I keep getting value error "state missing from auth_code_flow"

Python Django /w Microsoft Graphs -
I'm following this Microsoft Tutorial for building Django apps with Microsoft Graph (using it on my existing Django webapp), and I am having an issue with authentication: https://learn.microsoft.com/en-us/graph/tutorials/python
I'm on the step 'Add Azure AD authentication' and, after implementing,
I hit the sign in button and enter credentials...and I keep getting value error "state missing from auth_code_flow".
The "callback" method is only making it to result=get_token_from_code(request) and then fails.
Here is the get_token_from_code method:
def get_token_from_code(request):
cache = load_cache(request)
auth_app = get_msal_app(cache)
# Get the flow saved in session
flow = request.session.pop('auth_flow', {})
result = auth_app.acquire_token_by_auth_code_flow(flow, request.GET)
save_cache(request, cache)
return result
What I'm trying to do is eventually access excel online from my webapp.
Any help is greatly appreciated!
I just had this issue and resolved it. It is one of these two things:
You are starting out at 127.0.0.1:8000 and then when you're redirected you're at localhost:8000, which is a different domain. The sessions aren't remembered from one domain to the other. The solution is to start out on localhost:8000 so that the session persists across login.
Your browser is using super-strict cookie settings. Microsoft Edge appears to default to this mode on localhost and 127.0.0.1. There is a lock or shield icon in or near your address bar that lets you relax the restrictions on your cookie settings.
Try one or both of these and you should succeed.
I'm a beginner coder, so i'm pretty sure im just circumventing around the error. But replacing the website URL with http://localhost:8000/# and re running it somehow got around the error. maybe that could be of some use.
If you are running on chrome, rather than running application on http://127.0.0.1:8000 run it on http://localhost:8000, because chrome isn't saving the cookies when the ip address is being used.

Guincorn seems to be deleting Flask API data on each call

I am facing this issue where when I call an endpoint to my API (built in Flask, running on gunicorn with 9 workers), the data loads the first time, then, when I refresh the page, it seems to throw the follow error:
{'message': 'Internal Server Error'}
For some detail, the API does not store anything in persistent data, it is stored in runtime/ram before a specific endpoint is called (in my case, localhost:8000/see/), which then frees** the ram by deleting said data.
** I have disabled the function to free up stored data so no matter how many times I refresh the page, it should show the data.
So, I am facing two issues:
1: I have an endpoint localhost:8000/data/ where I store information being sent to the server. When I call this endpoint with that is inside of a dict, it should return all the data for that int. All data is shown at first, but when I refresh the page, it gives me the error I mentioned above. After a few refreshes of getting this error, the data shows up again but is gone on the next refresh.
2: When I call the /see/ endpoint, it should show me all the data I have collected, which it does but then when I refresh this page, it returns {} (disabled clearing, as mentioned above), which means the data is being cleared from runtime.
Furthermore, I have tried to fix this issue using the base Flask dev server, where I DO NOT encounter this error. I have also tried using another WSGI service: Waitress, which also does not give me these issues. I have tried running Gunicorn with supervisor and without, but I still am encountering these errors.
Any help would be appreciated as I would like to use Gunicorn. If there are good alternatives, please let me know.

debug python api: where to view `web.debug` messages

I inherited an NGINX server, which hosts an API, as part of a research project I'm involved in. I am responsible for the iOS portion, but I've been getting a lot of 500 Internal Server Error which is obviously a problem for the app.
Unfortunately, the grad students who built it are long gone and there are no READMEs to help get me acquainted.
I've never worked on an API before and am struggling with figuring out how this one works/how to debug it.
I see in the code some commented out web.debug messages. For example:
def imgpath2url(path):
"""Given a path for an image (e.g., from the 'leaf' table of the database ), returns a valid API url"""
els = path.split('/')
#web.debug('Getting imgurl for %s' % (path))
if path.startswith('findingspecies'):
return '/species/' + fixspeciesname(els[1]) + '/images/' + els[-1]
elif path.startswith('uploads'):
id = os.path.basename(path).rsplit('.',1)[0]
return '/%s/original.jpg' % (id)
return ''
Where are these debug messages printed out to? Are they printed out to a browser? Can I view these printed debu statements as I make calls to the api within the iOS app?
The errors in this case are usually written to the web server error logs which in case of NGINX can be found at /var/log/nginx/error.log check that file using any text editor i.e nano or use cat, tail.
Also the web debug functions seems to belong to a framework like Odoo or so (A non-native python function). Which can be isolated if you can share more code or name the specific framework the API is built on.

Flask project works in local environment, throws weird error on heroku about missing files (that are dynamically added anyway!)

I'm developing a flask application for image editing and up until a couple commits, it's been working great on both my local environment and heroku. I commited a tiny change and now I'm seeing a weird error. Yes, rolling back fixes the error but based on the change I made, I can't explain why heroku would be giving me trouble.
The app pulls images from an api and stores them in a tmp directory on the server* where edits are made to the image. The tmp directory is part of the git repo and the images should be pulled from the api every time the page loads. The error I'm getting on heroku now is:
File "/app/app/views.py", line 71, in edit_result
Jan 17 22:05:23 *** app/web.1: i.save('%s/%s' % (MEDIA_FOLDER, filename))
Jan 17 22:05:23 *** app/web.1: File "/app/.heroku/python/lib/python2.7/site-packages/PIL/Image.py", line 1459, in save
Jan 17 22:05:23 *** app/web.1: fp = builtins.open(fp, "wb")
Jan 17 22:05:23 *** app/web.1: IOError: [Errno 2] No such file or directory: u'/app/tmp/5cfae63a182ff935fe0fb142_640.jpg'
The relevant lines in my views file are:
#app.route('/edit/', methods = ['GET', 'POST'])
def edit_result():
url = request.args.get('url')
filename = url.split('/')[-1]
i = Image.open(StringIO(requests.get(url).content))
i.save('%s/%s' % (MEDIA_FOLDER, filename))
return render_template("edit.html",
image_filename=filename,
)
Looking at the working git commit vs the next one that broke things, this is all that was commited:
<option value="Default">Choose Font</option> was added to a Jinja2 template, #cache.cached(timeout=50) was removed from a view and "Default": "/ttf-bitstream-vera/Vera.ttf", was added to a dictionary
A couple more notes:
I tried to pull up the image listed in the error message and it
actually does come up on the heroku instance.
I thought maybe I was
confusing the commits and looked over the last few prior to the one I
thought was the working commit, and the next few following the broken
commit. I can't see anything that would cause just heroku to fail,
not my local environment.
I, probably stupidly, continued working on the app on my local environment. I hadn't finished coding logging into the app so I was just getting error 500 on heroku and thought it was a fluke. I'm a dozen commits ahead of the heroku deploy now and a lot has changed including those commits that broke things being removed entirely for a better user experience.
Things are still working perfectly, locally.
Permissions haven't changed on the tmp directory
I'm new to heroku, this is my first app deployed there. Maybe I'm missing something which is why I'm coming to you! Any advice would be appreciated.
*I know heroku doesn't want things like this stored locally and I'm in the process of rewriting the views to use S3 instead...
You can read more about my answer in the comment left by dirn on the first post but essentially my directory was NOT included in my git repo as I thought, so it wasn't on the heroku dyno after it cleared. I added a condition in my views.py to check for the directory and create it if it doesn't exist which fixed the problem:
if not os.path.exists(MEDIA_FOLDER):
os.makedirs(MEDIA_FOLDER)

How to call methods of Model Class in Interactive Console locally?

I have been searching this since yesterday and still nothing. From all that researching this is my understanding so far.
You can access your datastore remotely with remote_api_shell.py
Make sure your path is set correctly in your Environment variable.
And according to my understanding the remote datastore that they were talking about is the datastore in appspot.com and not the local one. I don't want to deploy my app right now and hence I want to just work locally, for now atleast.
I created a model named Usersdb in my app. As someone coming from PHP, MYSQL background I thought GQL would have console environment for us to test the queries. But after some googling I found out that you can manipulate local datastore from the interactive console that's in
http://localhost:8081/_ah/admin/interactive
From the post Google App Engine GQL query on localhost I got the idea of performing GqlQuery in the interactive console while in localhost which goes something like this:-
from google.appengine.ext import db
q = db.GqlQuery("SELECT * FROM Userdb where username = 'random_user'")
print q.get().username
But what I really wanted to do was perform method calls like get_by_id() and get_by_key_name() and such in my interactive console without having to test on my app. Like:-
print Userdb.get_by_id(12)
How can I get those running? Do I have to import my python file to the interactive console? I tried doing that too but it crashed app engine. I'm just starting out on app engine. Forgive me if this is a completely stupid question.
You should import the model class that you wrote into your session in the interactive console. For example, if you have a file named model.py in your application, which contains your Userdb class you could write the following in the interactive console:
import model
print model.Userdb.get_by_id(12)

Categories

Resources