Introduce intent for TFW messages and fix FSM related bugs

This commit is contained in:
R. Richard
2019-08-23 15:27:03 +02:00
committed by therealkrispet
parent f626fef8f8
commit 2e5867cc49
14 changed files with 42 additions and 19 deletions

View File

@ -1,7 +1,7 @@
import logging
from tfw.internals.crypto import KeyManager, sign_message
from tfw.internals.networking import Scope
from tfw.internals.networking import Scope, Intent
from .fsm_updater import FSMUpdater
@ -12,15 +12,16 @@ class FSMHandler:
keys = ['fsm']
def __init__(self, *, fsm_type, initial_trigger):
self.connector = None
self.fsm = fsm_type()
self._fsm_updater = FSMUpdater(self.fsm)
self.auth_key = KeyManager().auth_key
self.initial_trigger = initial_trigger
self.command_handlers = {
'frontend.ready': self.handle_ready,
'fsm.step' : self.handle_step,
'fsm.announce' : self.handle_announce
'frontend.ready' : self.handle_ready,
'fsm.step' : self.handle_step,
'fsm.update' : self.handle_update
}
def start(self):
@ -32,7 +33,7 @@ class FSMHandler:
if message:
fsm_update_message = self._fsm_updater.fsm_update
sign_message(self.auth_key, fsm_update_message)
connector.send_message(fsm_update_message, Scope.BROADCAST)
connector.send_message(fsm_update_message, Scope.BROADCAST, Intent.EVENT)
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
@ -41,10 +42,10 @@ class FSMHandler:
self.connector.unsubscribe('frontend.ready')
return message
def handle_step(self, message):
def handle_step(self, message): # pylint: disable=inconsistent-return-statements
if self.fsm.step(message['trigger']):
return message
def handle_announce(self, message):
def handle_update(self, message):
# pylint: disable=no-self-use
return message

View File

@ -15,8 +15,9 @@ class FSMUpdater:
{'trigger': trigger}
for trigger in self.fsm.get_triggers(self.fsm.state)
]
if not self.fsm.event_log:
return {}
last_fsm_event = self.fsm.event_log[-1]
last_fsm_event['timestamp'] = last_fsm_event['timestamp'].isoformat()
return {
'current_state': self.fsm.state,
'valid_transitions': valid_transitions,

View File

@ -1,6 +1,4 @@
import logging
from tfw.internals.networking import Scope
from tfw.internals.networking import Scope, Intent
from tfw.internals.inotify import InotifyObserver
from .supervisor import ProcessLogManager
@ -33,4 +31,4 @@ class LogInotifyObserver(InotifyObserver, ProcessLogManager):
'key': 'process.log.new',
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
}, Scope.BROADCAST)
}, Scope.BROADCAST, Intent.EVENT)

View File

@ -1,7 +1,7 @@
import logging
from xmlrpc.client import Fault as SupervisorFault
from tfw.internals.networking import Scope
from tfw.internals.networking import Scope, Intent
from .supervisor import ProcessManager
@ -26,7 +26,7 @@ class ProcessHandler(ProcessManager):
self.commands[message['key']](message['name'])
except SupervisorFault as fault:
message['error'] = fault.faultString
connector.send_message(message, scope=Scope.WEBSOCKET)
connector.send_message(message, scope=Scope.BROADCAST, intent=Intent.EVENT)
except KeyError:
if not message['key'].startswith('process.log'):
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)