Commits

Luke Plant  committed 3e2a499

Improvements to stopping/starting the gunicorn process

  • Participants
  • Parent commits 0cb0a52

Comments (0)

Files changed (2)

 - you are using Django 1.4 or greater
 
 - you have your development environment already setup, including
-  having 'fabric' installed.
+  having 'fabric' installed. You also need 'psutils' installed,
+  and added to your requirements.txt for production.
 
 - your server is basically 'provisioned' i.e.
 
   * Django
   * south
   * gunicorn
+  * psutils
 
 - use of Mercurial for source control, requiring hg to be installed on
   your server. (This could be easily modified to use git).
 from fabric.decorators import runs_once
 from fabric.contrib.files import exists
 from fabric.context_managers import cd, lcd, settings, hide
+import psutil
 
 # CHANGEME:
 
     Stop the webserver that is running the Django instance
     """
     run("kill $(cat %s)" % GUNICORN_PIDFILE)
+    run("rm %s" % GUNICORN_PIDFILE)
 
 
 def _webserver_command():
     run(_webserver_command())
 
 
+def _is_webserver_running():
+    try:
+        pid = int(open(GUNICORN_PIDFILE).read().strip())
+    except (IOError, OSError):
+        return False
+    for ps in psutil.process_iter():
+        if (ps.pid == pid and
+            any('gunicorn' in c for c in ps.cmdline)
+            and ps.username == USER):
+            return True
+    return False
+
+
 @task
 def local_webserver_start():
     """
     Starts the webserver that is running the Django instance, on the local machine
     """
-    local(_webserver_command())
+    if not _is_webserver_running():
+        local(_webserver_command())
 
 
 @task