From 5419ec0bf189eec9f8494e01b15ac9e3b98513b6 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sun, 12 May 2024 13:07:06 +0200
Subject: [PATCH] Fixed multiple awatches.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 anime/router/commands.py | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/anime/router/commands.py b/anime/router/commands.py
index 9028b59..de7348a 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
-- 
GitLab