Spaces:
Paused
Paused
Fix: Add local file persistence to JobRegistry to prevent 404s after space restart
Browse files- backend/core/job_registry.py +26 -1
backend/core/job_registry.py
CHANGED
|
@@ -1,10 +1,32 @@
|
|
| 1 |
import threading
|
|
|
|
|
|
|
|
|
|
| 2 |
from typing import Dict, Any
|
| 3 |
|
|
|
|
|
|
|
| 4 |
class JobRegistry:
|
| 5 |
-
def __init__(self):
|
| 6 |
self._jobs: Dict[str, Dict[str, Any]] = {}
|
| 7 |
self._lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def create_job(self, job_id: str, initial_data: Dict[str, Any] = None):
|
| 10 |
with self._lock:
|
|
@@ -14,11 +36,13 @@ class JobRegistry:
|
|
| 14 |
"results": None,
|
| 15 |
**(initial_data or {})
|
| 16 |
}
|
|
|
|
| 17 |
|
| 18 |
def update_job(self, job_id: str, **kwargs):
|
| 19 |
with self._lock:
|
| 20 |
if job_id in self._jobs:
|
| 21 |
self._jobs[job_id].update(kwargs)
|
|
|
|
| 22 |
|
| 23 |
def get_job(self, job_id: str) -> Dict[str, Any]:
|
| 24 |
with self._lock:
|
|
@@ -28,6 +52,7 @@ class JobRegistry:
|
|
| 28 |
with self._lock:
|
| 29 |
if job_id in self._jobs:
|
| 30 |
del self._jobs[job_id]
|
|
|
|
| 31 |
|
| 32 |
def list_jobs(self) -> Dict[str, Dict[str, Any]]:
|
| 33 |
with self._lock:
|
|
|
|
| 1 |
import threading
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
from typing import Dict, Any
|
| 6 |
|
| 7 |
+
logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
class JobRegistry:
|
| 10 |
+
def __init__(self, storage_file: str = "/tmp/job_registry.json"):
|
| 11 |
self._jobs: Dict[str, Dict[str, Any]] = {}
|
| 12 |
self._lock = threading.Lock()
|
| 13 |
+
self.storage_file = storage_file
|
| 14 |
+
self._load()
|
| 15 |
+
|
| 16 |
+
def _save(self):
|
| 17 |
+
try:
|
| 18 |
+
with open(self.storage_file, "w") as f:
|
| 19 |
+
json.dump(self._jobs, f)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
logger.error(f"Failed to save job registry to disk: {e}")
|
| 22 |
+
|
| 23 |
+
def _load(self):
|
| 24 |
+
if os.path.exists(self.storage_file):
|
| 25 |
+
try:
|
| 26 |
+
with open(self.storage_file, "r") as f:
|
| 27 |
+
self._jobs = json.load(f)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Failed to load job registry from disk: {e}")
|
| 30 |
|
| 31 |
def create_job(self, job_id: str, initial_data: Dict[str, Any] = None):
|
| 32 |
with self._lock:
|
|
|
|
| 36 |
"results": None,
|
| 37 |
**(initial_data or {})
|
| 38 |
}
|
| 39 |
+
self._save()
|
| 40 |
|
| 41 |
def update_job(self, job_id: str, **kwargs):
|
| 42 |
with self._lock:
|
| 43 |
if job_id in self._jobs:
|
| 44 |
self._jobs[job_id].update(kwargs)
|
| 45 |
+
self._save()
|
| 46 |
|
| 47 |
def get_job(self, job_id: str) -> Dict[str, Any]:
|
| 48 |
with self._lock:
|
|
|
|
| 52 |
with self._lock:
|
| 53 |
if job_id in self._jobs:
|
| 54 |
del self._jobs[job_id]
|
| 55 |
+
self._save()
|
| 56 |
|
| 57 |
def list_jobs(self) -> Dict[str, Dict[str, Any]]:
|
| 58 |
with self._lock:
|