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,), + )