Buckets:

download
raw
18.1 kB
import uuid
import datetime
import threading
import traceback
import json
import os
from typing import List, Dict, Any, Callable, Optional
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.orm.attributes import flag_modified
from backend.db.models.db_task import DBTask
from backend.db.base import TaskStatus
from backend.ws_manager import manager
from backend.models.task import TaskInfo
from backend.db.models.user import User as DBUser
from backend.tasks.utils import _to_task_info
def _serialize_task(db_task: DBTask) -> Optional[dict]:
"""
Serializes a SQLAlchemy DBTask object into a dictionary suitable for JSON transport.
Ensures that the owner relationship is loaded.
"""
if not db_task:
return None
try:
# 1. Truncate Logs with Cumulative Size Guard
# This prevents a payload from exploding if multiple logs are individually "medium" sized.
recent_logs = []
if db_task.logs and isinstance(db_task.logs, list):
cumulative_size = 0
# Iterate backwards through last 10 logs
for log in reversed(db_task.logs[-10:]):
msg = log.get("message", "")
# Clip very long single entries
if len(msg) > 2000:
msg = msg[:2000] + "... [clipped]"
log_entry = {**log, "message": msg}
entry_size = len(json.dumps(log_entry))
# If adding this log would push the payload toward the 1MB limit, stop here.
if cumulative_size + entry_size > 50000: # 50KB limit for logs in UI
recent_logs.insert(0, {"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(), "message": "[Older logs omitted]", "level": "INFO"})
break
recent_logs.insert(0, log_entry)
cumulative_size += entry_size
# 2. STRIP LARGE RESULTS (Crucial for Images)
# We NEVER send the full base64 image or huge JSON over WebSocket.
# This data is saved in the DB; the UI will fetch it via standard API calls.
safe_result = db_task.result
if safe_result:
if isinstance(safe_result, str) and len(safe_result) > 10000:
# Flag to the UI that data exists but is omitted for transport stability
safe_result = "[Large Output Stored in DB]"
elif isinstance(safe_result, (dict, list)):
try:
# Check if the serialized dict is over 10KB
if len(json.dumps(safe_result)) > 10000:
safe_result = {"ui_hint": "large_data_omitted", "full_data": "available_in_db"}
except:
safe_result = "[Unserializable Data]"
task_info = {
"id": db_task.id,
"name": db_task.name,
"description": db_task.description,
"status": db_task.status,
"progress": db_task.progress,
"logs": recent_logs,
"result": safe_result,
"error": db_task.error,
"result": db_task.result,
"error": db_task.error,
"created_at": db_task.created_at,
"started_at": db_task.started_at,
"completed_at": db_task.completed_at,
"updated_at": db_task.updated_at,
"file_name": db_task.file_name,
"total_files": db_task.total_files,
"owner_username": db_task.owner.username if db_task.owner else "System"
}
# Serialize datetime objects to ISO strings
for key in ['created_at', 'started_at', 'completed_at', 'updated_at']:
if task_info[key] and isinstance(task_info[key], datetime.datetime):
task_info[key] = task_info[key].isoformat()
return task_info
except Exception as e:
print(f"Error serializing task {db_task.id}: {e}")
return None
import time
class Task:
"""
Represents a runnable task that updates its state in the database.
This object is managed by the TaskManager and executed in a separate thread.
"""
def __init__(self, id: str, name: str, description: Optional[str], target: Callable, args: tuple, kwargs: dict, owner_username: Optional[str], db_session_factory: Callable[[], Session]):
self.id = id
self.name = name
self.description = description
self.target = target
self.args = args
self.kwargs = kwargs or {}
self.owner_username = owner_username
self.db_session_factory = db_session_factory
self.cancellation_event = threading.Event()
self.process = None
self.db_lock = threading.Lock()
# Throttling state to prevent WebSocket flooding and frontend crashes (STATUS_BREAKPOINT)
self.last_broadcast_time = 0
self.broadcast_interval = 0.1 # Max 10 updates per second
def _broadcast_update(self, db_task: DBTask, force: bool = False):
"""Sends a WebSocket update for the task. Throttled to prevent UI flooding."""
if not db_task:
return
now = time.time()
# Always broadcast if it's a final state or forced, otherwise check interval
is_final_state = db_task.status in [TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED]
if not force and not is_final_state and (now - self.last_broadcast_time < self.broadcast_interval):
return
try:
self.last_broadcast_time = now
task_data = _serialize_task(db_task)
if not task_data:
return
payload = {"type": "task_update", "data": task_data}
# Broadcast to admins and the specific user (if any)
# The manager will handle sending this to all relevant connections across all workers.
manager.broadcast_to_admins_sync(payload)
if db_task.owner_user_id:
# [FIX] Ensure user ID is passed as int for consistent lookup in connection manager
manager.send_personal_message_sync(payload, int(db_task.owner_user_id))
# Handle special 'result' payloads for direct UI updates
is_finished = db_task.status in [TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED]
if is_finished:
end_payload = {"type": "task_end", "data": task_data}
if db_task.owner_user_id:
manager.send_personal_message_sync(end_payload, int(db_task.owner_user_id))
manager.broadcast_to_admins_sync(end_payload)
except Exception as e:
print(f"Error broadcasting task update for {self.id}: {e}")
traceback.print_exc()
def _update_db(self, **kwargs):
"""Safely updates the task's record in the database."""
with self.db_lock:
with self.db_session_factory() as db:
try:
task_record = db.query(DBTask).options(joinedload(DBTask.owner)).filter(DBTask.id == self.id).first()
if not task_record:
return
for key, value in kwargs.items():
setattr(task_record, key, value)
task_record.updated_at = datetime.datetime.now(datetime.timezone.utc)
db.commit()
db.refresh(task_record, ['owner'])
# If we are setting a final status, force the broadcast
force_broadcast = "status" in kwargs and kwargs["status"] in [TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED]
self._broadcast_update(task_record, force=force_broadcast)
except Exception as e:
print(f"CRITICAL: Task {self.id} - Failed to update database: {e}")
traceback.print_exc()
db.rollback()
def log(self, message: str, level: str = "INFO"):
"""Adds a log entry to the task's record."""
log_entry = {
"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"message": message,
"level": level
}
with self.db_lock:
with self.db_session_factory() as db:
try:
task_record = db.query(DBTask).options(joinedload(DBTask.owner)).filter(DBTask.id == self.id).first()
if task_record:
if task_record.logs is None:
task_record.logs = []
# Clone logs to ensure SQLAlchemy detects change on mutable JSON
current_logs = list(task_record.logs)
current_logs.append(log_entry)
task_record.logs = current_logs
task_record.updated_at = datetime.datetime.now(datetime.timezone.utc)
flag_modified(task_record, "logs")
db.commit()
db.refresh(task_record, ['owner'])
self._broadcast_update(task_record)
except Exception as e:
print(f"CRITICAL: Task {self.id} - Failed to write log to database: {e}")
traceback.print_exc()
db.rollback()
def set_progress(self, value: int):
"""Sets the task's progress percentage."""
self._update_db(progress=max(0, min(100, value)))
def set_description(self, description: str):
"""Updates the task's description."""
self._update_db(description=description)
def set_file_info(self, file_name: str, total_files: int):
"""Sets file-related information for the task."""
self._update_db(file_name=file_name, total_files=total_files)
def cancel(self):
"""Signals the task to cancel."""
self.cancellation_event.set()
if self.process and self.process.poll() is None:
self.log("Terminating associated subprocess...", "WARNING")
try:
self.process.terminate()
except ProcessLookupError:
self.log("Process already terminated or does not exist.", "INFO")
self.log("Cancellation signal received.", level="WARNING")
def run(self):
"""The main execution method for the task thread."""
self._update_db(status=TaskStatus.RUNNING, started_at=datetime.datetime.now(datetime.timezone.utc))
self.log(f"Task '{self.name}' started.")
final_updates = {}
try:
result = self.target(self, *self.args, **self.kwargs)
if self.cancellation_event.is_set():
final_updates["status"] = TaskStatus.CANCELLED
self.log(f"Task '{self.name}' was cancelled.", level="WARNING")
else:
final_updates["status"] = TaskStatus.COMPLETED
final_updates["progress"] = 100
final_updates["result"] = result
self.log(f"Task '{self.name}' completed successfully.")
except Exception as e:
tb_str = traceback.format_exc()
final_updates["status"] = TaskStatus.FAILED
final_updates["error"] = str(e)
self.log(f"Task '{self.name}' failed: {e}\n{tb_str}", level="CRITICAL")
finally:
final_updates["completed_at"] = datetime.datetime.now(datetime.timezone.utc)
self._update_db(**final_updates)
class TaskManager:
"""
Manages the lifecycle of background tasks in a persistent, thread-safe manner.
"""
def __init__(self, db_session_factory: Optional[Callable[[], Session]] = None):
self.db_session_factory = db_session_factory
self.active_tasks: Dict[str, Task] = {}
self.lock = threading.Lock()
def init_app(self, db_session_factory: Callable[[], Session]):
"""Initializes the TaskManager with a database session factory."""
self.db_session_factory = db_session_factory
def _run_and_cleanup(self, task: Task):
"""Wrapper to run a task and ensure it's removed from the active list upon completion."""
try:
task.run()
finally:
with self.lock:
if task.id in self.active_tasks:
del self.active_tasks[task.id]
def submit_task(self, name: str, target: Callable, args: tuple = (), kwargs: dict = None, description: Optional[str] = None, owner_username: Optional[str] = None) -> TaskInfo:
"""
Creates a task record in the DB, and starts its execution in a new thread.
"""
if not self.db_session_factory:
raise RuntimeError("TaskManager not initialized. Call init_app first.")
with self.db_session_factory() as db:
owner_id = None
if owner_username:
user = db.query(DBUser).filter_by(username=owner_username).first()
if user:
owner_id = user.id
new_db_task = DBTask(
name=name,
description=description or name,
owner_user_id=owner_id
)
db.add(new_db_task)
db.commit()
db.refresh(new_db_task, ['owner'])
task_info_to_return = _to_task_info(new_db_task)
# Initial broadcast that the task has been created
task_instance_for_broadcast = Task(id=new_db_task.id, name=name, description=description, target=target, args=args, kwargs=kwargs, owner_username=owner_username, db_session_factory=self.db_session_factory)
task_instance_for_broadcast._broadcast_update(new_db_task)
task_instance = Task(id=new_db_task.id, name=name, description=description, target=target, args=args, kwargs=kwargs, owner_username=owner_username, db_session_factory=self.db_session_factory)
with self.lock:
self.active_tasks[task_instance.id] = task_instance
thread = threading.Thread(target=self._run_and_cleanup, args=(task_instance,), daemon=True)
thread.start()
return task_info_to_return
def cancel_task(self, task_id: str) -> bool:
"""
Cancels a task. If the task is actively running, it signals the thread.
If the task is a "zombie" (in DB but not running), it updates the DB directly.
"""
with self.lock:
task_instance = self.active_tasks.get(task_id)
if task_instance and not task_instance.cancellation_event.is_set():
task_instance.cancel()
return True
with self.db_session_factory() as db:
db_task = db.query(DBTask).options(joinedload(DBTask.owner)).filter(DBTask.id == task_id).first()
if db_task and db_task.status in [TaskStatus.PENDING, TaskStatus.RUNNING]:
db_task.status = TaskStatus.CANCELLED
db_task.completed_at = datetime.datetime.now(datetime.timezone.utc)
log_entry = {
"timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"message": "Task was cancelled manually while in a pending or orphaned state.",
"level": "WARNING"
}
current_logs = list(db_task.logs) if db_task.logs else []
current_logs.append(log_entry)
db_task.logs = current_logs
db.commit()
db.refresh(db_task, ['owner'])
# Manually create a task instance to call the broadcast method
task_instance_for_broadcast = Task(id=db_task.id, name=db_task.name, description=db_task.description, target=lambda: None, args=(), kwargs={}, owner_username=db_task.owner.username if db_task.owner else None, db_session_factory=self.db_session_factory)
task_instance_for_broadcast._broadcast_update(db_task)
return True
return False
def get_task(self, task_id: str) -> Optional[DBTask]:
"""Retrieves a task from the database by its ID."""
with self.db_session_factory() as db:
return db.query(DBTask).options(joinedload(DBTask.owner)).filter(DBTask.id == task_id).first()
def get_all_tasks(self) -> List[DBTask]:
"""Retrieves all tasks from the database for an admin."""
with self.db_session_factory() as db:
return db.query(DBTask).options(joinedload(DBTask.owner)).order_by(DBTask.created_at.desc()).all()
def get_tasks_for_user(self, username: str) -> List[DBTask]:
"""Retrieves all tasks for a specific user."""
with self.db_session_factory() as db:
return db.query(DBTask).options(joinedload(DBTask.owner)).join(DBUser, DBTask.owner_user_id == DBUser.id).filter(DBUser.username == username).order_by(DBTask.created_at.desc()).all()
def clear_completed_tasks(self, username: Optional[str] = None):
"""Deletes finished (completed, failed, cancelled) tasks from the database."""
with self.db_session_factory() as db:
query = db.query(DBTask).filter(DBTask.status.in_([TaskStatus.COMPLETED, TaskStatus.FAILED, TaskStatus.CANCELLED]))
if username:
query = query.join(DBUser, DBTask.owner_user_id == DBUser.id).filter(DBUser.username == username)
query.delete(synchronize_session=False)
db.commit()
payload = {"type": "tasks_cleared", "data": {"username": username}}
manager.broadcast_sync(payload)
task_manager = TaskManager()

Xet Storage Details

Size:
18.1 kB
·
Xet hash:
d214c58575d349816c8faa25b41ad6591f3c6e8b6295f026b5c7e511fe63d67e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.