R
Railway2y ago
rapid

Flask Timing Out

I'm getting the application failed to respond issue but on localhost it works fine.
180 Replies
Percy
Percy2y ago
Project ID: N/A
Percy
Percy2y ago
⚠️ experimental feature
jackson
jackson2y ago
need more info
rapid
rapid2y ago
ok so basically it sends a request and gets the json from it and then returns it as a svg that uses the json it works on localhost but it takes a second to load, it might be because Im literally doing it in flask
jackson
jackson2y ago
probably going to need to see some code, not much i can do just based on that
rapid
rapid2y ago
Its a lot but this is the route
@app.route("/picture/<id>")
def discord_presence(id):
try:
id = int(id)
except ValueError:
return jsonify({"error": "Please provide a valid ID"})
url = requests.get(f"https://presence.dark.bio/{id}")
try:
if url.status_code == 200:
response = url.json()
svg = render_presence(response)
return svg, 200, {'Content-Type': 'image/svg+xml'}
else:
return jsonify({"error": "User not found, join discord.gg/darkbio"})
except Exception as e:
print(e)
return(e)
@app.route("/picture/<id>")
def discord_presence(id):
try:
id = int(id)
except ValueError:
return jsonify({"error": "Please provide a valid ID"})
url = requests.get(f"https://presence.dark.bio/{id}")
try:
if url.status_code == 200:
response = url.json()
svg = render_presence(response)
return svg, 200, {'Content-Type': 'image/svg+xml'}
else:
return jsonify({"error": "User not found, join discord.gg/darkbio"})
except Exception as e:
print(e)
return(e)
jackson
jackson2y ago
server invite link lmao anyways
rapid
rapid2y ago
yea 😭 then it goes to a def render_presence(response) and thats really big 137 lines long
jackson
jackson2y ago
how is this deployed? also how do you initialize your flask app
rapid
rapid2y ago
wdym how is it deployed? I mean its through a github repo initialize as in run?
jackson
jackson2y ago
yeah but like do you have a dockerfile, railway.json, etc
rapid
rapid2y ago
nah
jackson
jackson2y ago
yeah how do you configure it in main.py
rapid
rapid2y ago
http_server = WSGIServer(('0.0.0.0', int(os.environ.get('PORT', 8080))), app)
http_server.serve_forever()
http_server = WSGIServer(('0.0.0.0', int(os.environ.get('PORT', 8080))), app)
http_server.serve_forever()
jackson
jackson2y ago
also are there any logs when it fails to return a response
rapid
rapid2y ago
to run it no
rapid
rapid2y ago
rapid
rapid2y ago
js this but it doesnt actually render it
rapid
rapid2y ago
jackson
jackson2y ago
does it sometimes take >60s to return a response? i see 99.xxx and i suspect that might be return time in seconds
rapid
rapid2y ago
it doesnt even return it, it just loads until it times out what
jackson
jackson2y ago
in the logs
rapid
rapid2y ago
yeah it takes more than 60 seconds to return a response in the logs I believe But its quick on localhost
jackson
jackson2y ago
yeah so if it does that railway will drop the request essentially
rapid
rapid2y ago
oh ok, so what can I do to fix it
jackson
jackson2y ago
i'd first try adding some logging around svg = render_presence(response), assuming that it actually runs and works fine there may be issues with returning a huge image in a response body
rapid
rapid2y ago
ok let me try
rapid
rapid2y ago
so I added some logging, well a lot but its timing out with cloudfalre
rapid
rapid2y ago
now also
rapid
rapid2y ago
rapid
rapid2y ago
nothing is being printed to console @jackson can someone help?
Adam
Adam2y ago
This server’s help is provided by the community, please be patient
Brody
Brody2y ago
go back to the basics, add a simple /health route and have it return a 200 code first
Adam
Adam2y ago
Before doing that I would check to see if your app is working with a Railway provided domain rather than a custom one There can be a variety of issues with custom domains that aren’t related to your app’s code but the connection process
rapid
rapid2y ago
doesnt work with custom, or provided mbmb different routes work
Adam
Adam2y ago
Oh you’re the dark.bio guy, I remember checking out your site wasn’t it working a couple days ago?
rapid
rapid2y ago
so rn the api itself works so https://presence.dark.bio/745421881594871939 but if you add /picture/{id} then thats what breaks it renders a svg looks like this
rapid
rapid2y ago
Adam
Adam2y ago
hmm gotcha where are you doing this rendering?
rapid
rapid2y ago
so I pass the response from the api to a def like ths
@app.route("/picture/<id>")
def discord_presence(id):
try:
id = int(id)
except ValueError:
return jsonify({"error": "Please provide a valid ID"})
url = requests.get(f"https://presence.dark.bio/{id}")
try:
if url.status_code == 200:
response = url.json()
print("Before try")
try:
print("got into try")
svg = render_presence(response)
except Exception as e:
print(e)
print("after try before return")
return svg, 200, {'Content-Type': 'image/svg+xml'}
print("after return")
else:
return jsonify({"error": "User not found, join discord.gg/darkbio"})
except Exception as e:
print(e)
return(e)
@app.route("/picture/<id>")
def discord_presence(id):
try:
id = int(id)
except ValueError:
return jsonify({"error": "Please provide a valid ID"})
url = requests.get(f"https://presence.dark.bio/{id}")
try:
if url.status_code == 200:
response = url.json()
print("Before try")
try:
print("got into try")
svg = render_presence(response)
except Exception as e:
print(e)
print("after try before return")
return svg, 200, {'Content-Type': 'image/svg+xml'}
print("after return")
else:
return jsonify({"error": "User not found, join discord.gg/darkbio"})
except Exception as e:
print(e)
return(e)
none of the prints have been printing it might be overloading or something but.. it does seem to work on localhost
Adam
Adam2y ago
For one I see a hell of a lot of bad practice But i’ll dig into it, what’s the repo link?
rapid
rapid2y ago
yeah I've never done this kinda shit its private, I can invite you to it
Adam
Adam2y ago
Gotcha, my username is ItsGoldeneyes
rapid
rapid2y ago
ok, btw be ready that it is NOT built well LMAO
Adam
Adam2y ago
Yeah no worries lol chances are that’s probably the issue though
rapid
rapid2y ago
shrug you want the repo link
Adam
Adam2y ago
Nah I got it from the email invite
rapid
rapid2y ago
oh btw some stuff might be extra weird because I used chatgpt to try and help make it faster
Adam
Adam2y ago
No worries, it's readable at least
rapid
rapid2y ago
LOL
Adam
Adam2y ago
What's an id I can use to test?
rapid
rapid2y ago
mine 745421881594871939 or if you want to use your own, you have to join the server oh btw the constants is for the discord commands that are for something else, hence why they're commented out
Adam
Adam2y ago
I think the reason why it's not returning anything correctly may be because of all the try except checks you're doing Those are really bad practice, I'll push a pr to correct that section of the code
rapid
rapid2y ago
ok, let me know then how come its not even printing?
Adam
Adam2y ago
I'm only seeing one print statement
rapid
rapid2y ago
really?? holdon
rapid
rapid2y ago
Adam
Adam2y ago
under get_presence yes
rapid
rapid2y ago
oh
Adam
Adam2y ago
Oh whoops I was changing the wrong function lmao
rapid
rapid2y ago
oh.. is that where he meant logging
Adam
Adam2y ago
Lots of try except everywhere
rapid
rapid2y ago
I think they're only in the discord commands theres none in the render_presence but theres 2 in the routes 3*
Adam
Adam2y ago
Pushed a commit that should help you out Whoops meant to make a pr but I just merged into main
rapid
rapid2y ago
thats fine, lets see if it works automatic server error https://presence.dark.bio/picture/745421881594871939 ima look in the deploy logs there are literally 0 logs oh nvm user not found but im in that server.. nothing got returned in jsonify tho
Adam
Adam2y ago
pushed a commit to print the request so you can see what's in it
rapid
rapid2y ago
alr, so its because url.status_code != 200 thats whats happening it has something to do with the api.. https://presence.dark.bio/745421881594871939 isnt returning anything except return("user not found")
Adam
Adam2y ago
I haven't used the Discord api in a while, is the guild correct?
rapid
rapid2y ago
it cant find me in my own guild tf
Adam
Adam2y ago
Maybe it should be a string instead of an int?
rapid
rapid2y ago
but its printing my guild name what should
Adam
Adam2y ago
The guild id
rapid
rapid2y ago
thats how its supposed to be because it does print my guild name let me make it print the id ok so the guild_id is right and it can find the guild but it doesnt seem to find the id let me try making it int(user_id)
Adam
Adam2y ago
Oh yeah that's my mistake
rapid
rapid2y ago
ok so its supposed to be member = guild.get_member(int(user_id))
Adam
Adam2y ago
does it have to be an int?
rapid
rapid2y ago
yeah, doesn't seem to work if its not.. let me try str
Adam
Adam2y ago
It was str before
rapid
rapid2y ago
yea user not found fr?
Adam
Adam2y ago
I pushed a fix
rapid
rapid2y ago
iht ight
rapid
rapid2y ago
I think below it also has to be int like where the picture route is
Adam
Adam2y ago
rapid
rapid2y ago
ha, works I think yeah add /picture
Adam
Adam2y ago
It's thinking
rapid
rapid2y ago
you can now see what I mean by loading for a long time 😭 ill lyk what logs appear
Adam
Adam2y ago
If it's going to take a while to render then your best option would be to rework it so that request is sent to another service that just renders the images And have your current service display a "waiting for image" message until it recieves a response
rapid
rapid2y ago
hmm, what do you mean so I could add the presence api to dark.bio and then make that entire application just be the image idk if it would wrok though
rapid
rapid2y ago
rapid
rapid2y ago
so it cant find my id so let me try sometihng oh
rapid
rapid2y ago
rapid
rapid2y ago
its already int weird.. btw it works on localhost so wait nvm, stopped working..
Adam
Adam2y ago
No, just make a third service that only processes render requests
rapid
rapid2y ago
what do you mean
Adam
Adam2y ago
When the user requests a /picture then they're given a screen that displays that the server is waiting for the render
rapid
rapid2y ago
idk im confused
Adam
Adam2y ago
Then once that request is finished the page is updated with the image that was created
rapid
rapid2y ago
how would this be
Adam
Adam2y ago
wdym
rapid
rapid2y ago
ok so the first service is the api
Adam
Adam2y ago
It would be a separate service that only creates the image
rapid
rapid2y ago
in a route?
Adam
Adam2y ago
the presence API sends a request to that service to create an image and the service returns that image when it's done processing
rapid
rapid2y ago
im sorry im kinda slow ok so
Adam
Adam2y ago
In the meantime the user is served a page that tells them that the rendering is in progress
rapid
rapid2y ago
yea
Adam
Adam2y ago
Once the rendering is done, the user is shown the image
Brody
Brody2y ago
sorry to chime in here, but how long does picture/<id> take to return when running this on your local machine?
rapid
rapid2y ago
when it works, maybe a second or two im getting status 203 I mean 503
rapid
rapid2y ago
rapid
rapid2y ago
it took around 10 seconds to get user not found though on my machine
Adam
Adam2y ago
What service is handling the user not found request?
rapid
rapid2y ago
/picture/<id> right because thats where the print for the response is
rapid
rapid2y ago
rapid
rapid2y ago
rapid
rapid2y ago
that is getting hit thats good though
Adam
Adam2y ago
Pretty sure you're getting rate limited
rapid
rapid2y ago
idk
Adam
Adam2y ago
By service I mean what's handling the presence.dark.bio/id call Because that's what's returning 503
rapid
rapid2y ago
I feel so stupid uhmm
Adam
Adam2y ago
It's all in the same service right? The only API you're reaching out to is the Discord API?
rapid
rapid2y ago
yes yes
Adam
Adam2y ago
Definitely think you're getting rate limited
rapid
rapid2y ago
actually no
Adam
Adam2y ago
At least with those 503s
rapid
rapid2y ago
I don't think I access the discord api at all..
Adam
Adam2y ago
You 100% do
rapid
rapid2y ago
well because I use stuff from discordpy like the bot does it if yk what I mean
Adam
Adam2y ago
That's just a wrapper for the discord api
rapid
rapid2y ago
oh lmao 💀 so would a discord gateway better better?
Adam
Adam2y ago
No, you'll just have to wait a bit to do more testing
rapid
rapid2y ago
do you know the rate
Adam
Adam2y ago
nah I don't
Brody
Brody2y ago
just checked, and there's nothing wrong with API calls taking up to 5s https://frantic-north-production-0ace.up.railway.app/wait/5s
rapid
rapid2y ago
so do yk wht the issue is
Brody
Brody2y ago
there are too many possibilities, if I were to debug id need the code
rapid
rapid2y ago
if you'd like I can add you to the repositry
Brody
Brody2y ago
sure
rapid
rapid2y ago
whats ur github
Brody
Brody2y ago
overrnet
rapid
rapid2y ago
added, also just be prepared that its not very... "good" code
Brody
Brody2y ago
alright ill see what i can do and get back to you, dinner is soon so be paitent please
rapid
rapid2y ago
yeah no problem, take your time
Brody
Brody2y ago
i have to install python first
rapid
rapid2y ago
oh lol
Brody
Brody2y ago
haven't done python in a while, but i used to do flask stuff all the time
rapid
rapid2y ago
shrug
Brody
Brody2y ago
Brody
Brody2y ago
this is a pretty cool little api, but theres room for improvement
rapid
rapid2y ago
for sure, like badges how's it going? have you found out anything? feel free to push a commit, its connected to the domain
Brody
Brody2y ago
yeah, your code as is, "/picture/<id>" doesn't work, but "/<id>" does like you said, but i did some things and now it does i dont have git
rapid
rapid2y ago
oh lol oh fr?
Brody
Brody2y ago
yeah, let me work some things out, and then ill give you a zip file for new code
rapid
rapid2y ago
sure also, this is asking for a lot but if you can lmk what things could be faster, it would be great
Brody
Brody2y ago
yeah it could be written in Go lol
rapid
rapid2y ago
oh that’s a smart idea idk go
Brody
Brody2y ago
im only partially joking, everyone is free to use whatever langue they like
rapid
rapid2y ago
for sure, but i’ve heard go is faster
Brody
Brody2y ago
easier than python 😉
rapid
rapid2y ago
sure idk i feel like python makes the most since tho yk
Brody
Brody2y ago
but sorry i dont know too much about optimising python code, all i can do is make things "work"
rapid
rapid2y ago
oh yeah np so what was wrong with it
Brody
Brody2y ago
execution was hanging forever one that web request url = requests.get(f"https://presence.dark.bio/{id}")
rapid
rapid2y ago
yea
Brody
Brody2y ago
as i thought from the very begining
rapid
rapid2y ago
hh huh
Brody
Brody2y ago
but why would you want to make a loopback type request when the function that handles that request is sitting right above it
rapid
rapid2y ago
idk I was just trying to make it work lmao
Brody
Brody2y ago
just request the function directly okay fair lol hold on for a bit and let me get this sorted out
rapid
rapid2y ago
ok !!!! :))))))
Brody
Brody2y ago
try it locally
rapid
rapid2y ago
sus download fr let me try this rq
Brody
Brody2y ago
yeah ik, but i have no inclination to download the git cli or even use github to begin with
rapid
rapid2y ago
yea np I was joking 😦
Brody
Brody2y ago
alright dinner, ttyl
rapid
rapid2y ago
ttyl its so fastomg
Brody
Brody2y ago
Yeah it's faster when it doesn't have to make an http request to an old version of the API running on railway
rapid
rapid2y ago
oh.. thats interesting Thanks for helping today!
Brody
Brody2y ago
no problem, happy coding!
Want results from more Discord servers?
Add your server