Commit e0af2210 authored by Robin Obůrka's avatar Robin Obůrka Committed by Robin Obůrka

msgloop: Small improvements in current proof-of-concept code

- Some rearrangement of code
- Add some comments
- More reasonable before_first_loop implementation
parent 7d68444d
......@@ -23,18 +23,20 @@ class LoopFail(Exception):
class SNBox():
def __init__(self, box_name, argparser=None):
# Important provided values into box
self.name = box_name
self.logger = logging.getLogger(box_name)
# Internal context values
# Local contexts for dependencies
self.zmq_ctx = zmq.Context.instance()
self.sn_ctx = SN(self.zmq_ctx, argparser or get_arg_parser())
# Important values provided to box
self.name = box_name
self.logger = logging.getLogger(box_name)
self.args = self.sn_ctx.args
# User data
self.context = None
# Error management
# Error management of the loop
self.loop_continue = True
self.errors_in_row = 0
# User data
# Data generated by setup function are placed into separate variable
# Final box shouldn't use "self" - we want to isolate its values
self.context = None
# Core methods - Will be implemented in non-abstract boxes
def check_configuration(self):
......@@ -61,14 +63,17 @@ class SNBox():
# Provided functionality - should be final implementation
def run(self):
# This is the only way to be sure that check will be called.
# Constructors will be overwritten in non-abstract boxes
self.check_configuration()
try:
self.context = self.get_user_data()
self.logger.info("SN main starting loop for %s box", self.name)
self.logger.info("SNBox starting loop for %s box", self.name)
self.register_signals()
self.before_first_request()
self.before_loop()
self.run_loop()
except LoopHardFail as e:
......@@ -81,8 +86,8 @@ class SNBox():
pass
finally:
self.teardown_box()
self.teardown()
self.teardown() # Clean-up data generated by setup()
self.teardown_box() # Clean-up my local contexts
def get_user_data(self):
user_data = self.setup()
......@@ -100,6 +105,11 @@ class SNBox():
for sig in [ signal.SIGHUP, signal.SIGTERM, signal.SIGQUIT, signal.SIGABRT ]:
signal.signal(sig, signal_handler)
def before_loop(self):
result = self.before_first_request()
if result:
self.process_result(result)
def teardown_box(self):
self.zmq_ctx.destroy()
......@@ -115,6 +125,8 @@ class SNBox():
break
except (SetupError, NotImplementedError) as e:
# These error are considered as show-stopper.
# It means programmer error ans there is no reason for trying to recover
raise e
except Exception as e:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment