Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better way to restart/reload Gunicorn (via Upstart) after 'git pull'ing my Django projects

Im looking for something better than sudo restart projectname every time I issue a git pull origin master, which pulls down my latest changes to a Django project. This restart command, I believe, is related to Upstart, which I use to start/top my Gunicorn server process.

This restart causes a brief outage. Users hitting the web server (nginx) will get a 500, because Gunicorn is still restarting. In fact, it seems to restart instantly, but it takes a few seconds for pages to load.

Any ideas on how to make this seamless? Ideally, I'd like to issue my git pull and Gunicorn reloads automatically.

like image 652
Ben Keating Avatar asked Mar 27 '12 00:03

Ben Keating


People also ask

How do I restart my Django Gunicorn?

If you update your Django application, you can restart the Gunicorn process to pick up the changes by typing: sudo systemctl restart gunicorn.

How do you reload a Gunicorn server?

If you are running gunicorn on a port rather than a socket, you can find the process id (pid) of gunicorn using fuser command. Then force gunicorn to reload the code by sending a HUP signal. The command fuser 8000/tcp will list the process ids of all processes using tcp port 8000.


2 Answers

You can tell Gunicorn to reload gracefully using the HUP signal like so:

kill -HUP <pid> 

(see the FAQ for details)

I use Supervisor to control my Gunicorn server, which allows me to use this (slightly hacky) way of reloading Gunicorn after a deploy:

supervisorctl status gunicorn | sed "s/.*[pid ]\([0-9]\+\)\,.*/\1/" | xargs kill -HUP 

You could obviously achieve something similar with pidof, or ps.

This is actually run from a Fabric script, so I don't even have to logon to the server at all.

like image 165
Rob Golding Avatar answered Sep 24 '22 09:09

Rob Golding


For those not using supervisord: what Rob said, it works with ps as well,

ps aux |grep gunicorn |grep projectname | awk '{ print $2 }' |xargs kill -HUP 
like image 38
littlegreen Avatar answered Sep 24 '22 09:09

littlegreen