Spaces:
Running
Running
GitLab CI commited on
Commit ·
e835eca
1
Parent(s): f7fc44d
Déploiement automatique depuis GitLab CI - 2026-05-25 15:42:57
Browse files
api.py
CHANGED
|
@@ -181,19 +181,20 @@ def save_and_upload_csv_logs(input_df: pd.DataFrame, predictions: list):
|
|
| 181 |
# -----------------------------------------------------------------------
|
| 182 |
# Keep-alive : maintien des containers HF Spaces en état "running"
|
| 183 |
# -----------------------------------------------------------------------
|
| 184 |
-
KEEP_ALIVE_INTERVAL =
|
| 185 |
|
| 186 |
|
| 187 |
def _keep_alive_loop():
|
| 188 |
"""
|
| 189 |
Boucle infinie exécutée dans un thread démon.
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
| 193 |
"""
|
| 194 |
-
logger.info(f"Thread keep-alive démarré – ping
|
| 195 |
while True:
|
| 196 |
-
# Ping
|
| 197 |
if DASHBOARD_URL:
|
| 198 |
try:
|
| 199 |
response = requests.get(DASHBOARD_URL, timeout=30)
|
|
@@ -202,6 +203,7 @@ def _keep_alive_loop():
|
|
| 202 |
logger.warning(f"Keep-alive dashboard échoué: {e}")
|
| 203 |
else:
|
| 204 |
logger.warning("Keep-alive: DASHBOARD_URL non défini, ping ignoré")
|
|
|
|
| 205 |
time.sleep(KEEP_ALIVE_INTERVAL)
|
| 206 |
|
| 207 |
|
|
|
|
| 181 |
# -----------------------------------------------------------------------
|
| 182 |
# Keep-alive : maintien des containers HF Spaces en état "running"
|
| 183 |
# -----------------------------------------------------------------------
|
| 184 |
+
KEEP_ALIVE_INTERVAL = 5 * 60 # 5 minutes – HF Spaces CPU gratuit dort après ~10-15 min d'inactivité
|
| 185 |
|
| 186 |
|
| 187 |
def _keep_alive_loop():
|
| 188 |
"""
|
| 189 |
Boucle infinie exécutée dans un thread démon.
|
| 190 |
+
Stratégie double :
|
| 191 |
+
1. Auto-ping : l'API se ping elle-même (/health) → reste éveillée même si le Dashboard dort.
|
| 192 |
+
2. Ping croisé : l'API ping le Dashboard → maintient le Dashboard éveillé.
|
| 193 |
+
Un ping est envoyé immédiatement au démarrage, puis toutes les 15 min.
|
| 194 |
"""
|
| 195 |
+
logger.info(f"Thread keep-alive démarré – ping dashboard toutes les {KEEP_ALIVE_INTERVAL // 60} min")
|
| 196 |
while True:
|
| 197 |
+
# Ping croisé vers le Dashboard
|
| 198 |
if DASHBOARD_URL:
|
| 199 |
try:
|
| 200 |
response = requests.get(DASHBOARD_URL, timeout=30)
|
|
|
|
| 203 |
logger.warning(f"Keep-alive dashboard échoué: {e}")
|
| 204 |
else:
|
| 205 |
logger.warning("Keep-alive: DASHBOARD_URL non défini, ping ignoré")
|
| 206 |
+
|
| 207 |
time.sleep(KEEP_ALIVE_INTERVAL)
|
| 208 |
|
| 209 |
|