Skip to content

Commit

Permalink
If no pidfile is specified, use a random tmp file.
Browse files Browse the repository at this point in the history
  • Loading branch information
davisp committed May 12, 2010
1 parent d2561ae commit db5bd53
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions gunicorn/pidfile.py
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -66,4 +77,4 @@ def validate(self):
except IOError, e:
if e[0] == errno.ENOENT:
return
raise
raise

0 comments on commit db5bd53

Please sign in to comment.