2019-07-05 13:25:59 +00:00
|
|
|
import logging
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from tfw.internals.networking import Scope
|
|
|
|
from tfw.internals.inotify import InotifyObserver
|
2019-07-05 13:25:59 +00:00
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .supervisor import ProcessLogManager
|
2019-07-05 13:25:59 +00:00
|
|
|
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
class LogInotifyObserver(InotifyObserver, ProcessLogManager):
|
2019-07-05 13:25:59 +00:00
|
|
|
def __init__(self, server_connector, supervisor_uri, process_name, log_tail=0):
|
|
|
|
self._prevent_log_recursion()
|
|
|
|
self._server_connector = server_connector
|
|
|
|
self._process_name = process_name
|
|
|
|
self.log_tail = log_tail
|
|
|
|
self._procinfo = None
|
2019-07-24 13:17:16 +00:00
|
|
|
ProcessLogManager.__init__(self, supervisor_uri)
|
2019-07-05 13:25:59 +00:00
|
|
|
InotifyObserver.__init__(self, self._get_logfiles())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _prevent_log_recursion():
|
|
|
|
# This is done to prevent inotify event logs triggering themselves (infinite log recursion)
|
|
|
|
logging.getLogger('watchdog.observers.inotify_buffer').propagate = False
|
|
|
|
|
|
|
|
def _get_logfiles(self):
|
|
|
|
self._procinfo = self.supervisor.getProcessInfo(self._process_name)
|
|
|
|
return self._procinfo['stdout_logfile'], self._procinfo['stderr_logfile']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def process_name(self):
|
|
|
|
return self._process_name
|
|
|
|
|
|
|
|
@process_name.setter
|
|
|
|
def process_name(self, process_name):
|
|
|
|
self._process_name = process_name
|
|
|
|
self.paths = self._get_logfiles()
|
|
|
|
|
|
|
|
def on_modified(self, event):
|
|
|
|
self._server_connector.send_message({
|
|
|
|
'key': 'processlog',
|
|
|
|
'data': {
|
|
|
|
'command': 'new_log',
|
|
|
|
'stdout': self.read_stdout(self.process_name, tail=self.log_tail),
|
|
|
|
'stderr': self.read_stderr(self.process_name, tail=self.log_tail)
|
|
|
|
}
|
|
|
|
}, Scope.BROADCAST)
|