fix circular imports

This commit is contained in:
Thephaseless 2024-12-13 22:43:29 +00:00
parent 709c9b6ef6
commit f325f5d8a7
3 changed files with 45 additions and 36 deletions

View File

@ -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)

View File

@ -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

View File

@ -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