From 0763ecbfefacc39c7cc39f3d84f0d319af48abf3 Mon Sep 17 00:00:00 2001 From: Benjamin Schwarz Date: Tue, 15 Sep 2026 13:25:34 +0200 Subject: [PATCH] =?UTF-8?q?API-Endpunkt=20f=C3=BCr=20Job-Cleanup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Benjamin Schwarz --- lufa/api_v1.py | 8 ++++++++ lufa/repository/api_repository.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lufa/api_v1.py b/lufa/api_v1.py index 303cbd8..1f3a63d 100644 --- a/lufa/api_v1.py +++ b/lufa/api_v1.py @@ -309,3 +309,11 @@ def db_now(): resp = {"db_now": now} return jsonify(resp) + + +@bp.route("/job_cleanup", methods=["POST"]) +@token_required +def job_cleanup(): + """Cleanup jobs older than 89 days.""" + repository = get_api_repository() + repository.job_cleanup(89) diff --git a/lufa/repository/api_repository.py b/lufa/repository/api_repository.py index bfd92e3..af4d0fb 100644 --- a/lufa/repository/api_repository.py +++ b/lufa/repository/api_repository.py @@ -147,6 +147,11 @@ def import_job(self, job_data: dict) -> int: """ pass + @abstractmethod + def job_cleanup(self, keep_days: int = 89): + """Cleanup jobs older than provided days.""" + pass + class SqliteApiRepository(ApiRepository): def __init__(self, db_manager: DatabaseManager) -> None: @@ -705,6 +710,16 @@ def import_job(self, export_data: dict) -> int: conn.rollback() raise ValueError(f"Failed to import job: {str(e)}") from e + def job_cleanup(self, keep_days: int = 89): + conn = self.db_manager.get_db_connection() + cursor = conn.cursor() + cursor.execute( + """ + DELETE FROM jobs WHERE start_time < datetime(current_timestamp, concat('-', ?, ' DAYS')); + """, + (keep_days,), + ) + class PostgresApiRepository(ApiRepository): def __init__(self, db_manager: DatabaseManager): @@ -1243,3 +1258,13 @@ def import_job(self, export_data: dict) -> int: except Exception as e: conn.rollback() raise ValueError(f"Failed to import job: {str(e)}") from e + + def job_cleanup(self, keep_days: int = 89): + conn = self.db_manager.get_db_connection() + cursor = conn.cursor() + cursor.execute( + """ + DELETE FROM jobs WHERE start_time < now() - INTERVAL '%s DAYS'; + """, + (keep_days,), + )