diff --git a/app/main.py b/app/main.py index 8a44780..6b06ca0 100644 --- a/app/main.py +++ b/app/main.py @@ -11,6 +11,7 @@ DB_FILE = Path("/opt/appfactory/data/appfactory/appfactory.db") CHECK_INTERVAL_SECONDS = int(os.getenv("APPFACTORY_MONITOR_INTERVAL_SECONDS", "30")) REQUEST_TIMEOUT_SECONDS = int(os.getenv("APPFACTORY_MONITOR_TIMEOUT_SECONDS", "5")) +HEALTH_RETENTION_DAYS = int(os.getenv("APPFACTORY_HEALTH_RETENTION_DAYS", "7")) CORE_SERVICES = { "appfactory-portal": "http://appfactory-portal:9100/health", @@ -54,6 +55,21 @@ def get_app_services(): return services +def cleanup_old_health_records(): + con = get_connection() + + con.execute( + """ + DELETE FROM service_health + WHERE checked_at < datetime('now', ?) + """, + (f"-{HEALTH_RETENTION_DAYS} days",), + ) + + con.commit() + con.close() + + def save_health_result( service_id: str, status: str, @@ -129,6 +145,8 @@ def check_service(service_id: str, url: str): def run_check_cycle(): + cleanup_old_health_records() + services = {} services.update(CORE_SERVICES) services.update(get_app_services()) @@ -170,6 +188,7 @@ def health(): "status": "ok", "interval_seconds": CHECK_INTERVAL_SECONDS, "timeout_seconds": REQUEST_TIMEOUT_SECONDS, + "retention_days": HEALTH_RETENTION_DAYS, }