From 11e364b9d84509aa0b9743a5b7efaa5acbe573f3 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Sat, 5 Oct 2024 14:35:58 +0200 Subject: [PATCH] fix screen alive status --- core/screen.py | 2 +- core/stack.py | 13 +++++++++---- ui/interface.py | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/core/screen.py b/core/screen.py index f41b297..1dc2eb3 100644 --- a/core/screen.py +++ b/core/screen.py @@ -53,7 +53,7 @@ def kill(pid): os.kill(pid, signal.SIGTERM) -def exists(pid: int) -> int: +def exists(pid: int) -> bool: command = f"screen -S {str(pid)} -Q select .".split() pop = subprocess.Popen(command, stdout=subprocess.DEVNULL) pop.wait() diff --git a/core/stack.py b/core/stack.py index 8aaebef..55d7e5b 100644 --- a/core/stack.py +++ b/core/stack.py @@ -7,6 +7,7 @@ from typing import Union, List, Optional import psutil from core import utils, screen +from core.screen import Screen from core.vars import logger, PYTHON_EXEC from ui import choices @@ -28,6 +29,7 @@ class Stack: self.port = port self.pid_file = self.path / f".pid" self.pid = self.read_pid() + self.screen_session: Screen = None def read_pid(self) -> Optional[int]: """ @@ -156,7 +158,10 @@ class Stack: :return: True if the service is running, False otherwise. """ - return self.pid is not None and psutil.pid_exists(self.pid) + if self.screen_session and self.pid: + return screen.exists(self.screen_session.pid) + else: + return self.pid is not None and psutil.pid_exists(self.pid) def create_venv(self) -> None: """Create a Python virtual environment for the stack.""" @@ -224,9 +229,9 @@ class Stack: # process = subprocess.Popen(full_cmd, shell=True, preexec_fn=os.setpgrp, # stdout=config.open_file(f"{self.id}-stdout"), # stderr=config.open_file(f"{self.id}-stderr")) - screen_session = screen.create(name=self.id) - screen_session.send(f"'{full_cmd}'") - self.write_pid(screen_session.pid) + self.screen_session = screen.create(name=self.id) + self.screen_session.send(f"'{full_cmd} && screen -wipe'") + self.write_pid(self.screen_session.pid) return else: logger.debug(f"Running command: {full_cmd}") diff --git a/ui/interface.py b/ui/interface.py index fbfc196..0bcf284 100644 --- a/ui/interface.py +++ b/ui/interface.py @@ -15,6 +15,7 @@ def handle_services(action, service): clear_terminal() if service == "back": + update_choices() return service = loaded_services[service] @@ -32,7 +33,8 @@ def handle_services(action, service): elif action == "uninstall": confirmation = choices.are_you_sure.ask() if confirmation: - type_confirmation = questionary.text(f"Please type {service.id} to confirm uninstallation (or type cancel):") + type_confirmation = questionary.text( + f"Please type {service.id} to confirm uninstallation (or type cancel):") value = type_confirmation.ask()