From db5bd53c4edfbeb683ef48b16107b95b48dc0fc0 Mon Sep 17 00:00:00 2001 From: "Paul J. Davis" Date: Tue, 11 May 2010 23:10:52 -0400 Subject: [PATCH] If no pidfile is specified, use a random tmp file. --- gunicorn/pidfile.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/gunicorn/pidfile.py b/gunicorn/pidfile.py index 10756999e..a8ba4c70f 100644 --- a/gunicorn/pidfile.py +++ b/gunicorn/pidfile.py @@ -11,9 +11,14 @@ class Pidfile(object): - - def __init__(self, path): - self.path = path + """\ + Manage a PID file. If a specific name is provided + it and '"%s.oldpid" % name' will be used. Otherwise + we create a temp file using os.mkstemp. + """ + + def __init__(self, fname): + self.fname = fname self.pid = None def create(self, pid): @@ -22,14 +27,17 @@ def create(self, pid): if oldpid == os.getpid(): return raise RuntimeError("Already running on PID %s " \ - "(or pid file '%s' is stale)" % (os.getpid(), self.path)) + "(or pid file '%s' is stale)" % (os.getpid(), self.fname)) self.pid = pid - # write pidfile - fd, fname = tempfile.mkstemp(dir=os.path.dirname(self.path)) + # Write pidfile + fd, fname = tempfile.mkstemp() os.write(fd, "%s\n" % self.pid) - os.rename(fname, self.path) + if self.fname: + os.rename(fname, self.fname) + else: + self.fname = fname os.close(fd) def rename(self, path): @@ -50,11 +58,14 @@ def unlink(self): def validate(self): """ Validate pidfile and make it stale if needed""" + if not self.fname: + return try: - with open(self.path, "r") as f: + with open(self.fname, "r") as f: wpid = int(f.read() or 0) - if wpid <= 0: return None + if wpid <= 0: + return try: os.kill(wpid, 0) @@ -66,4 +77,4 @@ def validate(self): except IOError, e: if e[0] == errno.ENOENT: return - raise \ No newline at end of file + raise