Quasar-Executo / space_keepalive_v2.py
KarlQuant's picture
Add cross-Space keep-alive for BTCUSD and V100
5ebf091
Raw History Blame Contribute Delete
3.1 kB
"""
K1RL QUASAR — Cross-Space Keep-Alive v2 (runs inside Executo)
==============================================================================
Executo never sleeps (asset Spaces hold WebSocket connections into it), so it
is the right place to keep the asset Spaces awake. Every PING_INTERVAL seconds
it sends a real external GET to each target Space's public *.hf.space URL —
inbound traffic from outside the target container, which is what resets the
HuggingFace inactivity timer. A Space's own self-ping is not a reliable
substitute.
Targets: KEEPALIVE_TARGETS env var (comma-separated base URLs) overrides the
defaults below. Set it in Executo → Settings → Variables and secrets.
"""
import os
import sys
import time
import logging
import urllib.request
import urllib.error
from concurrent.futures import ThreadPoolExecutor
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [KEEPALIVE] %(levelname)s: %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger("space_keepalive")
DEFAULT_TARGETS = [
"https://quantcoms-k1rl-quasar-btcusd.hf.space",
"https://quantcoms-k1rl-quasar-v100.hf.space",
]
TARGETS = [
t.strip().rstrip("/")
for t in os.environ.get("KEEPALIVE_TARGETS", ",".join(DEFAULT_TARGETS)).split(",")
if t.strip()
]
PING_INTERVAL = int(os.environ.get("KEEPALIVE_INTERVAL", "300")) # 5 min
PATHS = ("/health", "/") # /health first; "/" as a fallback
TIMEOUT = 30 # a waking Space can take a while to answer
UA = "QuasarExecutoKeepAlive/1.0"
failures = {t: 0 for t in TARGETS}
def ping(base: str) -> None:
last_err = None
for path in PATHS:
url = base + path
try:
req = urllib.request.Request(url, headers={"User-Agent": UA})
with urllib.request.urlopen(req, timeout=TIMEOUT) as r:
code = r.status
if 200 <= code < 400:
if failures[base]:
log.info(f"RECOVERED {url} -> {code} after {failures[base]} failures")
else:
log.info(f"OK {url} -> {code}")
failures[base] = 0
return
last_err = f"HTTP {code}"
except urllib.error.HTTPError as e:
last_err = f"HTTP {e.code}"
except Exception as e:
last_err = repr(e)
failures[base] += 1
lvl = logging.ERROR if failures[base] >= 3 else logging.WARNING
log.log(lvl, f"FAIL {base} ({last_err}) consecutive={failures[base]}"
+ (" — Space is likely asleep, paused or crashed" if failures[base] >= 3 else ""))
def main() -> None:
log.info(f"Started | interval={PING_INTERVAL}s | targets={TARGETS}")
time.sleep(20) # let the hub come up first
with ThreadPoolExecutor(max_workers=max(1, len(TARGETS))) as pool:
while True:
try:
list(pool.map(ping, TARGETS))
except Exception as e:
log.error(f"loop error: {e!r}")
time.sleep(PING_INTERVAL)
if __name__ == "__main__":
main()