mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
don't use a property, instead use simple function. spotted by davisp:
"It seems too magical to me"
This commit is contained in:
parent
6d747d7701
commit
cbd91309fe
@ -15,7 +15,7 @@ import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
from gunicorn.pidfile import Pidfile
|
||||
from gunicorn.pidfile import set_pidfile, unlink_pidfile
|
||||
from gunicorn.sock import create_socket
|
||||
from gunicorn.workers.sync import SyncWorker
|
||||
from gunicorn import util
|
||||
@ -44,8 +44,6 @@ class Arbiter(object):
|
||||
if name[:3] == "SIG" and name[3] != "_"
|
||||
)
|
||||
|
||||
pidfile = Pidfile()
|
||||
|
||||
def __init__(self, cfg, app):
|
||||
self.cfg = cfg
|
||||
self.app = app
|
||||
@ -64,7 +62,7 @@ class Arbiter(object):
|
||||
self.log.error("%s" % e)
|
||||
sys.exit(1)
|
||||
|
||||
self._pidfile = None
|
||||
self.pidfile = None
|
||||
self.worker_age = 0
|
||||
self.reexec_pid = 0
|
||||
self.master_name = "Master"
|
||||
@ -94,7 +92,7 @@ class Arbiter(object):
|
||||
self.pid = os.getpid()
|
||||
self.init_signals()
|
||||
self.LISTENER = create_socket(self.cfg)
|
||||
self.pidfile = self.cfg.pidfile
|
||||
self.pidfile = set_pidfile(self.pid, self.cfg.pidfile, self.pidfile)
|
||||
self.log.info("Arbiter booted")
|
||||
self.log.info("Listening at: %s" % self.LISTENER)
|
||||
|
||||
@ -155,13 +153,13 @@ class Arbiter(object):
|
||||
traceback.format_exc())
|
||||
self.stop(False)
|
||||
if self.pidfile:
|
||||
del self.pidfile
|
||||
unlink_pidfile(self.pid, self.pidfile)
|
||||
sys.exit(-1)
|
||||
|
||||
self.stop()
|
||||
self.log.info("Shutting down: %s" % self.master_name)
|
||||
if self.pidfile:
|
||||
del self.pidfile
|
||||
unlink_pidfile(self.pid, self.pidfile)
|
||||
sys.exit(0)
|
||||
|
||||
def handle_chld(self, sig, frame):
|
||||
@ -289,7 +287,7 @@ class Arbiter(object):
|
||||
"""
|
||||
if self.pidfile:
|
||||
old_pidfile = "%s.oldbin" % self.pidfile
|
||||
self.pidfile = old_pidfile
|
||||
self.pidfile = set_pidfile(self.pid, old_pidfile)
|
||||
|
||||
self.reexec_pid = os.fork()
|
||||
if self.reexec_pid != 0:
|
||||
|
||||
@ -9,65 +9,53 @@ import errno
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
class Pidfile(object):
|
||||
|
||||
def set_pidfile(pid, path, oldpath=None):
|
||||
oldpid = valid_pidfile(path)
|
||||
if oldpid:
|
||||
if oldpath is not None and path == oldpath and \
|
||||
oldpid == os.getpid():
|
||||
return path
|
||||
raise RuntimeError("Already running on PID %s " \
|
||||
"(or pid file '%s' is stale)" % (os.getpid(), path))
|
||||
|
||||
if oldpath:
|
||||
unlink_pidfile(pid, path)
|
||||
|
||||
# write pidfile
|
||||
fd, fname = tempfile.mkstemp(dir=os.path.dirname(path))
|
||||
os.write(fd, "%s\n" % pid)
|
||||
os.rename(fname, path)
|
||||
os.close(fd)
|
||||
return path
|
||||
|
||||
def __get__(self, instance, cls):
|
||||
if instance is None:
|
||||
return self
|
||||
def unlink_pidfile(pid, path):
|
||||
""" delete pidfile"""
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
pid1 = int(f.read() or 0)
|
||||
|
||||
return instance._pidfile
|
||||
|
||||
def __set__(self, instance, path):
|
||||
if not path:
|
||||
if pid1 == pid:
|
||||
os.unlink(path)
|
||||
except:
|
||||
pass
|
||||
|
||||
def valid_pidfile(path):
|
||||
""" Validate pidfile and make it stale if needed"""
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
wpid = int(f.read() or 0)
|
||||
|
||||
if wpid <= 0: return None
|
||||
|
||||
try:
|
||||
os.kill(wpid, 0)
|
||||
return wpid
|
||||
except OSError, e:
|
||||
if e[0] == errno.ESRCH:
|
||||
return
|
||||
raise
|
||||
except IOError, e:
|
||||
if e[0] == errno.ENOENT:
|
||||
return
|
||||
pid = self.valid_pidfile(path)
|
||||
if pid:
|
||||
if instance._pidfile is not None and path == instance._pidfile and \
|
||||
pid == os.getpid():
|
||||
return path
|
||||
raise RuntimeError("Already running on PID %s " \
|
||||
"(or pid file '%s' is stale)" % (os.getpid(), path))
|
||||
if instance._pidfile:
|
||||
self.unlink_pidfile(instance, instance._pidfile)
|
||||
|
||||
# write pidfile
|
||||
fd, fname = tempfile.mkstemp(dir=os.path.dirname(path))
|
||||
os.write(fd, "%s\n" % instance.pid)
|
||||
os.rename(fname, path)
|
||||
os.close(fd)
|
||||
instance._pidfile = path
|
||||
|
||||
def __delete__(self, instance):
|
||||
self.unlink_pidfile(instance, instance._pidfile)
|
||||
instance._pidfile = None
|
||||
|
||||
def unlink_pidfile(self, instance, path):
|
||||
""" delete pidfile"""
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
pid = int(f.read() or 0)
|
||||
|
||||
if pid == instance.pid:
|
||||
os.unlink(path)
|
||||
except:
|
||||
pass
|
||||
|
||||
def valid_pidfile(self, path):
|
||||
""" Validate pidfile and make it stale if needed"""
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
wpid = int(f.read() or 0)
|
||||
|
||||
if wpid <= 0: return None
|
||||
|
||||
try:
|
||||
os.kill(wpid, 0)
|
||||
return wpid
|
||||
except OSError, e:
|
||||
if e[0] == errno.ESRCH:
|
||||
return
|
||||
raise
|
||||
except IOError, e:
|
||||
if e[0] == errno.ENOENT:
|
||||
return
|
||||
raise
|
||||
raise
|
||||
Loading…
x
Reference in New Issue
Block a user