mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
If no pidfile is specified, use a random tmp file.
This commit is contained in:
parent
d2561ae681
commit
db5bd53c4e
@ -11,9 +11,14 @@ import tempfile
|
|||||||
|
|
||||||
|
|
||||||
class Pidfile(object):
|
class Pidfile(object):
|
||||||
|
"""\
|
||||||
|
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, path):
|
def __init__(self, fname):
|
||||||
self.path = path
|
self.fname = fname
|
||||||
self.pid = None
|
self.pid = None
|
||||||
|
|
||||||
def create(self, pid):
|
def create(self, pid):
|
||||||
@ -22,14 +27,17 @@ class Pidfile(object):
|
|||||||
if oldpid == os.getpid():
|
if oldpid == os.getpid():
|
||||||
return
|
return
|
||||||
raise RuntimeError("Already running on PID %s " \
|
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
|
self.pid = pid
|
||||||
|
|
||||||
# write pidfile
|
# Write pidfile
|
||||||
fd, fname = tempfile.mkstemp(dir=os.path.dirname(self.path))
|
fd, fname = tempfile.mkstemp()
|
||||||
os.write(fd, "%s\n" % self.pid)
|
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)
|
os.close(fd)
|
||||||
|
|
||||||
def rename(self, path):
|
def rename(self, path):
|
||||||
@ -50,11 +58,14 @@ class Pidfile(object):
|
|||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
""" Validate pidfile and make it stale if needed"""
|
""" Validate pidfile and make it stale if needed"""
|
||||||
|
if not self.fname:
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
with open(self.path, "r") as f:
|
with open(self.fname, "r") as f:
|
||||||
wpid = int(f.read() or 0)
|
wpid = int(f.read() or 0)
|
||||||
|
|
||||||
if wpid <= 0: return None
|
if wpid <= 0:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.kill(wpid, 0)
|
os.kill(wpid, 0)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user