mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
display log when murdering the socket. While i'm here fix the issue we
had when a worker was respawned
This commit is contained in:
parent
834c080b7a
commit
a79f2e6156
@ -39,10 +39,9 @@ class Arbiter(object):
|
|||||||
self.timeout = 30
|
self.timeout = 30
|
||||||
self.reexec_pid = 0
|
self.reexec_pid = 0
|
||||||
self.pid = os.getpid()
|
self.pid = os.getpid()
|
||||||
|
self.log = logging.getLogger(__name__)
|
||||||
self.init_signals()
|
self.init_signals()
|
||||||
self.listen(self.address)
|
self.listen(self.address)
|
||||||
self.log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
self.log.info("Booted Arbiter: %s" % os.getpid())
|
self.log.info("Booted Arbiter: %s" % os.getpid())
|
||||||
|
|
||||||
|
|
||||||
@ -200,7 +199,7 @@ class Arbiter(object):
|
|||||||
|
|
||||||
def sleep(self):
|
def sleep(self):
|
||||||
try:
|
try:
|
||||||
ready = select.select([self.PIPE[0]], [], [], 1)
|
ready = select.select([self.PIPE[0]], [], [], 1.0)
|
||||||
if not ready[0]:
|
if not ready[0]:
|
||||||
return
|
return
|
||||||
while os.read(self.PIPE[0], 1):
|
while os.read(self.PIPE[0], 1):
|
||||||
@ -237,6 +236,7 @@ class Arbiter(object):
|
|||||||
diff = time.time() - os.fstat(worker.tmp.fileno()).st_ctime
|
diff = time.time() - os.fstat(worker.tmp.fileno()).st_ctime
|
||||||
if diff <= self.timeout:
|
if diff <= self.timeout:
|
||||||
continue
|
continue
|
||||||
|
self.log.error("worker %s PID %s timeout killing." % (str(worker.id), pid))
|
||||||
self.kill_worker(pid, signal.SIGKILL)
|
self.kill_worker(pid, signal.SIGKILL)
|
||||||
|
|
||||||
def reap_workers(self):
|
def reap_workers(self):
|
||||||
@ -270,7 +270,7 @@ class Arbiter(object):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
worker = Worker(i, self.pid, self.LISTENER, self.modname,
|
worker = Worker(i, self.pid, self.LISTENER, self.modname,
|
||||||
self.timeout / 2.0)
|
self.timeout)
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
if pid != 0:
|
if pid != 0:
|
||||||
self.WORKERS[pid] = worker
|
self.WORKERS[pid] = worker
|
||||||
@ -283,6 +283,7 @@ class Arbiter(object):
|
|||||||
worker.run()
|
worker.run()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
|
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
self.log.exception("Exception in worker process.")
|
self.log.exception("Exception in worker process.")
|
||||||
@ -304,7 +305,8 @@ class Arbiter(object):
|
|||||||
self.log.warning("Problem killing process: %s" % pid)
|
self.log.warning("Problem killing process: %s" % pid)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
if e.errno == errno.ESRCH:
|
if e.errno == errno.ESRCH:
|
||||||
pass
|
try:
|
||||||
finally:
|
worker.tmp.close()
|
||||||
worker.tmp.close()
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,7 @@ class HttpResponse(object):
|
|||||||
resp_head.append("Date: %s\r\n" % http_date())
|
resp_head.append("Date: %s\r\n" % http_date())
|
||||||
# broken clients
|
# broken clients
|
||||||
resp_head.append("Status: %s\r\n" % str(self.status))
|
resp_head.append("Status: %s\r\n" % str(self.status))
|
||||||
# always close the conenction
|
# always close the connection
|
||||||
resp_head.append("Connection: close\r\n")
|
resp_head.append("Connection: close\r\n")
|
||||||
for name, value in self.headers.items():
|
for name, value in self.headers.items():
|
||||||
resp_head.append("%s: %s\r\n" % (name, value))
|
resp_head.append("%s: %s\r\n" % (name, value))
|
||||||
|
|||||||
@ -22,7 +22,6 @@ weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
|||||||
monthname = [None,
|
monthname = [None,
|
||||||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||||||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
||||||
|
|
||||||
|
|
||||||
def close_on_exec(fd):
|
def close_on_exec(fd):
|
||||||
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
|
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
|
||||||
@ -62,21 +61,9 @@ def write(sock, data):
|
|||||||
if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
|
if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
|
||||||
break
|
break
|
||||||
elif e[0] in (errno.EPIPE,):
|
elif e[0] in (errno.EPIPE,):
|
||||||
if i == 0:
|
break
|
||||||
continue
|
|
||||||
raise
|
raise
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def write_nonblock(sock, data):
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
ret = select.select([], [sock.fileno()], [], 2.0)
|
|
||||||
if ret[1]: break
|
|
||||||
except socket.error, e:
|
|
||||||
if e[0] == errno.EINTR:
|
|
||||||
continue
|
|
||||||
raise
|
|
||||||
write(sock, data)
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_name(name):
|
def normalize_name(name):
|
||||||
|
|||||||
@ -28,7 +28,7 @@ class Worker(object):
|
|||||||
def __init__(self, workerid, ppid, socket, app, timeout):
|
def __init__(self, workerid, ppid, socket, app, timeout):
|
||||||
self.id = workerid
|
self.id = workerid
|
||||||
self.ppid = ppid
|
self.ppid = ppid
|
||||||
self.timeout = timeout
|
self.timeout = timeout / 2.0
|
||||||
fd, tmpname = tempfile.mkstemp()
|
fd, tmpname = tempfile.mkstemp()
|
||||||
self.tmp = os.fdopen(fd, "r+b")
|
self.tmp = os.fdopen(fd, "r+b")
|
||||||
self.tmpname = tmpname
|
self.tmpname = tmpname
|
||||||
@ -53,7 +53,7 @@ class Worker(object):
|
|||||||
signal.signal(signal.SIGTERM, self.handle_exit)
|
signal.signal(signal.SIGTERM, self.handle_exit)
|
||||||
signal.signal(signal.SIGINT, self.handle_exit)
|
signal.signal(signal.SIGINT, self.handle_exit)
|
||||||
signal.signal(signal.SIGUSR1, self.handle_quit)
|
signal.signal(signal.SIGUSR1, self.handle_quit)
|
||||||
|
|
||||||
def handle_quit(self, sig, frame):
|
def handle_quit(self, sig, frame):
|
||||||
self.alive = False
|
self.alive = False
|
||||||
|
|
||||||
@ -80,9 +80,11 @@ class Worker(object):
|
|||||||
try:
|
try:
|
||||||
client, addr = self.socket.accept()
|
client, addr = self.socket.accept()
|
||||||
|
|
||||||
|
self.client = client
|
||||||
|
|
||||||
# handle connection
|
# handle connection
|
||||||
self.handle(client, addr)
|
self.handle(client, addr)
|
||||||
|
|
||||||
# Update the fd mtime on each client completion
|
# Update the fd mtime on each client completion
|
||||||
# to signal that this worker process is alive.
|
# to signal that this worker process is alive.
|
||||||
spinner = (spinner+1) % 2
|
spinner = (spinner+1) % 2
|
||||||
@ -118,6 +120,7 @@ class Worker(object):
|
|||||||
|
|
||||||
spinner = (spinner+1) % 2
|
spinner = (spinner+1) % 2
|
||||||
self._fchmod(spinner)
|
self._fchmod(spinner)
|
||||||
|
|
||||||
|
|
||||||
def handle(self, client, addr):
|
def handle(self, client, addr):
|
||||||
util.close_on_exec(client)
|
util.close_on_exec(client)
|
||||||
@ -126,7 +129,6 @@ class Worker(object):
|
|||||||
response = self.app(req.read(), req.start_response)
|
response = self.app(req.read(), req.start_response)
|
||||||
http.HttpResponse(client, response, req).send()
|
http.HttpResponse(client, response, req).send()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
# TODO: try to send something if an error happend
|
self.log.exception("Error processing request. [%s]" % str(e))
|
||||||
self.log.exception("Error processing request. [%s]" % str(e))
|
|
||||||
# try to send something if an error happend
|
|
||||||
util.close(client)
|
util.close(client)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user