73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
"""
|
|
Task-related routes for the Podcastrr application.
|
|
"""
|
|
import logging
|
|
from flask import Blueprint, jsonify, request, current_app, render_template
|
|
from app.services.task_manager import task_manager, TaskStatus
|
|
|
|
# Set up logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
tasks_bp = Blueprint('tasks', __name__)
|
|
|
|
@tasks_bp.route('/tasks', methods=['GET'])
|
|
def view_tasks():
|
|
"""
|
|
Render the tasks page showing task history and in-progress tasks.
|
|
"""
|
|
tasks = task_manager.get_all_tasks()
|
|
|
|
# Separate tasks by status
|
|
running_tasks = [task for task in tasks if task.status == TaskStatus.RUNNING or task.status == TaskStatus.PENDING]
|
|
completed_tasks = [task for task in tasks if task.status == TaskStatus.COMPLETED]
|
|
failed_tasks = [task for task in tasks if task.status == TaskStatus.FAILED]
|
|
|
|
# Sort tasks by created_at (newest first)
|
|
running_tasks.sort(key=lambda x: x.created_at, reverse=True)
|
|
completed_tasks.sort(key=lambda x: x.completed_at or x.created_at, reverse=True)
|
|
failed_tasks.sort(key=lambda x: x.completed_at or x.created_at, reverse=True)
|
|
|
|
return render_template('tasks/index.html',
|
|
running_tasks=running_tasks,
|
|
completed_tasks=completed_tasks,
|
|
failed_tasks=failed_tasks)
|
|
|
|
@tasks_bp.route('/api/tasks', methods=['GET'])
|
|
def get_tasks():
|
|
"""
|
|
Get all tasks or filter by status.
|
|
"""
|
|
status = request.args.get('status')
|
|
tasks = task_manager.get_all_tasks()
|
|
|
|
if status:
|
|
tasks = [task for task in tasks if task.status.value == status]
|
|
|
|
return jsonify({
|
|
'tasks': [task.to_dict() for task in tasks]
|
|
})
|
|
|
|
@tasks_bp.route('/api/tasks/<task_id>', methods=['GET'])
|
|
def get_task(task_id):
|
|
"""
|
|
Get a specific task by ID.
|
|
"""
|
|
task = task_manager.get_task(task_id)
|
|
|
|
if not task:
|
|
return jsonify({'error': 'Task not found'}), 404
|
|
|
|
return jsonify(task.to_dict())
|
|
|
|
@tasks_bp.route('/api/tasks/clean', methods=['POST'])
|
|
def clean_tasks():
|
|
"""
|
|
Clean up old completed or failed tasks.
|
|
"""
|
|
max_age = request.json.get('max_age_seconds', 3600) if request.json else 3600
|
|
count = task_manager.clean_old_tasks(max_age)
|
|
|
|
return jsonify({
|
|
'message': f'Cleaned up {count} old tasks',
|
|
'count': count
|
|
})
|