ongoing polish in queue handling
This commit is contained in:
@@ -168,6 +168,45 @@ async def recent_activity():
|
||||
})
|
||||
|
||||
|
||||
@bp.route("/reset-orphaned", methods=["POST"])
|
||||
async def reset_orphaned_downloads():
|
||||
"""Reset orphaned running downloads back to queued.
|
||||
|
||||
Jobs can get stuck in 'running' status if the worker crashes.
|
||||
This endpoint resets them so they can be retried.
|
||||
"""
|
||||
from app.tasks.maintenance import reset_orphaned_jobs
|
||||
|
||||
# Allow specifying threshold, default to 0 to reset ALL running jobs
|
||||
data = await request.get_json() or {}
|
||||
threshold_minutes = data.get("threshold_minutes", 0) # 0 = reset all running
|
||||
|
||||
try:
|
||||
if threshold_minutes == 0:
|
||||
# Reset all running jobs immediately (synchronous for immediate feedback)
|
||||
from app.tasks.maintenance import _reset_all_running_jobs_async
|
||||
import asyncio
|
||||
|
||||
# Run in the current event loop
|
||||
result = await _reset_all_running_jobs_async()
|
||||
current_app.logger.info(f"Manual reset of running jobs: {result}")
|
||||
return jsonify({
|
||||
"message": f"Reset {result['reset_count']} orphaned running jobs",
|
||||
"reset_count": result['reset_count'],
|
||||
})
|
||||
else:
|
||||
# Queue the task for threshold-based reset
|
||||
task = reset_orphaned_jobs.delay(threshold_minutes)
|
||||
return jsonify({
|
||||
"message": "Orphaned job reset queued",
|
||||
"task_id": task.id,
|
||||
"threshold_minutes": threshold_minutes,
|
||||
}), 202
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Failed to reset orphaned jobs: {e}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@bp.route("/stats", methods=["GET"])
|
||||
async def get_stats():
|
||||
"""Get download statistics."""
|
||||
@@ -208,4 +247,6 @@ async def get_stats():
|
||||
"completed": status_counts.get(DownloadStatus.COMPLETED, 0),
|
||||
"failed": status_counts.get(DownloadStatus.FAILED, 0),
|
||||
"pending": status_counts.get(DownloadStatus.PENDING, 0),
|
||||
"queued": status_counts.get(DownloadStatus.QUEUED, 0),
|
||||
"running": status_counts.get(DownloadStatus.RUNNING, 0),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user