Gracefully Reload flask app systemd service - python

I'm using nginx and gunicorn to serve the Flask app. The problem is when I make any change in APIs and to have those changes into effect I'm doing
sudo service nginx reload && sudo systemctl restart myapp
But sometimes it gives 502 errors. My assumption is that there are already ongoing API requests served by the server while I'm doing systemctl restart myapp.
What is the best way to reload the service without any downtime and also the existing API calls shouldn't be affected as well?

Related

GUNICORN FastAPI Deployment with Proxy

I am currently setting up a FastAPI application to run on an AWS RockyLinux 8 instance with a gunicorn deployment. Most of the documentation that I have read recommends NGINX as a proxy server.
I currently have an Apache sever running on this instance and would rather not install another HTTP daemon if I can help it.
My question is: Why is a proxy HTTP server needed at all? gunicorn seems to run just fine without a proxy HTTP server simply by specifying a port other than 80. Is this a security recommendation or am I missing something?

502 Bad Gateway after reloading supervisor

My Flask web application runs using nginx and gunicorn. I use supervisor to let my application run in the background. I always updated my files using Windows Power Shell and the command SCP. After i moved the new edited files, which are already existing on my Ubuntu server, to the server, i use the command sudo supervisorctl reload to restart the flask app to see the changes. But this time the flask app did not start and i only get 502 Bad Gateway. It does not matter how many times i reload the supervisor or restart nginx, i only get the error code 502.
The issue was a not installed module and a typing error in a configuration file.

After deploy django app in digitalocean i can't see my changes in server?

I was deploy my webapp in digitalocean which is written by django. Now i change and add some file in it. I change the template folder and also add some photos in static folder. After change i pull the changes from github in my server app directory. But i can't see the changes. I stop the nginx server. I use this command
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
then again i start but no changes. what should i do?
In order to reload Django app restart your gunicorn service.
sudo systemctl restart gunicorn

Flask app run with Gunicorn async workers appears less efficient than dev server

I've got a web app developed in Flask. The setup is simple. The app is running on Gunicorn. All requests are proxied through the nginx. The Flask app itself makes HTTP requests to external API. The HTTP requests from the flask app to the external API are initiated by AJAX calls from the javascript code in the frontend. The external API returns data in JSON format to the Flask app and the back to the frontend.
The problem is that when I run this app in development mode with the option multithreaded = True I can see that the JSON data get returned asynchronously to the server and I can see the result on the frontend page very quickly.
However, when I try to run the app in production mode with nginx and gunicorn I see that the JSON data get returned sequentially - quit slowly, one by one. It seems that due to some reason the HTTP requests to the external API get blocked.
I use supervisor on linux Ubuntu Server 16.04. This is how I start gunicorn through supervisor:
command = /path/to/project/env/bin/gunicorn -k gevent --worker-connections 1000 wsgi:app -b localhost:8500
It seems that gunicorn does not handle the requests asynchronously, although it should.
As experiment I ran the Flask app using it's built in wsgi server (NOT gunicorn) in development mode, with debug=True and multithreaded=True. All requests were still proxied through the nginx. The JSON data returned much quicker, i.e. asynchronously (seems the calls did not block).
I read gunicorn's documentation. It says if I need to make calls to external API, then I should use async workers. I use them but it doesn't work.
All the caching stuff was taken into account. I may assume that I don't use any cache. I cleared it all when I checked the server setups.
What am I missing? How can I make gunicorn run as expected?
Thanks.
I actually solved this problem quite quickly and forgot to post the answer right away. The reason why the gunicorn server did not process the requests acynchronously as I would expect was very simple and stupid. Since I was managing gunicorn through the supervisor after I had changed the config to:
command = /path/to/project/env/bin/gunicorn -k gevent --worker-connections 1000 wsgi:app -b localhost:8500
I forgot to run:
sudo supervisorctl reread
sudo supervisorctl update
It's simple but not obvious though. My mistake was that I expected the config to update automatically after I restart my app on gunicorn using this command:
sudo supervisorctl restart my_app
Yes it restart the app, but not the config of gunicorn.

How to stop flask app.run()?

I made a flask app following flask's tutorial. After python flaskApp.py, how can I stop the app? I pressed ctrl + c in the terminal but I can still access the app through the browser. I'm wondering how to stop the app? Thanks.
I even rebooted the vps. After the vps is restated, the app still is running!
CTRL+C is the right way to quit the app, I do not think that you can visit the url after CTRL+C. In my environment it works well.
What is the terminal output after CTRL+C? Maybe you can add some details.
You can try to visit the url by curl to test if browser cache or anything related with browser cause this problem.
Since you are using apache so in order to stop your app, you have to disable it by deleting .conf file from '/etc/apache2/sites-enabled/' folder and then restart the apache server. This will surely destroy your current running session.
$ cd /etc/apache2/sites-enabled/
$ sudo rm conf_filename.conf
$ sudo service apache2 restart
Try it and your site will be down. To enable it again, copy paste your file to '/etc/apache2/sites-available/' and run the following commands to enable it again.
$ sudo a2ensite conf_filename.conf
$ sudo service apache2 restart
Now your site will be live again.
Have you tried pkill python?
WARNING: do not do so before consulting your system admin if are sharing a server with others.

Categories