added dirty start check

This commit is contained in:
itdominator 2022-10-22 23:26:13 -05:00
parent 39c1306aae
commit 1e7ad1ea34
5 changed files with 70 additions and 26 deletions

View File

@ -36,6 +36,13 @@ if __name__ == "__main__":
# Read arguments (If any...)
args, unknownargs = parser.parse_known_args()
if args.debug == "true":
settings.set_debug(True)
if args.trace_debug == "true":
settings.set_trace_debug(True)
settings.do_dirty_start_check()
Application(args, unknownargs)
Gtk.main()
except Exception as e:

View File

@ -6,7 +6,6 @@ import time
# Application imports
from utils.ipc_server import IPCServer
from utils.settings import Settings
from core.window import Window
@ -23,23 +22,18 @@ class Application(IPCServer):
def __init__(self, args, unknownargs):
super(Application, self).__init__()
if args.debug == "true":
settings.set_debug(True)
if args.trace_debug == "true":
settings.set_trace_debug(True)
if not settings.is_trace_debug():
try:
self.create_ipc_listener()
time.sleep(0.05)
except Exception:
...
if not self.is_ipc_alive:
if unknownargs:
for arg in unknownargs:
for arg in unknownargs + [args.new_tab,]:
if os.path.isdir(arg):
message = f"FILE|{arg}"
self.send_ipc_message(message)
raise AppLaunchException(f"IPC Server Exists: Will send path(s) to it and close...\nNote: If no fm exists, remove /tmp/{app_name}-ipc.sock")
raise AppLaunchException(f"{app_name} IPC Server Exists: Will send path(s) to it and close...")
Window(args, unknownargs)

View File

@ -72,5 +72,6 @@ class Window(Gtk.ApplicationWindow):
def _tear_down(self, widget=None, eve=None):
settings.clear_pid()
time.sleep(event_sleep_time)
Gtk.main_quit()

View File

@ -10,7 +10,7 @@ from multiprocessing.connection import Listener, Client
class IPCServer:
""" Create a listener so that other SolarFM instances send requests back to existing instance. """
""" Create a listener so that other {app_name} 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
@ -35,11 +35,10 @@ class IPCServer:
event_system.subscribe("post_file_to_ipc", self.send_ipc_message)
@daemon_threaded
def create_ipc_listener(self) -> None:
if self._conn_type == "socket":
if os.path.exists(self._ipc_address):
return
if os.path.exists(self._ipc_address) and settings.is_dirty_start():
os.unlink(self._ipc_address)
listener = Listener(address=self._ipc_address, family="AF_UNIX", authkey=self._ipc_authkey)
elif "unsecured" not in self._conn_type:
@ -49,17 +48,21 @@ class IPCServer:
self.is_ipc_alive = True
self._run_ipc_loop(listener)
@daemon_threaded
def _run_ipc_loop(self, listener) -> None:
while True:
conn = listener.accept()
start_time = time.perf_counter()
self.handle_message(conn, start_time)
self.handle_ipc_message(conn, start_time)
listener.close()
def handle_message(self, conn, start_time) -> None:
def handle_ipc_message(self, conn, start_time) -> None:
while True:
msg = conn.recv()
if debug:
if settings.is_debug():
print(msg)
if "FILE|" in msg:

View File

@ -21,6 +21,7 @@ class Settings:
self._KEY_BINDINGS_FILE = f"{self._CONFIG_PATH}/key-bindings.json"
self._CSS_FILE = f"{self._CONFIG_PATH}/stylesheet.css"
self._DEFAULT_ICONS = f"{self._CONFIG_PATH}/icons"
self._PID_FILE = f"{self._CONFIG_PATH}/{app_name.lower()}.pid"
self._WINDOW_ICON = f"{self._DEFAULT_ICONS}/{app_name.lower()}.png"
self._USR_PATH = f"/usr/share/{app_name.lower()}"
@ -61,11 +62,43 @@ class Settings:
self._trace_debug = False
self._debug = False
self._dirty_start = False
def get_builder(self) -> any: return self._builder
def set_builder(self, builder) -> any: self._builder = builder
def get_glade_file(self) -> str: return self._GLADE_FILE
def do_dirty_start_check(self):
if not os.path.exists(self._PID_FILE):
self._write_new_pid()
else:
with open(self._PID_FILE, "r") as _pid:
pid = _pid.readline().strip()
if pid not in ("", None):
self._check_alive_status(int(pid))
else:
self._write_new_pid()
""" Check For the existence of a unix pid. """
def _check_alive_status(self, pid):
print(f"PID Found: {pid}")
try:
os.kill(pid, 0)
except OSError:
print(f"{app_name} is starting dirty...")
self._dirty_start = True
self._write_new_pid()
return
print("PID is alive... Let downstream errors (sans debug args) handle app closure propigation.")
def _write_new_pid(self):
pid = os.getpid()
self._write_pid(pid)
def _clean_pid(self):
os.unlink(self._PID_FILE)
def _write_pid(self, pid):
with open(self._PID_FILE, "w") as _pid:
_pid.write(f"{pid}")
def register_signals_to_builder(self, classes=None):
handlers = {}
@ -80,6 +113,11 @@ class Settings:
self._builder.connect_signals(handlers)
def get_builder(self) -> any: return self._builder
def set_builder(self, builder) -> any: self._builder = builder
def get_glade_file(self) -> str: return self._GLADE_FILE
def get_logger(self) -> Logger: return self._logger
def get_plugins_path(self) -> str: return self._PLUGINS_PATH
def get_icon_theme(self) -> str: return self._ICON_THEME
@ -101,7 +139,8 @@ class Settings:
def is_trace_debug(self) -> str: return self._trace_debug
def is_debug(self) -> str: return self._debug
def is_dirty_start(self) -> bool: return self._dirty_start
def clear_pid(self): self._clean_pid()
def set_trace_debug(self, trace_debug):
self._trace_debug = trace_debug