Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions pretext/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,43 @@ def is_active_server(self) -> bool:
"""Returns whether the server represented by this object is active on the provided port"""
try:
p = psutil.Process(self.pid)
if not p.is_running():
log.debug(f"Found entry no longer running {p.pid}; removing entry.")
remove_server_entry(self.path_hash)
return False
if p.status == psutil.STATUS_ZOMBIE:
log.debug(f"Found zombie process {p.pid}; removing entry.")
remove_server_entry(self.path_hash)
return False
for _, _, _, laddr, _, _ in p.net_connections("all"):
if isinstance(laddr, str):
log.debug(
f"BUG: the `pretext view` command encountered an error. Please report this: process {self.pid} with laddr {laddr}"
)
remove_server_entry(self.path_hash)
return False
elif isinstance(laddr.port, int) and laddr.port == self.port:
log.info(f"Found PreTeXt web server at {self.url()}")
return True
else:
log.debug(f"Found process {self.pid} with laddr {laddr}")
log.debug(
f"Found process {self.pid} no longer listening on specified port {self.port}"
)
return False
except psutil.NoSuchProcess:
log.debug(f"Found entry no longer exists {self.pid}; removing entry.")
remove_server_entry(self.path_hash)
return False
if not p.is_running():
log.debug(f"Found entry no longer running {p.pid}; removing entry.")
except psutil.AccessDenied:
log.debug(
f"Was denied access to process {self.pid}; please report. Removing entry."
)
remove_server_entry(self.path_hash)
return False
if p.status == psutil.STATUS_ZOMBIE:
log.debug(f"Found zombie process {p.pid}; removing entry.")
remove_server_entry(self.path_hash)
except Exception as e:
log.debug(f"Unexpected error when accessing process {self.pid}: {e}")
return False
for _, _, _, laddr, _, _ in p.net_connections("all"):
if isinstance(laddr, str):
log.debug(
f"BUG: the `pretext view` command encountered an error. Please report this: process {self.pid} with laddr {laddr}"
)
remove_server_entry(self.path_hash)
return False
elif isinstance(laddr.port, int) and laddr.port == self.port:
log.info(f"Found PreTeXt web server at {self.url()}")
return True
else:
log.debug(f"Found process {self.pid} with laddr {laddr}")
log.debug(
f"Found process {self.pid} no longer listening on specified port {self.port}"
)
return False

def terminate(self) -> None:
"""Attempt to terminate the process described by this info."""
Expand Down