diff --git a/gasflux.ini b/gasflux.ini index 9a3456c..cd5353a 100644 --- a/gasflux.ini +++ b/gasflux.ini @@ -30,7 +30,7 @@ task_cleanup_interval = 30 # 24 hours in seconds max_task_age = 86400 # 1 minute in seconds for successful tasks -successful_task_cleanup_age = 60 +successful_task_cleanup_age = 315360000 # 1 minute in seconds for failed tasks failed_task_cleanup_age = 60 janitor_dry_run = false diff --git a/src/gasflux/blueprints/download.py b/src/gasflux/blueprints/download.py index 894c054..4e13c96 100644 --- a/src/gasflux/blueprints/download.py +++ b/src/gasflux/blueprints/download.py @@ -3,6 +3,7 @@ Download Blueprint Handles file download endpoints. """ +from datetime import datetime from pathlib import Path from flask import Blueprint, send_file, current_app @@ -10,40 +11,6 @@ from ..shared import _format_response, log_performance, logger from ..auth import require_api_key -def _mark_task_downloaded(task_id): - """Mark task as downloaded and schedule deletion in database.""" - import sqlite3 - from pathlib import Path - - # Use independent database connection (not from flask.g which may be closed) - from ..db import get_db_path as get_config_db_path - db_path = get_config_db_path(current_app) - - # Get cleanup age for successful tasks from config (in seconds) - successful_task_cleanup_age = current_app.config.get('SUCCESSFUL_TASK_CLEANUP_AGE', 3600) - - try: - conn = sqlite3.connect(str(db_path), check_same_thread=False) - conn.execute("PRAGMA foreign_keys=ON") - conn.execute("PRAGMA busy_timeout=3000") - - # Update downloaded timestamp and set deletion time based on config - conn.execute(""" - UPDATE tasks - SET downloaded_at = datetime('now', '+8 hours'), - delete_after_at = datetime('now', '+8 hours', '+' || ? || ' seconds') - WHERE task_id = ? - """, (successful_task_cleanup_age, task_id)) - - conn.commit() - logger.info(f"Task {task_id} marked as downloaded, scheduled for deletion in {successful_task_cleanup_age} seconds") - - except Exception as e: - logger.error(f"Failed to mark task {task_id} as downloaded: {str(e)}", exc_info=True) - finally: - if 'conn' in locals(): - conn.close() - # Create blueprint download_bp = Blueprint('download', __name__, url_prefix='/download') @@ -124,12 +91,19 @@ def download_file(filename): file_size = file_path.stat().st_size logger.info(f"Serving file: {filename} ({file_size} bytes)") - # Mark download immediately before sending file + # 记录下载时间(仅时间戳,不设置自动删除) if task_id: try: - _mark_task_downloaded(task_id) + from ..db import get_db + db = get_db() + db.execute( + "UPDATE tasks SET downloaded_at = datetime('now', '+8 hours') WHERE task_id = ?", + (task_id,) + ) + db.commit() + logger.info(f"Task {task_id} downloaded at {datetime.now()}") except Exception as e: - logger.error(f"Failed to mark download for task {task_id}: {str(e)}") + logger.error(f"Failed to record download for task {task_id}: {str(e)}") response = send_file(file_path) return response