Files
FusionAGI/fusionagi/api/task_queue.py

107 lines
3.5 KiB
Python
Raw Normal View History

Full optimization: 38 improvements across frontend, backend, infrastructure, and docs Frontend (17 items): - Virtualized message list with batch loading - CSS split with skeleton, drawer, search filter, message action styles - Code splitting via React.lazy + Suspense for Admin/Ethics/Settings pages - Skeleton loading components (Skeleton, SkeletonCard, SkeletonGrid) - Debounced search/filter component (SearchFilter) - Error boundary with fallback UI - Keyboard shortcuts (Ctrl+K search, Ctrl+Enter send, Escape dismiss) - Page transition animations (fade-in) - PWA support (manifest.json + service worker) - WebSocket auto-reconnect with exponential backoff (10 retries) - Chat history persistence to localStorage (500 msg limit) - Message edit/delete on hover - Copy-to-clipboard on code blocks - Mobile drawer (bottom-sheet for consensus panel) - File upload support - User preferences sync to backend Testing (8 items): - Component tests: Toast, Markdown, ChatMessage, Avatar, ErrorBoundary, Skeleton - Hook tests: useChatHistory - E2E smoke tests (5 tests) - Accessibility audit utility Backend (12 items): - Vector memory with cosine similarity search - TTS/STT adapter factory wiring - Geometry kernel with orphan detection - Tenant registry with CRUD operations - Response cache with TTL - Connection pool (async) - Background task queue - Health check endpoints (/health, /ready) - Request tracing middleware (X-Request-ID) - API key rotation mechanism - Environment-based config (settings.py) - API route documentation improvements Infrastructure (4 items): - Grafana dashboard template - Database migration system - Storybook configuration Documentation (3 items): - ADR-001: Advisory Governance Model - ADR-002: Twelve-Head Architecture - ADR-003: Consequence Engine 552 Python tests + 45 frontend tests passing, 0 ruff errors. Co-Authored-By: Nakamoto, S <defi@defi-oracle.io>
2026-05-02 03:08:08 +00:00
"""Async background task queue for long-running operations."""
import asyncio
import time
import uuid
from enum import Enum
from typing import Any, Callable, Coroutine
from pydantic import BaseModel, Field
class TaskStatus(str, Enum):
"""Background task status."""
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
CANCELLED = "cancelled"
class TaskResult(BaseModel):
"""Result of a background task."""
task_id: str
status: TaskStatus
result: Any = None
error: str | None = None
created_at: float = Field(default_factory=time.time)
completed_at: float | None = None
duration_ms: float | None = None
class BackgroundTaskQueue:
"""Async task queue for offloading long-running work.
Tasks are submitted and run concurrently via asyncio. Results are
stored in-memory and queryable by task_id.
"""
def __init__(self, max_concurrent: int = 5, result_ttl: float = 3600.0) -> None:
self._semaphore = asyncio.Semaphore(max_concurrent)
self._results: dict[str, TaskResult] = {}
self._tasks: dict[str, asyncio.Task[None]] = {}
self._result_ttl = result_ttl
def submit(
self,
fn: Callable[..., Coroutine[Any, Any, Any]],
*args: Any,
task_id: str | None = None,
**kwargs: Any,
) -> str:
"""Submit a coroutine to run in the background. Returns task_id."""
tid = task_id or str(uuid.uuid4())
self._results[tid] = TaskResult(task_id=tid, status=TaskStatus.PENDING)
async def _runner() -> None:
async with self._semaphore:
self._results[tid].status = TaskStatus.RUNNING
start = time.time()
try:
result = await fn(*args, **kwargs)
self._results[tid].result = result
self._results[tid].status = TaskStatus.COMPLETED
except Exception as e:
self._results[tid].error = str(e)
self._results[tid].status = TaskStatus.FAILED
finally:
self._results[tid].completed_at = time.time()
self._results[tid].duration_ms = (time.time() - start) * 1000
loop = asyncio.get_event_loop()
task = loop.create_task(_runner())
self._tasks[tid] = task
return tid
def get_status(self, task_id: str) -> TaskResult | None:
"""Get the status and result of a task."""
return self._results.get(task_id)
def cancel(self, task_id: str) -> bool:
"""Cancel a pending or running task."""
task = self._tasks.get(task_id)
if task and not task.done():
task.cancel()
self._results[task_id].status = TaskStatus.CANCELLED
return True
return False
def list_tasks(self, status: TaskStatus | None = None) -> list[TaskResult]:
"""List all tasks, optionally filtered by status."""
results = list(self._results.values())
if status:
results = [r for r in results if r.status == status]
return results
def cleanup_expired(self) -> int:
"""Remove completed tasks older than result_ttl."""
now = time.time()
expired = [
tid for tid, r in self._results.items()
if r.completed_at and (now - r.completed_at) > self._result_ttl
]
for tid in expired:
del self._results[tid]
self._tasks.pop(tid, None)
return len(expired)