diff --git a/anime/router/commands.py b/anime/router/commands.py index 9028b590212f6a4b0be9ddd269310d515f4a9ece..de7348a593e69b6b6486e2ad80c3c5b8a010c789 100644 --- a/anime/router/commands.py +++ b/anime/router/commands.py @@ -9,16 +9,6 @@ from anime import dtos router = APIRouter() -def is_pid_alive(pid: int) -> bool: - if pid: - try: - os.kill(pid, 0) - except OSError: - return False - else: - return True - - @router.get("/can-start-watching") def can_start_watching(request: Request) -> None: if not request.app.state.anime_dir: @@ -39,8 +29,17 @@ def start_watching(request: Request) -> None: awatch = shutil.which("awatch") if awatch is None: raise Exception("awatch command is not available") - ret = subprocess.Popen( + pidof = subprocess.Popen( + ["pidof", "awatch"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + if pidof.wait() == 0: + raise HTTPException(status_code=400, detail="awatch is already running") + subprocess.Popen( [awatch], cwd=anime_dir, + shell=True, + stderr=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, ) - request.app.state.pid = ret.pid