j49_c commited on
Commit
59b4e09
·
1 Parent(s): 21e0093

add auto-recovery and self-ping system

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py CHANGED
@@ -991,6 +991,43 @@ def admin_quickstats():
991
  }})
992
 
993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994
  if __name__ == "__main__":
 
 
 
995
  port = int(os.environ.get("SERVER_PORT", 30170))
996
  app.run(host="0.0.0.0", port=port)
 
991
  }})
992
 
993
 
994
+ def run_keep_alive():
995
+ """Background thread to keep the server awake and monitor bots."""
996
+ port = int(os.environ.get("SERVER_PORT", 30170))
997
+ url = f"http://127.0.0.1:{port}/"
998
+
999
+ print(f"[*] Starting Keep-Alive and Auto-Recovery system...")
1000
+
1001
+ while True:
1002
+ try:
1003
+ # 1. Self-ping to prevent sleep
1004
+ requests.get(url, timeout=10)
1005
+
1006
+ # 2. Check and recover crashed bots (Auto-Recovery)
1007
+ # This is a basic safety mechanism to restart nodes if they die unexpectedly
1008
+ with lock:
1009
+ for key, state in list(server_states.items()):
1010
+ if state == "Running" and key not in running_procs:
1011
+ # Found a bot that should be running but the process is missing
1012
+ print(f"[!] Auto-Recovery: Restarting crashed node '{key}'")
1013
+ try:
1014
+ owner, folder = parse_server_key(key, allow_admin=True)
1015
+ meta = read_meta(owner, folder)
1016
+ startup = meta.get("startup_file")
1017
+ if startup:
1018
+ t = threading.Thread(target=background_start, args=(key, owner, folder, startup), daemon=True)
1019
+ t.start()
1020
+ except Exception as re:
1021
+ print(f"[!] Recovery failed for '{key}': {re}")
1022
+
1023
+ except Exception as e:
1024
+ print(f"[!] Keep-Alive Error: {e}")
1025
+
1026
+ time.sleep(300) # Check every 5 minutes
1027
+
1028
  if __name__ == "__main__":
1029
+ # Start the safety/keep-alive thread
1030
+ threading.Thread(target=run_keep_alive, daemon=True).start()
1031
+
1032
  port = int(os.environ.get("SERVER_PORT", 30170))
1033
  app.run(host="0.0.0.0", port=port)