updated logger, updated ipc setup

This commit is contained in:
itdominator 2022-06-14 18:19:21 -05:00
parent 88db4171cf
commit d74761344c
3 changed files with 77 additions and 65 deletions

View File

@ -16,7 +16,7 @@ class Application(EventSystem):
def __init__(self, args, unknownargs):
if not debug:
event_system.create_ipc_server()
event_system.create_ipc_listener()
time.sleep(0.1)
if not trace_debug:

View File

@ -16,35 +16,48 @@ def threaded(fn):
class IPCServer:
''' Create a listener so that other instances send requests back to existing instance. '''
def __init__(self, conn_type: str = "socket"):
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
def __init__(self, ipc_address: str = '127.0.0.1', conn_type: str = "socket"):
self.is_ipc_alive = False
self._ipc_port = 4848
self._ipc_address = ipc_address
self._conn_type = conn_type
self.ipc_authkey = b'app-ipc'
self.ipc_timeout = 15.0
self._ipc_authkey = b'app-ipc'
self._ipc_timeout = 15.0
if conn_type == "socket":
self.ipc_address = '/tmp/app-ipc.sock'
else:
self.ipc_address = '127.0.0.1'
self.ipc_port = 8888
self._ipc_address = '/tmp/app-ipc.sock'
elif conn_type == "full_network":
self._ipc_address = '0.0.0.0'
elif conn_type == "full_network_unsecured":
self._ipc_authkey = None
self._ipc_address = '0.0.0.0'
elif conn_type == "local_network_unsecured":
self._ipc_authkey = None
@threaded
def create_ipc_server(self) -> None:
def create_ipc_listener(self) -> None:
if self._conn_type == "socket":
if os.path.exists(self.ipc_address):
if os.path.exists(self._ipc_address):
return
listener = Listener(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
listener = Listener(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
listener = Listener((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
listener = Listener((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
listener = Listener((self._ipc_address, self._ipc_port))
self.is_ipc_alive = True
while True:
conn = listener.accept()
start_time = time.time()
start_time = time.perf_counter()
self.handle_message(conn, start_time)
print(f"New Connection: {listener.last_accepted}")
listener.close()
def handle_message(self, conn, start_time) -> None:
while True:
msg = conn.recv()
if debug:
@ -59,30 +72,29 @@ class IPCServer:
break
if msg == 'close connection':
conn.close()
break
if msg == 'close server':
if msg in ['close connection', 'close server']:
conn.close()
break
# NOTE: Not perfect but insures we don't lock up the connection for too long.
end_time = time.time()
if (end - start) > self.ipc_timeout:
end_time = time.perf_counter()
if (end_time - start_time) > self._ipc_timeout:
conn.close()
listener.close()
break
def send_ipc_message(self, message: str = "Empty Data...") -> None:
try:
if self._conn_type == "socket":
conn = Client(address=self.ipc_address, family="AF_UNIX", authkey=self.ipc_authkey)
conn = Client(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
conn = Client((self._ipc_address, self._ipc_port), authkey=self._ipc_authkey)
else:
conn = Client((self.ipc_address, self.ipc_port), authkey=self.ipc_authkey)
conn = Client((self._ipc_address, self._ipc_port))
conn.send(message)
conn.send('close connection')
conn.close()
except ConnectionRefusedError as e:
print("Connection refused...")
except Exception as e:
print(repr(e))

View File

@ -5,10 +5,6 @@ import os, logging
class Logger:
def __init__(self, config_path: str):
self._CONFIG_PATH = config_path
def get_logger(self, loggerName: str = "NO_LOGGER_NAME_PASSED", createFile: bool = True) -> logging.Logger:
"""
Create a new logging object and return it.
:note:
@ -26,18 +22,22 @@ class Logger:
:return: the logging object we created
"""
globalLogLvl = logging.DEBUG # Keep this at highest so that handlers can filter to their desired levels
chLogLevel = logging.CRITICAL # Prety musch the only one we change ever
fhLogLevel = logging.DEBUG
def __init__(self, config_path: str, _ch_log_lvl = logging.CRITICAL, _fh_log_lvl = logging.INFO):
self._CONFIG_PATH = config_path
self.global_lvl = logging.DEBUG # Keep this at highest so that handlers can filter to their desired levels
self.ch_log_lvl = _ch_log_lvl # Prety much the only one we ever change
self.fh_log_lvl = _fh_log_lvl
def get_logger(self, loggerName: str = "NO_LOGGER_NAME_PASSED", createFile: bool = True) -> logging.Logger:
log = logging.getLogger(loggerName)
log.setLevel(globalLogLvl)
log.setLevel(self.global_lvl)
# Set our log output styles
fFormatter = logging.Formatter('[%(asctime)s] %(pathname)s:%(lineno)d %(levelname)s - %(message)s', '%m-%d %H:%M:%S')
cFormatter = logging.Formatter('%(pathname)s:%(lineno)d] %(levelname)s - %(message)s')
ch = logging.StreamHandler()
ch.setLevel(level=chLogLevel)
ch.setLevel(level=self.ch_log_lvl)
ch.setFormatter(cFormatter)
log.addHandler(ch)
@ -49,7 +49,7 @@ class Logger:
os.mkdir(folder)
fh = logging.FileHandler(file)
fh.setLevel(level=fhLogLevel)
fh.setLevel(level=self.fh_log_lvl)
fh.setFormatter(fFormatter)
log.addHandler(fh)