⬆️ ⬇️

Smooth restart of FastCGI processes - django_graceful

Of all the ways to deploy django projects, my favorite is FastCGI. It is supported by the majority of web servers, allows you to clearly distinguish access rights and has many other advantages.



However, in django its implementation is not without flaws. To run a FastCGI server, you need to run a “./manage.py runfcgi” with a rather large number of parameters that, if you can remember, you don’t want to write with your hands each time. And if this happens in the context of updating the project code on the combat server, then there are even more teams. You have to write different wrappers for launching and restarting, which clog up the project.



By the way, the restart, which we perform to update the code, is associated with another problem. There is no smooth restart of the FastCGI server in django. You can only kill the old process, and then start a new one. This does not happen instantly, so that in the time interval between the death of the old process and the opening of the port by the new process, visitors see an error 502, which, of course, does not increase their confidence in your site. We have to invent various tricks and complicate the wrapper-scripts.

')

From one of these scripts, the rather universal django_graceful application has grown that solves all these problems. His idea is simple: FastCGI settings are stored in the settings.py file, and simple commands from manage.py are used to manage FastCGI processes. These commands support several concurrently running instances of the FastCGI server and allow switching the web server between them using a symbolic link to the unix-socket file, that is, without restarting the web server.



This circumstance allows us to crank up such a focus: when we need to update the code, we restart the FastCGI process with one command, to which the web server is not currently accessing, and then, when it is ready to serve the requests again, switch the symbolic link to it. Clients connected before switching continue to be served by the first FastCGI server, and the new ones by the second. This action does not even need server administrator privileges. Tests have shown that a break in service will not occur even with a very dense stream of requests. By the way, the application was first used on a project on foreign real estate Tranio.Ru and still works fine there.



How to install it?

  1. The application itself can be downloaded and installed with the easy_install django_graceful command.
  2. Then the application name “django_graceful” needs to be added to your project’s INSTALLED_APPS list.
  3. In the settings.py file you need to add another option - GRACEFUL_STATEDIR. It should contain the absolute path to the folder where FastCGI processes .pid and .socket files will be stored.
  4. You can also add the GRACEFUL_OPTIONS dictionary in settings.py, specifying additional options for the “./manage.py runfcgi” command.
  5. A symbolic link “fastcgi.socket” will be created in the GRACEFUL_STATEDIR folder, the path to which you need to register in the web server configuration. For example, "FastCGI_pass unix: /home/web/project/var/run/fastcgi.socket" in nginx.


How to use



We list the typical tasks, the implementation of which simplifies django_graceful.



The initial launch and support of the FastCGI server in a running state:


./manage.py keepalive — a command that checks whether at least one FastCGI process is running and whether a link points to it. If no process is running, it starts the first one. If the link points to an unplayed process, the command switches it to the socket of the running process. Otherwise, she does nothing.



I like to add this command to the crontab to run once a minute. It will raise FastCGI after an unexpected crash or unplanned reboot of the server.



Smooth restart of FastCGI after updating the code:


./manage.py update — A command that performs the focus described above with restarting the inactive FastCGI process and setting a link to it.



Full list of commands





That's all! This application lives at github.com/andreiko/django_graceful

Convenient to you and high uptime :)

Source: https://habr.com/ru/post/129006/



All Articles