diff --git a/main.py b/main.py index 4b6dfa5..ba722e8 100644 --- a/main.py +++ b/main.py @@ -12,11 +12,10 @@ from sbase import SB, BaseCase import src import src.utils -from src.utils import consts import src.utils.consts from src.models.requests import LinkRequest, LinkResponse, Solution -from src.utils import logger -from src.utils.consts import LOG_LEVEL, kill_chromium_processes +from src.utils import consts, kill_chromium_processes, logger +from src.utils.consts import LOG_LEVEL app = FastAPI(debug=LOG_LEVEL == logging.DEBUG, log_level=LOG_LEVEL) @@ -36,7 +35,7 @@ async def health_check(): health_check_request = read_item( LinkRequest.model_construct(url="https://prowlarr.servarr.com/v1/ping") ) - if consts.MAX_CHROME_LIFETIME>0: + if consts.MAX_CHROME_LIFETIME > 0: kill_chromium_processes() if health_check_request.solution.status != HTTPStatus.OK: diff --git a/src/utils/__init__.py b/src/utils/__init__.py index e0dde19..fc6b8d1 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,8 +1,48 @@ import logging +import time -from src.utils.consts import LOG_LEVEL +import psutil + +from src.utils.consts import LOG_LEVEL, MAX_CHROME_LIFETIME logger = logging.getLogger("uvicorn.error") logger.setLevel(LOG_LEVEL) if len(logger.handlers) == 0: logger.addHandler(logging.StreamHandler()) + + +def kill_chromium_processes(): + # Define the prefix and time threshold + """ + Kill all chromium processes that have been running longer than the specified time threshold. + + This is used to clean up any rogue chromium processes that may be left behind. + """ + prefix = "chromium" + time_threshold = MAX_CHROME_LIFETIME + + # Get the current time + current_time = time.time() + + # Iterate through all processes + for proc in psutil.process_iter(["pid", "name", "create_time"]): + try: + # Extract process details + pid = proc.info["pid"] + name: str = proc.info["name"] + create_time = proc.info["create_time"] + + # Check if the process name starts with the prefix and has been running longer than the threshold + if ( + name + and name.startswith(prefix) + and (current_time - create_time > time_threshold) + ): + logger.info( + f"Terminating process {name} (PID: {pid}) running for {int(current_time - create_time)} seconds" + ) + psutil.Process(pid).terminate() # Terminate the process + + except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): + # Ignore processes that no longer exist or can't be accessed + pass diff --git a/src/utils/consts.py b/src/utils/consts.py index d291964..1656a98 100644 --- a/src/utils/consts.py +++ b/src/utils/consts.py @@ -1,10 +1,5 @@ import logging import os -import time - -import psutil - -from src.utils import logger def get_version_from_env(): @@ -26,38 +21,13 @@ def get_version_from_env(): return version_env.removeprefix("v") -def kill_chromium_processes(): - # Define the prefix and time threshold - prefix = "chromium" - time_threshold = 300 # 5 minutes in seconds - - # Get the current time - current_time = time.time() - - # Iterate through all processes - for proc in psutil.process_iter(['pid', 'name', 'create_time']): - try: - # Extract process details - pid = proc.info['pid'] - name:str = proc.info['name'] - create_time = proc.info['create_time'] - - # Check if the process name starts with the prefix and has been running longer than the threshold - if name and name.startswith(prefix) and (current_time - create_time > time_threshold): - logger.info(f"Terminating process {name} (PID: {pid}) running for {int(current_time - create_time)} seconds") - psutil.Process(pid).terminate() # Terminate the process - - except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): - # Ignore processes that no longer exist or can't be accessed - pass LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO" LOG_LEVEL = logging.getLevelNamesMapping()[LOG_LEVEL.upper()] VERSION = get_version_from_env() or "unknown" -MAX_CHROME_LIFETIME= int(os.getenv("MAX_CHROME_LIFETIME", 300)) - +MAX_CHROME_LIFETIME = int(os.getenv("MAX_CHROME_LIFETIME", "300")) CHALLENGE_TITLES = [ # Cloudflare