mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 18:51:31 +00:00
Update process- and logmonitor according to the new API
This commit is contained in:
parent
e414ea2631
commit
4b7510e704
@ -7,7 +7,7 @@ from .supervisor import ProcessLogManager
|
|||||||
|
|
||||||
|
|
||||||
class LogInotifyObserver(InotifyObserver, ProcessLogManager):
|
class LogInotifyObserver(InotifyObserver, ProcessLogManager):
|
||||||
def __init__(self, connector, supervisor_uri, process_name, log_tail=0):
|
def __init__(self, connector, process_name, supervisor_uri, log_tail=0):
|
||||||
self._prevent_log_recursion()
|
self._prevent_log_recursion()
|
||||||
self._connector = connector
|
self._connector = connector
|
||||||
self._process_name = process_name
|
self._process_name = process_name
|
||||||
@ -36,10 +36,7 @@ class LogInotifyObserver(InotifyObserver, ProcessLogManager):
|
|||||||
|
|
||||||
def on_modified(self, event):
|
def on_modified(self, event):
|
||||||
self._connector.send_message({
|
self._connector.send_message({
|
||||||
'key': 'processlog',
|
'key': 'log.new',
|
||||||
'data': {
|
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
|
||||||
'command': 'new_log',
|
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
|
||||||
'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)
|
||||||
|
@ -9,43 +9,31 @@ LOG = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class ProcessHandler(ProcessManager, ProcessLogManager):
|
class ProcessHandler(ProcessManager, ProcessLogManager):
|
||||||
keys = ['processmanager']
|
keys = ['process.start', 'process.stop', 'process.restart']
|
||||||
"""
|
|
||||||
Event handler that can manage processes managed by supervisor.
|
|
||||||
|
|
||||||
This EventHandler accepts messages that have a data['command'] key specifying
|
|
||||||
a command to be executed.
|
|
||||||
Every message must contain a data['process_name'] field with the name of the
|
|
||||||
process to manage. This is the name specified in supervisor config files like so:
|
|
||||||
[program:someprogram]
|
|
||||||
|
|
||||||
Commands available: start, stop, restart, readlog
|
|
||||||
(the names are as self-documenting as it gets)
|
|
||||||
"""
|
|
||||||
def __init__(self, *, supervisor_uri, log_tail=0):
|
def __init__(self, *, supervisor_uri, log_tail=0):
|
||||||
ProcessManager.__init__(self, supervisor_uri)
|
ProcessManager.__init__(self, supervisor_uri)
|
||||||
ProcessLogManager.__init__(self, supervisor_uri)
|
ProcessLogManager.__init__(self, supervisor_uri)
|
||||||
self.log_tail = log_tail
|
self.log_tail = log_tail
|
||||||
|
|
||||||
self.commands = {
|
self.commands = {
|
||||||
'start': self.start_process,
|
'process.start': self.start_process,
|
||||||
'stop': self.stop_process,
|
'process.stop': self.stop_process,
|
||||||
'restart': self.restart_process
|
'process.restart': self.restart_process
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle_event(self, message, connector):
|
def handle_event(self, message, connector):
|
||||||
try:
|
try:
|
||||||
data = message['data']
|
|
||||||
try:
|
try:
|
||||||
self.commands[data['command']](data['process_name'])
|
self.commands[message['key']](message['name'])
|
||||||
except SupervisorFault as fault:
|
except SupervisorFault as fault:
|
||||||
message['data']['error'] = fault.faultString
|
message['error'] = fault.faultString
|
||||||
finally:
|
finally:
|
||||||
message['data']['stdout'] = self.read_stdout(
|
message['stdout'] = self.read_stdout(
|
||||||
data['process_name'],
|
message['name'],
|
||||||
self.log_tail
|
self.log_tail
|
||||||
)
|
)
|
||||||
message['data']['stderr'] = self.read_stderr(
|
message['stderr'] = self.read_stderr(
|
||||||
data['process_name'],
|
message['name'],
|
||||||
self.log_tail
|
self.log_tail
|
||||||
)
|
)
|
||||||
connector.send_message(message, scope=Scope.WEBSOCKET)
|
connector.send_message(message, scope=Scope.WEBSOCKET)
|
||||||
|
@ -6,63 +6,38 @@ LOG = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class ProcessLogHandler:
|
class ProcessLogHandler:
|
||||||
keys = ['logmonitor']
|
keys = ['log.set']
|
||||||
"""
|
|
||||||
Monitors the output of a supervisor process (stdout, stderr) and
|
|
||||||
sends the results to the frontend.
|
|
||||||
|
|
||||||
Accepts messages that have a data['command'] key specifying
|
|
||||||
a command to be executed.
|
|
||||||
|
|
||||||
The API of each command is documented in their respective handler.
|
|
||||||
"""
|
|
||||||
def __init__(self, *, process_name, supervisor_uri, log_tail=0):
|
def __init__(self, *, process_name, supervisor_uri, log_tail=0):
|
||||||
self.connector = None
|
self.connector, self._monitor = None, None
|
||||||
self.process_name = process_name
|
self.process_name = process_name
|
||||||
self._supervisor_uri = supervisor_uri
|
self._supervisor_uri = supervisor_uri
|
||||||
self._initial_log_tail = log_tail
|
self._initial_log_tail = log_tail
|
||||||
self._monitor = None
|
|
||||||
|
|
||||||
self.command_handlers = {
|
self.command_handlers = {
|
||||||
'process_name': self.handle_process_name,
|
'log.set': self.handle_set
|
||||||
'log_tail': self.handle_log_tail
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._monitor = LogInotifyObserver(
|
self._monitor = LogInotifyObserver(
|
||||||
connector=self.connector,
|
connector=self.connector,
|
||||||
supervisor_uri=self._supervisor_uri,
|
|
||||||
process_name=self.process_name,
|
process_name=self.process_name,
|
||||||
|
supervisor_uri=self._supervisor_uri,
|
||||||
log_tail=self._initial_log_tail
|
log_tail=self._initial_log_tail
|
||||||
)
|
)
|
||||||
self._monitor.start()
|
self._monitor.start()
|
||||||
|
|
||||||
def handle_event(self, message, _):
|
def handle_event(self, message, _):
|
||||||
try:
|
try:
|
||||||
data = message['data']
|
self.command_handlers[message['key']](message)
|
||||||
self.command_handlers[data['command']](data)
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
||||||
|
|
||||||
def handle_process_name(self, data):
|
def handle_set(self, data):
|
||||||
"""
|
if data.get('name'):
|
||||||
Changes the monitored process.
|
self._monitor.process_name = data['name']
|
||||||
|
if data.get('tail'):
|
||||||
:param data: TFW message data containing 'value'
|
self._monitor.log_tail = data['tail']
|
||||||
(name of the process to monitor)
|
|
||||||
"""
|
|
||||||
self._monitor.process_name = data['value']
|
|
||||||
|
|
||||||
def handle_log_tail(self, data):
|
|
||||||
"""
|
|
||||||
Sets tail length of the log the monitor will send
|
|
||||||
to the frontend (the monitor will send back the last
|
|
||||||
'value' characters of the log).
|
|
||||||
|
|
||||||
:param data: TFW message data containing 'value'
|
|
||||||
(new tail length)
|
|
||||||
"""
|
|
||||||
self._monitor.log_tail = data['value']
|
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
self._monitor.stop()
|
self._monitor.stop()
|
||||||
|
Loading…
Reference in New Issue
Block a user