58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
import json
|
|
import time
|
|
from pathlib import Path
|
|
from networkx.drawing.nx_agraph import write_dot
|
|
|
|
BATCH_DIR = Path(__file__).resolve().parent
|
|
JOBS_DIR = BATCH_DIR / "jobs"
|
|
STATUS_FILE = BATCH_DIR / "status.json"
|
|
|
|
def charger_status():
|
|
if STATUS_FILE.exists():
|
|
return json.loads(STATUS_FILE.read_text())
|
|
return {}
|
|
|
|
def sauvegarder_status(data):
|
|
STATUS_FILE.write_text(json.dumps(data, indent=2))
|
|
|
|
def statut_utilisateur(login):
|
|
status = charger_status()
|
|
entry = status.get(login)
|
|
if not entry:
|
|
return {"statut": None, "position": None, "telechargement": None,
|
|
"message": "Aucune tâche en cours."}
|
|
|
|
if entry["status"] == "en attente":
|
|
return {"statut": "en attente", "position": entry.get("position"),
|
|
"telechargement": None,
|
|
"message": f"En attente (position {entry.get('position', '?')})."}
|
|
|
|
if entry["status"] == "en cours":
|
|
return {"statut": "en cours", "position": 0,
|
|
"telechargement": None, "message": "Analyse en cours."}
|
|
|
|
if entry["status"] == "terminé":
|
|
result_file = JOBS_DIR / f"{login}.result.txt"
|
|
if result_file.exists():
|
|
return {"statut": "terminé", "position": None,
|
|
"telechargement": result_file.read_text(),
|
|
"message": "Analyse terminée. Télécharger le résultat."}
|
|
|
|
if entry["status"] == "échoué":
|
|
return {"statut": "échoué", "position": None, "telechargement": None,
|
|
"message": f"Échec : {entry.get('error', 'erreur inconnue')}"}
|
|
|
|
def soumettre_batch(login, G):
|
|
if statut_utilisateur(login)["statut"]:
|
|
raise RuntimeError("Un batch est déjà en cours.")
|
|
write_dot(G, JOBS_DIR / f"{login}.dot")
|
|
status = charger_status()
|
|
status[login] = {"status": "en attente", "timestamp": time.time()}
|
|
sauvegarder_status(status)
|
|
|
|
def nettoyage_post_telechargement(login):
|
|
(JOBS_DIR / f"{login}.dot").unlink(missing_ok=True)
|
|
(JOBS_DIR / f"{login}.result.txt").unlink(missing_ok=True)
|
|
status = charger_status()
|
|
status.pop(login, None)
|
|
sauvegarder_status(status) |