- Fix missing await on FocusGroup.get_messages() (N-L1) - Replace time.sleep with asyncio.sleep in key_theme_service and focus_group_service (N-P10) - Replace flask import with quart in focus_groups.py (N-S3) - Add logger.error before all 500 returns in focus_groups.py (N-P6) - Add logging to silent except blocks across routes (N-M10, N-M11) - Add @rate_limit to 6 remaining AI endpoints (N-H4) - Add --confirm flag to populate scripts before delete_many (S-H2) - Remove hardcoded Azure ID fallbacks from msal_service.py and msalConfig.ts (A-M2, F-H4) - Centralize make_serializable() in utils.py, remove duplicates from 3 route files (N-P7) - Replace all datetime.utcnow() with datetime.now(timezone.utc) across entire backend (M-L2) - AuthContext.tsx: only mark token validated on 200 success, not on non-401 errors (F-H2) - Rename authType → auth_type in auth.py (N-S4) - Add security_report.md and security_report.pdf with full 92-finding status Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
130 lines
No EOL
4 KiB
Python
Executable file
130 lines
No EOL
4 KiB
Python
Executable file
"""
|
|
Task management routes for handling cancellable operations.
|
|
"""
|
|
|
|
from quart import Blueprint, jsonify, request
|
|
from app.services.task_manager import get_task_manager
|
|
from app.websocket_manager_async import get_async_websocket_manager
|
|
from app.auth.quart_jwt import jwt_required, get_jwt_identity
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
tasks_bp = Blueprint('tasks', __name__)
|
|
|
|
|
|
@tasks_bp.route('/<task_id>', methods=['DELETE'])
|
|
@jwt_required()
|
|
async def cancel_task(task_id: str):
|
|
"""
|
|
Cancel a running task by its ID.
|
|
|
|
Args:
|
|
task_id: The unique identifier of the task to cancel
|
|
|
|
Returns:
|
|
JSON response indicating success or failure
|
|
"""
|
|
try:
|
|
task_manager = get_task_manager()
|
|
|
|
# Get task info before cancellation for WebSocket notification
|
|
task_info = await task_manager.get_task_info(task_id)
|
|
|
|
# Attempt to cancel the task
|
|
cancelled = await task_manager.cancel_task(task_id)
|
|
|
|
if not cancelled:
|
|
return jsonify({
|
|
'error': 'Task not found or already completed',
|
|
'task_id': task_id
|
|
}), 404
|
|
|
|
# Send WebSocket notification about cancellation
|
|
websocket_manager = get_async_websocket_manager()
|
|
if task_info and task_info.user_id:
|
|
await websocket_manager.emit_to_user(
|
|
task_info.user_id,
|
|
'task_cancelled',
|
|
{
|
|
'task_id': task_id,
|
|
'task_type': task_info.task_type,
|
|
'message': f'{task_info.task_type.replace("_", " ").title()} cancelled successfully'
|
|
}
|
|
)
|
|
|
|
logger.info(f"Successfully cancelled task {task_id}")
|
|
|
|
return jsonify({
|
|
'message': 'Task cancelled successfully',
|
|
'task_id': task_id,
|
|
'task_type': task_info.task_type if task_info else None
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error cancelling task {task_id}: {str(e)}")
|
|
return jsonify({
|
|
'error': 'Internal server error while cancelling task',
|
|
'task_id': task_id
|
|
}), 500
|
|
|
|
|
|
@tasks_bp.route('/user/me', methods=['GET'])
|
|
@jwt_required()
|
|
async def get_user_tasks():
|
|
"""
|
|
Get all active tasks for the authenticated user.
|
|
|
|
Returns:
|
|
JSON response with list of active tasks
|
|
"""
|
|
try:
|
|
user_id = get_jwt_identity()
|
|
task_manager = get_task_manager()
|
|
user_tasks = await task_manager.get_user_tasks(user_id)
|
|
|
|
# Convert task info to JSON-serializable format
|
|
tasks_data = []
|
|
for task_id, task_info in user_tasks.items():
|
|
tasks_data.append({
|
|
'task_id': task_id,
|
|
'task_type': task_info.task_type,
|
|
'status': task_info.status,
|
|
'created_at': task_info.created_at.isoformat(),
|
|
'metadata': task_info.metadata
|
|
})
|
|
|
|
return jsonify({
|
|
'tasks': tasks_data,
|
|
'count': len(tasks_data)
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error fetching tasks for user {user_id}: {str(e)}")
|
|
return jsonify({
|
|
'error': 'Internal server error while fetching user tasks'
|
|
}), 500
|
|
|
|
|
|
@tasks_bp.route('/status', methods=['GET'])
|
|
async def get_task_status():
|
|
"""
|
|
Get overall task manager status (for debugging/monitoring).
|
|
|
|
Returns:
|
|
JSON response with task manager statistics
|
|
"""
|
|
try:
|
|
task_manager = get_task_manager()
|
|
active_count = await task_manager.get_active_task_count()
|
|
|
|
return jsonify({
|
|
'active_tasks': active_count,
|
|
'status': 'operational'
|
|
}), 200
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error fetching task manager status: {str(e)}")
|
|
return jsonify({
|
|
'error': 'Internal server error while fetching status'
|
|
}), 500 |