Delete queue_manager.py
Browse files- queue_manager.py +0 -65
queue_manager.py
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 1 |
-
# queue_manager.py
|
| 2 |
-
import os
|
| 3 |
-
import threading
|
| 4 |
-
import queue
|
| 5 |
-
import time
|
| 6 |
-
from typing import Dict, Any
|
| 7 |
-
from models_job import JobStatus, JobResult
|
| 8 |
-
|
| 9 |
-
UPLOAD_DIR = os.environ.get("UPLOAD_DIR", "/app/data/uploads")
|
| 10 |
-
RESULTS_DIR = os.environ.get("RESULTS_DIR", "/app/data/results")
|
| 11 |
-
|
| 12 |
-
class JobStore:
|
| 13 |
-
"""
|
| 14 |
-
Almacena estados y resultados en memoria.
|
| 15 |
-
Para producci贸n: sustituir por Redis / DB persistente si lo necesitas.
|
| 16 |
-
"""
|
| 17 |
-
def __init__(self):
|
| 18 |
-
self.status: Dict[str, JobStatus] = {}
|
| 19 |
-
self.result: Dict[str, JobResult] = {}
|
| 20 |
-
self.lock = threading.Lock()
|
| 21 |
-
|
| 22 |
-
def set_status(self, job_id: str, status: JobStatus):
|
| 23 |
-
with self.lock:
|
| 24 |
-
self.status[job_id] = status
|
| 25 |
-
|
| 26 |
-
def get_status(self, job_id: str) -> JobStatus | None:
|
| 27 |
-
with self.lock:
|
| 28 |
-
return self.status.get(job_id)
|
| 29 |
-
|
| 30 |
-
def set_result(self, job_id: str, result: JobResult):
|
| 31 |
-
with self.lock:
|
| 32 |
-
self.result[job_id] = result
|
| 33 |
-
|
| 34 |
-
def get_result(self, job_id: str) -> JobResult | None:
|
| 35 |
-
with self.lock:
|
| 36 |
-
return self.result.get(job_id)
|
| 37 |
-
|
| 38 |
-
job_store = JobStore()
|
| 39 |
-
job_queue: "queue.Queue[Dict[str, Any]]" = queue.Queue()
|
| 40 |
-
|
| 41 |
-
def worker_loop(process_fn):
|
| 42 |
-
while True:
|
| 43 |
-
job = job_queue.get()
|
| 44 |
-
if job is None:
|
| 45 |
-
break
|
| 46 |
-
try:
|
| 47 |
-
process_fn(job)
|
| 48 |
-
except Exception as e:
|
| 49 |
-
# Marca como failed
|
| 50 |
-
st = job_store.get_status(job["job_id"])
|
| 51 |
-
if st:
|
| 52 |
-
st.status = "failed"
|
| 53 |
-
st.message = f"Error: {e}"
|
| 54 |
-
st.progress = 0
|
| 55 |
-
job_store.set_status(job["job_id"], st)
|
| 56 |
-
finally:
|
| 57 |
-
job_queue.task_done()
|
| 58 |
-
|
| 59 |
-
_worker_thread = None
|
| 60 |
-
|
| 61 |
-
def start_worker(process_fn):
|
| 62 |
-
global _worker_thread
|
| 63 |
-
if _worker_thread is None or not _worker_thread.is_alive():
|
| 64 |
-
_worker_thread = threading.Thread(target=worker_loop, args=(process_fn,), daemon=True)
|
| 65 |
-
_worker_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|