2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .log_inotify_observer import LogInotifyObserver
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2018-05-30 13:21:36 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
class ProcessLogHandler:
|
2019-07-12 21:25:16 +00:00
|
|
|
keys = ['logmonitor']
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
|
|
|
Monitors the output of a supervisor process (stdout, stderr) and
|
|
|
|
sends the results to the frontend.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
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.
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
2019-07-23 13:32:50 +00:00
|
|
|
def __init__(self, *, process_name, supervisor_uri, log_tail=0):
|
2019-07-12 21:25:16 +00:00
|
|
|
self.server_connector = None
|
2018-05-30 13:21:36 +00:00
|
|
|
self.process_name = process_name
|
2019-07-23 13:32:50 +00:00
|
|
|
self._supervisor_uri = supervisor_uri
|
2019-07-12 21:25:16 +00:00
|
|
|
self._initial_log_tail = log_tail
|
|
|
|
self._monitor = None
|
2018-05-30 13:21:36 +00:00
|
|
|
|
|
|
|
self.command_handlers = {
|
|
|
|
'process_name': self.handle_process_name,
|
|
|
|
'log_tail': self.handle_log_tail
|
|
|
|
}
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def start(self):
|
|
|
|
self._monitor = LogInotifyObserver(
|
|
|
|
server_connector=self.server_connector,
|
2019-07-23 13:32:50 +00:00
|
|
|
supervisor_uri=self._supervisor_uri,
|
2019-07-12 21:25:16 +00:00
|
|
|
process_name=self.process_name,
|
|
|
|
log_tail=self._initial_log_tail
|
|
|
|
)
|
|
|
|
self._monitor.start()
|
|
|
|
|
|
|
|
def handle_event(self, message, _):
|
2018-05-30 13:21:36 +00:00
|
|
|
try:
|
|
|
|
data = message['data']
|
|
|
|
self.command_handlers[data['command']](data)
|
|
|
|
except KeyError:
|
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
|
|
|
|
def handle_process_name(self, data):
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
|
|
|
Changes the monitored process.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'value'
|
|
|
|
(name of the process to monitor)
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
2019-06-27 13:22:48 +00:00
|
|
|
self._monitor.process_name = data['value']
|
2018-05-30 13:21:36 +00:00
|
|
|
|
|
|
|
def handle_log_tail(self, data):
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
|
|
|
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).
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'value'
|
|
|
|
(new tail length)
|
2018-05-31 12:03:11 +00:00
|
|
|
"""
|
2019-06-27 13:22:48 +00:00
|
|
|
self._monitor.log_tail = data['value']
|
2018-06-01 13:15:07 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
2019-06-27 13:22:48 +00:00
|
|
|
self._monitor.stop()
|