Back to Home

Smooth restart of FastCGI processes - django_graceful

Of all the ways to deploy django projects · my favorite is FastCGI. It is supported by most web servers · allows you to clearly distinguish between access rights and has a lot of other ...

Smooth restart of FastCGI processes - django_graceful

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

    However, in django its implementation is not without flaws. To start the FastCGI server, you need to execute "./manage.py runfcgi" with a rather large number of parameters, which, if you can remember, you definitely won’t want to write each time with your hands. And if this happens in the context of updating the project code on the combat server, then there will be even more teams. You have to write various wrappers to start and restart, which clog the project.

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

    From one of these scripts, a fairly universal django_graceful application has grown, which 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 control FastCGI processes. These commands support several parallel running FastCGI server instances and allow you to switch the web server between them using a symbolic link to the unix-socket file, that is, without restarting the web server.

    Just this circumstance allows us to crank up this trick: when we need to update the code, we restart the FastCGI process with one command, which the web server is not currently accessing, and then, when it is ready to service requests again, we switch the symbolic link to it. Clients connected before the switch continue to be served by the first FastCGI server, and new ones by the second. You do not even need server administrator rights for this action. Tests showed that a service interruption 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 great 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 the INSTALLED_APPS list of your project.
    3. In the settings.py file, you need to add one more option - GRACEFUL_STATEDIR. It should contain the absolute path to the folder where the .pid and .socket files of FastCGI processes will be stored.
    4. You can also add the GRACEFUL_OPTIONS dictionary to settings.py by specifying additional options for the "./manage.py runfcgi" command in it.
    5. A symbolic link “fastcgi.socket” will be created in the GRACEFUL_STATEDIR folder, the path to which must be specified in the web server configuration. For example, "FastCGI_pass unix: /home/web/project/var/run/fastcgi.socket" in nginx.

    How to use


    Here are some common tasks that make django_graceful easier.

    Initial launch and support of the FastCGI server in the running state:

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

    I love adding this command to crontab to execute once a minute. It will raise FastCGI after an unexpected crash or unscheduled server reboot.

    Smooth restart of FastCGI after updating the code:

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

    Full list of teams

    • start - starts the selected processes;
    • stop - stops the selected processes;
    • switch - switches a symbolic link to the specified process;
    • keepalive - checks if there is at least one running and active process;
    • restart - restarts the specified processes;
    • status - shows the status of processes;
    • update - restarts an inactive process and sets a link to it;


    That's all! This application lives at github.com/andreiko/django_graceful
    Convenient to you deployment and high uptime :)

    Read Next