mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Exit with a non-zero status if workers don't boot.
Raising a HaltServer exception in the arbiter will now exit with the provided reason and status code.
This commit is contained in:
parent
43232411fc
commit
63b53c9f51
5
examples/boot_fail.py
Normal file
5
examples/boot_fail.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
raise RuntimeError("Bad app!")
|
||||||
|
|
||||||
|
def app(environ, start_response):
|
||||||
|
assert 1 == 2, "Shouldn't get here."
|
||||||
@ -14,6 +14,7 @@ import sys
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from gunicorn.errors import HaltServer
|
||||||
from gunicorn.pidfile import Pidfile
|
from gunicorn.pidfile import Pidfile
|
||||||
from gunicorn.sock import create_socket
|
from gunicorn.sock import create_socket
|
||||||
from gunicorn import util
|
from gunicorn import util
|
||||||
@ -168,6 +169,8 @@ class Arbiter(object):
|
|||||||
self.halt()
|
self.halt()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.halt()
|
self.halt()
|
||||||
|
except HaltServer, inst:
|
||||||
|
self.halt(reason=inst.reason, exit_status=inst.exit_status)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -259,13 +262,15 @@ class Arbiter(object):
|
|||||||
if e.errno not in [errno.EAGAIN, errno.EINTR]:
|
if e.errno not in [errno.EAGAIN, errno.EINTR]:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def halt(self):
|
def halt(self, reason=None, exit_status=0):
|
||||||
""" halt arbiter """
|
""" halt arbiter """
|
||||||
self.stop()
|
self.stop()
|
||||||
self.log.info("Shutting down: %s" % self.master_name)
|
self.log.info("Shutting down: %s" % self.master_name)
|
||||||
|
if reason is not None:
|
||||||
|
self.log.info("Reason: %s" % reason)
|
||||||
if self.pidfile is not None:
|
if self.pidfile is not None:
|
||||||
self.pidfile.unlink()
|
self.pidfile.unlink()
|
||||||
sys.exit(0)
|
sys.exit(exit_status)
|
||||||
|
|
||||||
def sleep(self):
|
def sleep(self):
|
||||||
"""\
|
"""\
|
||||||
@ -386,7 +391,8 @@ class Arbiter(object):
|
|||||||
# to avoid infinite start/stop cycles.
|
# to avoid infinite start/stop cycles.
|
||||||
exitcode = status >> 8
|
exitcode = status >> 8
|
||||||
if exitcode == self.WORKER_BOOT_ERROR:
|
if exitcode == self.WORKER_BOOT_ERROR:
|
||||||
raise StopIteration
|
reason = "Worker failed to boot."
|
||||||
|
raise HaltServer(reason, self.WORKER_BOOT_ERROR)
|
||||||
worker = self.WORKERS.pop(wpid, None)
|
worker = self.WORKERS.pop(wpid, None)
|
||||||
if not worker:
|
if not worker:
|
||||||
continue
|
continue
|
||||||
|
|||||||
8
gunicorn/errors.py
Normal file
8
gunicorn/errors.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
class HaltServer(Exception):
|
||||||
|
def __init__(self, reason, exit_status=1):
|
||||||
|
self.reason = reason
|
||||||
|
self.exit_status = exit_status
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "<HaltServer %r %d>" % (self.reason, self.exit_status)
|
||||||
Loading…
x
Reference in New Issue
Block a user