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