Browse Source

Clean up initialization of controller

master
Neil Booth 8 years ago
parent
commit
6711ed0ae8
  1. 12
      server/controller.py
  2. 4
      server_main.py

12
server/controller.py

@ -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):

4
server_main.py

@ -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:

Loading…
Cancel
Save