Forums

Error code: 504-loadbalancer

I'm getting this when I try to run the newly-updated version of my web app (which consists of a button that, when clicked, modifies a Google spreadsheet and sends some emails via Gmail). I'm having a hard time figuring out why this might be.

I tried to solve this by moving in the asychronous direction via batching, but I'm not positive I did this correctly or identified the correct solution.

Possibly relevant: due to Google rate limit issues, I had thrown in "time.sleep(1)", which worked until one time it didn't-- so I updated it to "time.sleep(2)". (I did this as part of a larger update, so I don't know whether or not it worked independently of other changes.) I wonder if that might be part of the issue, and if so, I wonder whether the batching might help enough that I can drop back down to 1. But I'm not at all sure that the two are connected.

Happy to share any relevant portions of my code... just not sure what to share yet!

Thanks for any help!

504 is a timeout response code. It could be that the request to the webapp (the button click) is taking too long to respond. When you click the button, how long before you get the 504 response?

Seems to be about five minutes. Not sure what would be delaying the response; it worked fine in my earlier version of the app (which modified the spreadsheet but didn't send emails).

In case it's helpful, here's the button-related bit of my code:

def button_click(request):
    if request.method == 'POST':
        # Update the spreadsheet
        update_spreadsheet()

        # Return a response indicating the button was clicked successfully
        return HttpResponse("Button clicked successfully")

    # For GET requests, render the button.html template
    return render(request, 'button.html')

We have a help page to help you to identify what is making your web app slow here https://help.pythonanywhere.com/pages/MySiteIsSlow/

Thanks! Based on that, I have a guess about what the problem is:

The old version of my app only used the Google Sheets API (via a service account), but the new version also uses the Gmail API (via OAuth). My server log includes the below:

2023-07-28 23:32:06 Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=

(And yes, the URL continues; I cut off the rest.)

When I ran the script locally, it opened a browser window for me. If I understand right (and I'm not sure I do), I won't be able to achieve the same effect via the hosted version of the app. Is that correct?

If so... is there a way around this, so I can get the app to work?

I think the best shot would be to check with the Google documentation -- I haven't used that for a while but there used to be an option of uploading a pickled creds file on the server which enabled running an app in noninteractive mode.

Thanks!

Just to confirm: is it the case that I can't use OAuth via Python Anywhere and will need to find an alternate approach?

I'm entirely unfamiliar with pickled creds and having trouble figuring out what Google documentation is relevant. Does this seem right? https://developers.google.com/gmail/postmaster/quickstart/python

Edited to add one last question: just to confirm: if I revise the local version of my script that I'm using for testing purposes, and by doing that, I have a token.pickle file that I can upload to Python Anywhere... is that something that can work, or would I need to find some way to generate the pickle file within a Python Anywhere environment? I'm just wary of making changes that will work locally but not work in the hosted version.

If I remember correctly, I created the pickle file locally, then uploaded it on PA and it worked. But I can't guarantee 100% that Google hasn't changed something else that would affect the behavior now. I guess the general answer to your issue would be: PythonAnywhere is not blocking Google API, but it's a headless environment, so any form of interactive authorization that needs a browser would not work.

It worked! Thanks!!

Excellent, glad we could help!