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

View File

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