db_cleanup module

This commit is contained in:
Mateusz Gruszczyński
2026-06-05 11:47:55 +02:00
parent 7bf24e39f9
commit 9ee65cbf07
6 changed files with 158 additions and 7 deletions
+9 -5
View File
@@ -22,8 +22,8 @@ from flask import Blueprint, jsonify, request, abort, send_file, redirect, Respo
# Note: url_for is exported through this shared module for API routes that build temporary in-app links.
from ..config import DB_PATH, JOBS_RETENTION_DAYS, SMART_QUEUE_HISTORY_RETENTION_DAYS, LOG_RETENTION_DAYS, WORKERS, PYTORRENT_TMP_DIR
from ..db import connect, utcnow
from ..services.auth import current_user_id as default_user_id, current_user, list_users, save_user, delete_user, login_user, logout_user, enabled as auth_enabled, require_profile_write
from ..services import preferences, rtorrent, torrent_stats, speed_peaks, tracker_cache, rss as rss_service, ratio_rules, backup as backup_service, download_planner, operation_logs, poller_control
from ..services.auth import current_user_id as default_user_id, current_user, list_users, save_user, delete_user, login_user, logout_user, enabled as auth_enabled, require_profile_write, require_admin, is_admin
from ..services import preferences, rtorrent, torrent_stats, speed_peaks, tracker_cache, rss as rss_service, ratio_rules, backup as backup_service, download_planner, operation_logs, poller_control, database_maintenance
from ..services.torrent_cache import torrent_cache
from ..services.torrent_summary import cached_summary
from ..services.workers import enqueue, list_jobs, cancel_job, retry_job, force_job, clear_jobs, emergency_clear_jobs
@@ -260,10 +260,13 @@ def _table_count(table: str, where: str = "", params: tuple = ()) -> int:
def _db_size() -> dict:
try:
size = DB_PATH.stat().st_size if DB_PATH.exists() else 0
return {"path": str(DB_PATH), "size": size, "size_h": rtorrent.human_size(size)}
return database_maintenance.database_status()
except Exception as exc:
return {"path": str(DB_PATH), "size": 0, "size_h": "0 B", "error": str(exc)}
try:
size = DB_PATH.stat().st_size if DB_PATH.exists() else 0
except Exception:
size = 0
return {"path": str(DB_PATH), "size": size, "size_h": rtorrent.human_size(size), "error": str(exc)}
def _active_profile_cache_summary(profile_id: int | None = None) -> dict:
@@ -312,6 +315,7 @@ def cleanup_summary() -> dict:
"operation_logs": operation_logs.retention_label(operation_log_retention),
},
"database": _db_size(),
"admin": is_admin(current_user()),
}
def active_default_download_path(profile: dict | None) -> str:
+11
View File
@@ -203,6 +203,17 @@ def cleanup_jobs():
return ok({"deleted": deleted, "cleanup": cleanup_summary()})
@bp.post("/cleanup/database/vacuum")
def cleanup_database_vacuum():
require_admin()
data = request.get_json(silent=True) or {}
try:
result = database_maintenance.vacuum_database(force=bool(data.get("force")))
return ok({"vacuum": result, "cleanup": cleanup_summary()})
except Exception as exc:
return jsonify({"ok": False, "error": str(exc), "cleanup": cleanup_summary()}), 400
@bp.post("/cleanup/smart-queue")
def cleanup_smart_queue():