3
votes

I'm trying to deploy a fairly simple Flask app with a couple views using Google's App Engine. As far as I know, nothing is wrong with the Flask code itself - it runs just fine on a local port. The problem, however, occurs when I cd into the project directory and run "gcloud app deploy." The app deploys without any errors and returns a 502 code when I go into the .appspot.com domain reserved for the app.

I was a little confused, so I ran "gcloud app logs tail." This is the message that is displayed on repeat in the logs:

* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[2019-01-06 23:55:00 +0000] [240] [INFO] Starting gunicorn 19.9.0
[2019-01-06 23:55:00 +0000] [240] [ERROR] Connection in use: ('', 8081)
[2019-01-06 23:55:00 +0000] [240] [ERROR] Retrying in 1 second.
[2019-01-07 23:55:01 +0000] [240] [ERROR] Can't connect to ('', 8081)

Running "netstat -ano" shows no processes that are actually using port 8081. Running netstat and killing whatever process was using the occupied port was suggested in a post with a problem similar to mine. However, there's literally nothing using 8081 here.

"main" is main.py, a file that creates an app, registers all the blueprints, and runs the app with

app.run(host="0.0.0.0", debug=True)

I'm totally new to using the App Engine - am I missing something? Anyone know where to start?

2
Can you share your app.yaml file? (Make sure that there is no sensitive information). As well, you mentioned that you are using the netstat command in your machine to see which ports were being in use, however when the application is deployed, it is not running locally in your computer, but on an instance in App Engine, so unless you can access the machine running your app (I don't believe it's possible to do so), and run the command in its environment, this command doesn't help at all.Joan Grau Noël
I keep getting an error when trying to edit in the app.yaml file in the original post, so here's the code, if you can make it out. I don't think I changed much from the Google App Engine tutorial: runtime: python37 entrypoint: gunicorn -b :$PORT main:app handlers: - url: /static static_dir: static - url: .* script: autoJustas Stankevičius

2 Answers

1
votes

I have tested the issue on my side, and I have been able to reproduce the error message.

The problem is that in your main.py file, you are calling the line app.run(host="0.0.0.0", debug=True) from the root of your script.

You should have this line inside your main.py, as shown here:

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

What this conditioning does, is to make sure that its nested statements are only executed when the file is directly executed, i.e. running python main.py in your local computer.

What the app.run statement does, is to execute the Flask application in a local development server, that is created at the host direction (in your case "0.0.0.0", which in flask means that the application is exposed externally), and in the specific port (which defaults to 5000).

You are using gunicorn as a entrypoint to the application, and you are pointing it to the app object inside the main script, as defined in the app.yaml file. But since the flask app is already running in another server, created by the app.run statement, gunicorn is not able to point to the application, hence it's not able to serve requests.

In short, don't execute app.run when deploying to App Engine.

1
votes

Google App Engine uses the gunicorn webprocess to serve the app, so app.run() in the main file only runs locally and does not change anything to the webserver, unless you specify an entrypoint.

Are you specifying an entrypoint in your app.yaml? Try adding the following line to your app.yaml and adding gunicorn in your requirements.txt.

entrypoint: gunicorn -b :$PORT main:app