Spaces:
Build error
Build error
Create health_ping.py
Browse files- health_ping.py +76 -0
health_ping.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
+
from config import config
|
| 7 |
+
|
| 8 |
+
class HealthPinger:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.running = False
|
| 11 |
+
self.thread = None
|
| 12 |
+
self.last_ping = None
|
| 13 |
+
self.ping_count = 0
|
| 14 |
+
self.space_url = config.SPACE_URL or os.getenv('SPACE_HOST', '')
|
| 15 |
+
self.interval = config.PING_INTERVAL
|
| 16 |
+
|
| 17 |
+
def start(self):
|
| 18 |
+
"""Start the health ping service"""
|
| 19 |
+
if self.running:
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
self.running = True
|
| 23 |
+
self.thread = threading.Thread(target=self._ping_loop, daemon=True)
|
| 24 |
+
self.thread.start()
|
| 25 |
+
print(f"Health pinger started - Interval: {self.interval}s")
|
| 26 |
+
|
| 27 |
+
def stop(self):
|
| 28 |
+
"""Stop the health ping service"""
|
| 29 |
+
self.running = False
|
| 30 |
+
if self.thread:
|
| 31 |
+
self.thread.join(timeout=5)
|
| 32 |
+
|
| 33 |
+
def _ping_loop(self):
|
| 34 |
+
"""Main ping loop"""
|
| 35 |
+
while self.running:
|
| 36 |
+
try:
|
| 37 |
+
self._do_ping()
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Ping error: {e}")
|
| 40 |
+
|
| 41 |
+
time.sleep(self.interval)
|
| 42 |
+
|
| 43 |
+
def _do_ping(self):
|
| 44 |
+
"""Perform a health check ping"""
|
| 45 |
+
urls_to_ping = []
|
| 46 |
+
|
| 47 |
+
# Add Space URL if available
|
| 48 |
+
if self.space_url:
|
| 49 |
+
if not self.space_url.startswith('http'):
|
| 50 |
+
self.space_url = f"https://{self.space_url}"
|
| 51 |
+
urls_to_ping.append(self.space_url)
|
| 52 |
+
|
| 53 |
+
# Also ping localhost to keep internal services warm
|
| 54 |
+
urls_to_ping.append(f"http://localhost:{config.WEB_PORT}/api/health")
|
| 55 |
+
|
| 56 |
+
for url in urls_to_ping:
|
| 57 |
+
try:
|
| 58 |
+
response = requests.get(url, timeout=30)
|
| 59 |
+
if response.status_code == 200:
|
| 60 |
+
self.last_ping = datetime.now()
|
| 61 |
+
self.ping_count += 1
|
| 62 |
+
except requests.RequestException:
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
def get_status(self):
|
| 66 |
+
"""Get pinger status"""
|
| 67 |
+
return {
|
| 68 |
+
'running': self.running,
|
| 69 |
+
'last_ping': self.last_ping.isoformat() if self.last_ping else None,
|
| 70 |
+
'ping_count': self.ping_count,
|
| 71 |
+
'interval': self.interval,
|
| 72 |
+
'target_url': self.space_url
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Global instance
|
| 76 |
+
health_pinger = HealthPinger()
|