From 4e9dd94433b8ed3d61874b67e59b1c230754f8b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Thu, 25 Jun 2026 13:29:06 +0400 Subject: [PATCH] =?UTF-8?q?v1.0.178:=20=D0=BB=D0=BE=D0=B3=D0=B8=20+=20lock?= =?UTF-8?q?-=D1=84=D0=B0=D0=B9=D0=BB=20+=20guard=20=D0=BE=D1=82=20=D0=B4?= =?UTF-8?q?=D0=B2=D0=BE=D0=B9=D0=BD=D0=BE=D0=B3=D0=BE=20classify.=20=D0=9E?= =?UTF-8?q?=D1=82=D0=B2=D0=B5=D1=82=20=D0=9E=D0=BF=D1=83=D1=81=D0=B0:=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=BD=D0=B8=D0=BC=D1=83=D0=BC=20=D1=81=D0=B5=D0=B9?= =?UTF-8?q?=D1=87=D0=B0=D1=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/classify_worker.py | 5 +++++ deploy/convert_server.py | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/deploy/classify_worker.py b/deploy/classify_worker.py index b5a5089..d239177 100644 --- a/deploy/classify_worker.py +++ b/deploy/classify_worker.py @@ -25,9 +25,14 @@ if __name__ == "__main__": sys.exit(1) batch_id = sys.argv[1] + lock_path = f"/tmp/classify_{batch_id}.lock" try: result = classify_batch(batch_id) print(f"classify_worker DONE: {result}") except Exception as e: print(f"classify_worker FAILED: {e}", file=sys.stderr) sys.exit(1) + finally: + # Убрать lock-файл в любом случае + if os.path.exists(lock_path): + os.remove(lock_path) diff --git a/deploy/convert_server.py b/deploy/convert_server.py index 2adb857..08edc9c 100755 --- a/deploy/convert_server.py +++ b/deploy/convert_server.py @@ -252,6 +252,12 @@ class Handler(BaseHTTPRequestHandler): from db import documents as db_docs import subprocess, os + # Guard: если classify для этого batch уже запущен — не запускать повторно + lock_path = f"/tmp/classify_{batch_id}.lock" + if os.path.exists(lock_path): + self._json({"ok": False, "error": "classify already running for this batch"}, 409) + return + # Посчитать сколько файлов — ответить сразу, classify в отдельном процессе pending = db_docs.list_pending(batch_id) total = len(pending) @@ -260,12 +266,22 @@ class Handler(BaseHTTPRequestHandler): return # Запустить classify_worker.py в отдельном процессе + # Лог в /home/naeel/contracts/logs/ вместо DEVNULL + log_dir = "/home/naeel/contracts/logs" + os.makedirs(log_dir, exist_ok=True) + log_path = os.path.join(log_dir, f"classify_{batch_id}.log") + log_file = open(log_path, "w") + + # Создать lock-файл (воркер удалит при завершении) + with open(lock_path, "w") as lf: + lf.write(str(os.getpid())) + worker_path = os.path.join(os.path.dirname(__file__), "classify_worker.py") subprocess.Popen( [sys.executable, worker_path, batch_id], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + stdout=log_file, stderr=subprocess.STDOUT, ) - self._json({"ok": True, "total": total}, 202) + self._json({"ok": True, "total": total, "log": log_path}, 202) # ── POST /apply-groups ───────────────────────────────────────────────