CoolFace
Apppublic

BluefxPraise/The_Oracle

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
keep_alive.py49 linesDownload Raw Back to utils
1"""Keep-alive: self-ping web server + Telegram heartbeat to admin."""2 3import threading4import time5import logging6import requests7 8from config import SPACE_URL, FLASK_PORT, ADMIN_CHAT_ID, TELEGRAM_BOT_TOKEN9 10log = logging.getLogger("oracle.keepalive")11 12 13def _self_ping_loop():14    while True:15        try:16            url = SPACE_URL.rstrip("/") + "/ping"17            r = requests.get(url, timeout=10)18            log.info("self-ping %s -> %s", url, r.status_code)19        except Exception as e:20            try:21                requests.get(f"http://127.0.0.1:{FLASK_PORT}/ping", timeout=5)22            except Exception:23                log.warning("self-ping failed: %s", e)24        time.sleep(30 * 60)25 26 27def _heartbeat_loop():28    while True:29        try:30            url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"31            requests.post(32                url,33                json={34                    "chat_id": ADMIN_CHAT_ID,35                    "text": f"๐Ÿ’“ Oracle heartbeat โ€” {time.strftime('%Y-%m-%d %H:%M UTC', time.gmtime())}",36                    "disable_notification": True,37                },38                timeout=10,39            )40        except Exception as e:41            log.warning("heartbeat send failed: %s", e)42        time.sleep(15 * 60)43 44 45def start_keep_alive():46    threading.Thread(target=_self_ping_loop, daemon=True, name="self-ping").start()47    threading.Thread(target=_heartbeat_loop, daemon=True, name="heartbeat").start()48    log.info("keep-alive started")49