mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-09 02:17:16 +00:00
Improve EventHandler response sending pattern
This commit is contained in:
parent
481a919606
commit
018286f397
@ -53,7 +53,7 @@ class DirectorySnapshottingEventHandler(EventHandler):
|
||||
try:
|
||||
data = message['data']
|
||||
message['data'] = self.command_handlers[data['command']](data)
|
||||
return message
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
||||
|
@ -17,7 +17,7 @@ class FrontendEventHandler(EventHandler):
|
||||
self._frontend_message_storage.save_message(message)
|
||||
if message['key'] == 'recover':
|
||||
self.recover_frontend()
|
||||
return message
|
||||
self.send_message(message)
|
||||
|
||||
def recover_frontend(self):
|
||||
for message in self._frontend_message_storage.messages:
|
||||
|
@ -21,6 +21,5 @@ class FSMAwareEventHandler(EventHandler, FSMAware, ABC):
|
||||
self.subscribe('fsm_update')
|
||||
|
||||
def dispatch_handling(self, message):
|
||||
if self.refresh_on_fsm_update(message):
|
||||
return None
|
||||
return super().dispatch_handling(message)
|
||||
if not self.refresh_on_fsm_update(message):
|
||||
super().dispatch_handling(message)
|
||||
|
@ -46,7 +46,7 @@ class FSMManagingEventHandler(EventHandler):
|
||||
sign_message(self.auth_key, message)
|
||||
sign_message(self.auth_key, fsm_update_message)
|
||||
self.server_connector.send_message(fsm_update_message, Scope.BROADCAST)
|
||||
return message
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
||||
|
@ -187,7 +187,7 @@ class IdeEventHandler(EventHandler):
|
||||
data = message['data']
|
||||
message['data'] = self.commands[data['command']](data)
|
||||
self.attach_fileinfo(data)
|
||||
return message
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
||||
|
@ -58,6 +58,6 @@ class ProcessManagingEventHandler(EventHandler):
|
||||
data['process_name'],
|
||||
self.log_tail
|
||||
)
|
||||
return message
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
@ -56,7 +56,7 @@ class TerminalEventHandler(EventHandler):
|
||||
try:
|
||||
data = message['data']
|
||||
message['data'] = self.commands[data['command']](data)
|
||||
return message
|
||||
self.send_message(message)
|
||||
except KeyError:
|
||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||
|
||||
|
@ -5,8 +5,6 @@ import logging
|
||||
from inspect import currentframe
|
||||
from typing import Iterable
|
||||
|
||||
from tfw.networking import Scope
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@ -17,7 +15,7 @@ class EventHandlerBase:
|
||||
|
||||
Derived classes must implement the handle_event() method
|
||||
"""
|
||||
def __init__(self, key, server_connector, scope=Scope.ZMQ):
|
||||
def __init__(self, key, server_connector, scope):
|
||||
self.server_connector = server_connector
|
||||
self.scope = scope
|
||||
self.keys = []
|
||||
@ -29,12 +27,18 @@ class EventHandlerBase:
|
||||
self.subscribe(*self.keys)
|
||||
self.server_connector.register_callback(self.event_handler_callback)
|
||||
|
||||
@property
|
||||
def key(self):
|
||||
def subscribe(self, *keys):
|
||||
"""
|
||||
Returns the oldest key this EventHandler was subscribed to.
|
||||
Subscribe this EventHandler to receive events for given keys.
|
||||
Note that you can subscribe to the same key several times in which
|
||||
case you will need to unsubscribe multiple times in order to stop
|
||||
receiving events.
|
||||
|
||||
:param keys: list of keys to subscribe to
|
||||
"""
|
||||
return self.keys[0]
|
||||
for key in keys:
|
||||
self.server_connector.subscribe(key)
|
||||
self.keys.append(key)
|
||||
|
||||
def event_handler_callback(self, message):
|
||||
"""
|
||||
@ -43,15 +47,8 @@ class EventHandlerBase:
|
||||
a response back in case the handler returned something.
|
||||
This is subscribed in __init__().
|
||||
"""
|
||||
if not self.check_key(message):
|
||||
return
|
||||
|
||||
response = self.dispatch_handling(message)
|
||||
if response:
|
||||
self.send_message(response)
|
||||
|
||||
def send_message(self, message):
|
||||
self.server_connector.send_message(message, self.scope)
|
||||
if self.check_key(message):
|
||||
self.dispatch_handling(message)
|
||||
|
||||
def check_key(self, message):
|
||||
"""
|
||||
@ -74,7 +71,7 @@ class EventHandlerBase:
|
||||
:param message: the message received
|
||||
:returns: the message to send back
|
||||
"""
|
||||
return self.handle_event(message)
|
||||
self.handle_event(message)
|
||||
|
||||
def handle_event(self, message):
|
||||
"""
|
||||
@ -85,18 +82,8 @@ class EventHandlerBase:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def subscribe(self, *keys):
|
||||
"""
|
||||
Subscribe this EventHandler to receive events for given keys.
|
||||
Note that you can subscribe to the same key several times in which
|
||||
case you will need to unsubscribe multiple times in order to stop
|
||||
receiving events.
|
||||
|
||||
:param keys: list of keys to subscribe to
|
||||
"""
|
||||
for key in keys:
|
||||
self.server_connector.subscribe(key)
|
||||
self.keys.append(key)
|
||||
def send_message(self, message):
|
||||
self.server_connector.send_message(message, self.scope)
|
||||
|
||||
def unsubscribe(self, *keys):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user