Clean up initialization of controller

This commit is contained in:
Neil Booth 2016-10-18 20:57:14 +09:00
parent 8452d0c016
commit 6711ed0ae8
2 changed files with 9 additions and 7 deletions

View File

@ -16,12 +16,13 @@ from lib.util import LoggedClass
class Controller(LoggedClass): class Controller(LoggedClass):
def __init__(self, env): def __init__(self, loop, env):
'''Create up the controller. '''Create up the controller.
Creates DB, Daemon and BlockCache instances. Creates DB, Daemon and BlockCache instances.
''' '''
super().__init__() super().__init__()
self.loop = loop
self.env = env self.env = env
self.db = DB(env) self.db = DB(env)
self.daemon = Daemon(env.daemon_url) self.daemon = Daemon(env.daemon_url)
@ -32,9 +33,10 @@ class Controller(LoggedClass):
self.jobs = set() self.jobs = set()
self.peers = {} self.peers = {}
def start(self, loop): def start(self):
'''Prime the event loop with asynchronous servers and jobs.''' '''Prime the event loop with asynchronous servers and jobs.'''
env = self.env env = self.env
loop = self.loop
if False: if False:
protocol = partial(LocalRPC, self) protocol = partial(LocalRPC, self)
@ -69,18 +71,18 @@ class Controller(LoggedClass):
# Signal handlers # Signal handlers
for signame in ('SIGINT', 'SIGTERM'): for signame in ('SIGINT', 'SIGTERM'):
loop.add_signal_handler(getattr(signal, signame), loop.add_signal_handler(getattr(signal, signame),
partial(self.on_signal, loop, signame)) partial(self.on_signal, signame))
def stop(self): def stop(self):
'''Close the listening servers.''' '''Close the listening servers.'''
for server in self.servers: for server in self.servers:
server.close() server.close()
def on_signal(self, loop, signame): def on_signal(self, signame):
'''Call on receipt of a signal to cleanly shutdown.''' '''Call on receipt of a signal to cleanly shutdown.'''
self.logger.warning('received {} signal, preparing to shut down' self.logger.warning('received {} signal, preparing to shut down'
.format(signame)) .format(signame))
for task in asyncio.Task.all_tasks(loop): for task in asyncio.Task.all_tasks(self.loop):
task.cancel() task.cancel()
def add_session(self, session): def add_session(self, session):

View File

@ -25,8 +25,8 @@ def main_loop():
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
#loop.set_debug(True) #loop.set_debug(True)
controller = Controller(env) controller = Controller(loop, env)
controller.start(loop) controller.start()
tasks = asyncio.Task.all_tasks(loop) tasks = asyncio.Task.all_tasks(loop)
try: try: