mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 15:41:32 +00:00
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
import logging
|
|
|
|
from .log_inotify_observer import LogInotifyObserver
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class ProcessLogHandler:
|
|
keys = ['process.log']
|
|
type_id = 'ControlEventHandler'
|
|
|
|
def __init__(self, *, process_name, supervisor_uri, log_tail=0):
|
|
self.connector, self._monitor = None, None
|
|
self.process_name = process_name
|
|
self._supervisor_uri = supervisor_uri
|
|
self._initial_log_tail = log_tail
|
|
|
|
self.command_handlers = {
|
|
'process.log.set': self.handle_set,
|
|
'process.log.start': self.handle_start,
|
|
'process.log.stop': self.handle_stop
|
|
}
|
|
|
|
def start(self):
|
|
if not self._monitor:
|
|
self._monitor = LogInotifyObserver(
|
|
connector=self.connector,
|
|
process_name=self.process_name,
|
|
supervisor_uri=self._supervisor_uri,
|
|
log_tail=self._initial_log_tail
|
|
)
|
|
self._monitor.start()
|
|
|
|
def handle_event(self, message, _):
|
|
try:
|
|
self.command_handlers[message['key']](message)
|
|
except KeyError:
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
def handle_set(self, data):
|
|
if data.get('name'):
|
|
self._monitor.process_name = data['name']
|
|
if data.get('tail'):
|
|
self._monitor.log_tail = data['tail']
|
|
|
|
def handle_start(self, _):
|
|
self.start()
|
|
|
|
def handle_stop(self, _):
|
|
self._stop_monitor()
|
|
|
|
def _stop_monitor(self):
|
|
if self._monitor:
|
|
self._monitor.stop()
|
|
self._monitor = None
|
|
|
|
def cleanup(self):
|
|
self._stop_monitor()
|