Prune more stale code

Change-Id: I73cf7c5af589e731fb4db56ac18481e7643552ed
This commit is contained in:
R. Tyler Ballance 2010-04-11 13:19:34 -07:00
parent c5845f13da
commit 353c023d4f
1 changed files with 1 additions and 210 deletions

View File

@ -184,7 +184,7 @@ class Controller(object):
self.spawn_children(number=1)
def run(self):
self.log.info('(%s) *** Controller starting at %s' % (self.controller_pid,
self.log.info('(%s) *** Controller starting at %s' % (self.controller_pid,
time.asctime()))
if self.config.get('pidfile'):
@ -204,116 +204,6 @@ class Controller(object):
self.kill_children()
self.log.info('(%s) *** Controller exiting' % (self.controller_pid))
def spawn_new_children(sock, factory_qual, args, config):
num_processes = int(config.get('num_processes', 1))
parent_pid = os.getpid()
print "(%s) Spawning starting up: %s io processes, %s worker threads" % (
parent_pid, num_processes, config['threadpool_workers'])
if args.get('verbose'):
print "(%s) serving wsgi with configuration:" % (
os.getpid(), )
prettyconfig = pprint.pformat(config)
for line in prettyconfig.split('\n'):
print "(%s)\t%s" % (os.getpid(), line)
dev = args.get('dev', False)
child_pipes = []
for x in range(num_processes):
child_side, parent_side = os.pipe()
try:
child_pid = os.fork()
except:
print_exc("Couldn't fork child! Panic!")
restart_controller(factory_qual, args, sock, panic=True)
## Never gets here!
if not child_pid:
os.close(parent_side)
command = [
sys.executable,
'-c', 'from spawning import spawning_child;spawning_child.main();import os;os._exit(0)',
str(parent_pid),
str(sock.fileno()),
str(child_side),
factory_qual,
json.dumps(args)]
if dev and x == 0:
command.append('--reload')
env = environ()
env['EVENTLET_THREADPOOL_SIZE'] = str(config.get('threadpool_workers', 0))
os.execve(sys.executable, command, env)
os.close(child_side)
child_pipes.append(parent_side)
def sighup(_signum, _stack_frame):
global RESTART_CONTROLLER
RESTART_CONTROLLER = True
tokill = child_pipes[:]
del child_pipes[:]
for child in tokill:
try:
os.write(child, ' ')
os.close(child)
except OSError, e:
if e[0] != errno.EPIPE:
raise
signal.signal(signal.SIGHUP, sighup)
def reap_children():
global KEEP_GOING
try:
pid, result = os.wait()
except OSError, e:
if e.errno != errno.EINTR:
raise
except KeyboardInterrupt:
print "(%s) Controller exiting at %s" % (
os.getpid(), time.asctime())
KEEP_GOING = False
os.kill(os.getpid(), signal.SIGHUP)
while True:
## Keep waiting until all children are dead.
try:
pid, result = os.wait()
except OSError, e:
if e[0] == errno.ECHILD:
break
else:
if result:
signum = os.WTERMSIG(result)
exitcode = os.WEXITSTATUS(result)
if signum:
print "(%s) Child died from signal %s with code %s." % (
os.getpid(), signum, exitcode)
else:
print "(%s) Child %s died with code %s." % (
os.getpid(), pid, exitcode)
## The way the code is set up right now it's easier just to panic and
## start new children if one of the children dies in a way we didn't expect.
## Would probably be better to give this code access to child_pipes
## in spawn_new_children somehow so it can just start a new child and munge
## child_pipes appropriately
global PANIC
PANIC = True
print "(%s) !!! Panic: Why did that child die? Restarting" % (os.getpid(), )
os.kill(os.getpid(), signal.SIGHUP)
else:
print "(%s) Child %s exited normally." % (
os.getpid(), pid)
def bind_socket(config):
sleeptime = 0.5
host = config.get('host', '')
@ -335,105 +225,6 @@ def bind_socket(config):
sys.exit(1)
return sock
def restart_controller(factory_qual, args, sock, panic=False):
## In case the installed copy of spawning has changed,
## execv spawn here so the controller process gets reloaded.
## We could somehow check to see if the spawning_controller
## actually is different from the current one and not restart the
## entire process in this case, which would result in faster restarts.
## But it's 'fast enough' for now.
restart_args = dict(
factory=factory_qual,
factory_args=args)
if sock is not None:
restart_args['fd'] = sock.fileno()
if panic:
start_delay = args.get('start_delay')
if start_delay is None:
start_delay = 0.125
else:
start_delay *= 2
restart_args['start_delay'] = start_delay
os.execvpe(
sys.executable,
[sys.executable, '-c', 'from spawning import spawning_controller;spawning_controller.main()', '-z', json.dumps(restart_args)],
environ())
## Never gets here!
def run_controller(factory_qual, args, sock=None):
controller_pid = os.getpid()
print "(%s) **** Controller starting up at %s" % (
controller_pid, time.asctime())
try:
config = spawning.util.named(factory_qual)(args)
except:
print_exc("Couldn't import the WSGI factory! Panic!")
restart_controller(factory_qual, args, sock, panic=True)
## Never gets here!
pidfile = config.get("pidfile")
if pidfile:
f = open(pidfile, "w")
try:
f.write("%s\n" % (controller_pid,))
finally:
f.close()
spawning.setproctitle("spawn: controller " + args.get('argv_str', ''))
dev = config.get('dev', False)
if not dev:
## Set up the production reloader that watches the svn revision number.
if not os.fork():
if sock is not None:
sock.close()
base = os.path.dirname(__file__)
os.chdir(base)
args = [
sys.executable,
'reloader_svn.py',
'--pid=' + str(controller_pid),
'--dir=' + base,
]
for dirname in config.get('source_directories', []):
args.append('--dir=' + dirname)
os.execve(sys.executable, args, environ())
## Never gets here!
if sock is None:
sock = bind_socket(config)
spawn_new_children(sock, factory_qual, args, config)
start_time = time.time()
start_delay = args.get('start_delay')
while True:
reap_children()
## Random heuristic: If we've been running for 64x longer than the start_delay
## or 5 minutes, whatever is shorter, we can clear the start_delay
if start_delay is not None:
if time.time() - start_time > min(start_delay * 64, 60 * 5):
print "(%s) We've been running OK for a while, clear the exponential backoff" % (
os.getpid(), )
del args['start_delay']
if RESTART_CONTROLLER:
break
if KEEP_GOING:
restart_controller(factory_qual, args, sock, panic=PANIC)
## Never gets here!
def set_process_owner(spec):
import pwd, grp
if ":" in spec: