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-08-08 13:05:37 +00:00
|
|
|
keys = ['process.log']
|
2019-08-30 12:45:53 +00:00
|
|
|
type_id = 'ControlEventHandler'
|
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-08-07 07:47:57 +00:00
|
|
|
self.connector, self._monitor = None, 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
|
2018-05-30 13:21:36 +00:00
|
|
|
|
|
|
|
self.command_handlers = {
|
2019-12-19 15:21:15 +00:00
|
|
|
'process.log.set': self.handle_set,
|
|
|
|
'process.log.start': self.handle_start,
|
|
|
|
'process.log.stop': self.handle_stop
|
2018-05-30 13:21:36 +00:00
|
|
|
}
|
2018-05-30 11:14:25 +00:00
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def start(self):
|
2019-12-19 15:21:15 +00:00
|
|
|
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()
|
2019-07-12 21:25:16 +00:00
|
|
|
|
|
|
|
def handle_event(self, message, _):
|
2018-05-30 13:21:36 +00:00
|
|
|
try:
|
2019-08-07 07:47:57 +00:00
|
|
|
self.command_handlers[message['key']](message)
|
2018-05-30 13:21:36 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
|
2019-08-07 07:47:57 +00:00
|
|
|
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']
|
2018-06-01 13:15:07 +00:00
|
|
|
|
2019-12-19 15:21:15 +00:00
|
|
|
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
|
|
|
|
|
2018-06-01 13:15:07 +00:00
|
|
|
def cleanup(self):
|
2019-12-19 15:21:15 +00:00
|
|
|
self._stop_monitor()
|