2024-09-04 05:43:59 +00:00
|
|
|
# Python imports
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import inspect
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StartCheckMixin:
|
|
|
|
def is_dirty_start(self) -> bool:
|
|
|
|
return self._dirty_start
|
|
|
|
|
|
|
|
def clear_pid(self):
|
|
|
|
if not self.is_trace_debug():
|
|
|
|
self._clean_pid()
|
|
|
|
|
|
|
|
def do_dirty_start_check(self):
|
|
|
|
if self.is_trace_debug():
|
|
|
|
pid = os.getpid()
|
|
|
|
self._print_pid(pid)
|
|
|
|
return
|
|
|
|
|
|
|
|
if os.path.exists(self._PID_FILE):
|
|
|
|
with open(self._PID_FILE, "r") as f:
|
|
|
|
pid = f.readline().strip()
|
|
|
|
if pid not in ("", None):
|
|
|
|
if self.is_pid_alive( int(pid) ):
|
|
|
|
print("PID file exists and PID is alive... Letting downstream errors (sans debug args) handle app closure propigation.")
|
|
|
|
return
|
|
|
|
|
|
|
|
self._write_new_pid()
|
|
|
|
|
|
|
|
""" Check For the existence of a unix pid. """
|
|
|
|
def is_pid_alive(self, pid):
|
|
|
|
print(f"PID Found: {pid}")
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
2024-09-04 05:59:52 +00:00
|
|
|
print(f"{APP_NAME} PID file exists but PID is irrelevant; starting dirty...")
|
2024-09-04 05:43:59 +00:00
|
|
|
self._dirty_start = True
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _write_new_pid(self):
|
2024-09-04 05:59:52 +00:00
|
|
|
pid = os.getpid()
|
|
|
|
self.pid = pid
|
2024-09-04 05:43:59 +00:00
|
|
|
self._write_pid(pid)
|
|
|
|
self._print_pid(pid)
|
|
|
|
|
|
|
|
def _print_pid(self, pid):
|
2024-09-04 05:59:52 +00:00
|
|
|
print(f"{APP_NAME} PID: {pid}")
|
2024-09-04 05:43:59 +00:00
|
|
|
|
|
|
|
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}")
|