import time
import threading
from models import Server
from modules.system_monitor import fetch_server_metrics

def start_background_monitor(interval_seconds=60):
    def run():
        while True:
            try:
                servers = Server.get_all()
                for s in servers:
                    fetch_server_metrics(s)
            except Exception as e:
                pass
            time.sleep(interval_seconds)

    t = threading.Thread(target=run, daemon=True)
    t.start()
