fix(janitor): 禁止启动时因目录不存在而自动删除数据库任务记录
reconcile_tasks_on_startup() 中有一段逻辑:遍历所有任务,如果 output_dir 在磁盘上不存在,直接 DELETE FROM tasks。这导致重启时如果目录被移动/重命名/ 被之前的 janitor 清理,所有任务记录静默丢失。 修改为仅输出 WARNING 日志,保留数据库记录。用户可通过 DELETE /task/<id> 手动清理。
This commit is contained in:
@ -310,8 +310,7 @@ def reconcile_tasks_on_startup():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
current_app.logger.error(f"Failed to delete failed task {task_id}: {str(e)}", exc_info=True)
|
current_app.logger.error(f"Failed to delete failed task {task_id}: {str(e)}", exc_info=True)
|
||||||
|
|
||||||
# Check for tasks with output directories that no longer exist
|
# 检查 output_dir 不存在的任务(仅警告,不自动删除)
|
||||||
# This helps clean up database entries for manually deleted directories
|
|
||||||
rows = conn.execute("""
|
rows = conn.execute("""
|
||||||
SELECT task_id, output_dir
|
SELECT task_id, output_dir
|
||||||
FROM tasks
|
FROM tasks
|
||||||
@ -321,18 +320,21 @@ def reconcile_tasks_on_startup():
|
|||||||
|
|
||||||
orphaned_count = 0
|
orphaned_count = 0
|
||||||
for row in rows:
|
for row in rows:
|
||||||
task_id_from_db = row[0] # task_id是第一个字段
|
task_id_from_db = row[0]
|
||||||
output_dir_from_db = row[1] # output_dir是第二个字段
|
output_dir_from_db = row[1]
|
||||||
|
|
||||||
if not Path(output_dir_from_db).exists():
|
if not Path(output_dir_from_db).exists():
|
||||||
conn.execute(
|
current_app.logger.warning(
|
||||||
"DELETE FROM tasks WHERE task_id = ?",
|
f"Startup reconciliation: Task {task_id_from_db} output directory "
|
||||||
(task_id_from_db,)
|
f"not found on disk: {output_dir_from_db}. Database record preserved."
|
||||||
)
|
)
|
||||||
orphaned_count += 1
|
orphaned_count += 1
|
||||||
|
|
||||||
if orphaned_count > 0:
|
if orphaned_count > 0:
|
||||||
current_app.logger.info(f"Startup reconciliation: Marked {orphaned_count} tasks as deleted (directories not found)")
|
current_app.logger.info(
|
||||||
|
f"Startup reconciliation: Found {orphaned_count} tasks with missing directories "
|
||||||
|
f"(records preserved, use DELETE /task/<id> to manually clean up)"
|
||||||
|
)
|
||||||
|
|
||||||
# Backfill output_dir for rows with NULL, using OUTPUT_FOLDER/<task_id>
|
# Backfill output_dir for rows with NULL, using OUTPUT_FOLDER/<task_id>
|
||||||
output_base = Path(current_app.config.get('OUTPUT_FOLDER') or '')
|
output_base = Path(current_app.config.get('OUTPUT_FOLDER') or '')
|
||||||
|
|||||||
Reference in New Issue
Block a user