Fix critical in-memory task storage with database persistence
Major architectural improvement to replace in-memory task storage with database-backed persistence while maintaining backward compatibility. Changes: - Created Task SQLAlchemy model matching database schema - Added Workflow and Execution SQLAlchemy models - Created TaskService for database CRUD operations - Updated UnifiedCoordinator to use database persistence - Modified task APIs to leverage database storage - Added task loading from database on coordinator initialization - Implemented status change persistence during task execution - Enhanced task cleanup with database support - Added comprehensive task statistics from database Benefits: - Tasks persist across application restarts - Better scalability and reliability - Historical task data retention - Comprehensive task filtering and querying - Maintains in-memory cache for performance 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
from . import agent
|
||||
from . import project
|
||||
from . import project
|
||||
from . import task
|
||||
from . import sqlalchemy_models
|
||||
@@ -1,5 +1,6 @@
|
||||
from sqlalchemy import Column, Integer, String, DateTime, JSON
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from ..core.database import Base
|
||||
|
||||
class Agent(Base):
|
||||
@@ -23,6 +24,9 @@ class Agent(Base):
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
last_seen = Column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
# Relationships
|
||||
tasks = relationship("Task", back_populates="assigned_agent")
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
|
||||
63
backend/app/models/sqlalchemy_models.py
Normal file
63
backend/app/models/sqlalchemy_models.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
SQLAlchemy models for workflows and executions
|
||||
"""
|
||||
|
||||
from sqlalchemy import Column, String, Text, Integer, Boolean, DateTime, ForeignKey, UUID as SqlUUID
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from ..core.database import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Workflow(Base):
|
||||
__tablename__ = "workflows"
|
||||
|
||||
# Primary identification
|
||||
id = Column(SqlUUID(as_uuid=True), primary_key=True, index=True, default=uuid.uuid4)
|
||||
|
||||
# Workflow details
|
||||
name = Column(String(255), nullable=False)
|
||||
description = Column(Text)
|
||||
n8n_data = Column(JSONB, nullable=False)
|
||||
mcp_tools = Column(JSONB)
|
||||
|
||||
# Relationships
|
||||
created_by = Column(SqlUUID(as_uuid=True), ForeignKey("users.id"))
|
||||
|
||||
# Metadata
|
||||
version = Column(Integer, default=1)
|
||||
active = Column(Boolean, default=True)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
creator = relationship("User", back_populates="workflows")
|
||||
executions = relationship("Execution", back_populates="workflow")
|
||||
tasks = relationship("Task", back_populates="workflow")
|
||||
|
||||
|
||||
class Execution(Base):
|
||||
__tablename__ = "executions"
|
||||
|
||||
# Primary identification
|
||||
id = Column(SqlUUID(as_uuid=True), primary_key=True, index=True, default=uuid.uuid4)
|
||||
|
||||
# Execution details
|
||||
workflow_id = Column(SqlUUID(as_uuid=True), ForeignKey("workflows.id"), nullable=True)
|
||||
status = Column(String(50), default='pending')
|
||||
input_data = Column(JSONB)
|
||||
output_data = Column(JSONB)
|
||||
error_message = Column(Text)
|
||||
progress = Column(Integer, default=0)
|
||||
|
||||
# Timestamps
|
||||
started_at = Column(DateTime(timezone=True), nullable=True)
|
||||
completed_at = Column(DateTime(timezone=True), nullable=True)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
# Relationships
|
||||
workflow = relationship("Workflow", back_populates="executions")
|
||||
tasks = relationship("Task", back_populates="execution")
|
||||
41
backend/app/models/task.py
Normal file
41
backend/app/models/task.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""
|
||||
Task model for SQLAlchemy ORM
|
||||
"""
|
||||
|
||||
from sqlalchemy import Column, String, Text, Integer, DateTime, ForeignKey, UUID as SqlUUID
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from ..core.database import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Task(Base):
|
||||
__tablename__ = "tasks"
|
||||
|
||||
# Primary identification
|
||||
id = Column(SqlUUID(as_uuid=True), primary_key=True, index=True, default=uuid.uuid4)
|
||||
|
||||
# Task details
|
||||
title = Column(String(255), nullable=False)
|
||||
description = Column(Text)
|
||||
priority = Column(Integer, default=5)
|
||||
status = Column(String(50), default='pending')
|
||||
|
||||
# Relationships
|
||||
assigned_agent_id = Column(String(255), ForeignKey("agents.id"), nullable=True)
|
||||
workflow_id = Column(SqlUUID(as_uuid=True), ForeignKey("workflows.id"), nullable=True)
|
||||
execution_id = Column(SqlUUID(as_uuid=True), ForeignKey("executions.id"), nullable=True)
|
||||
|
||||
# Metadata and context
|
||||
metadata = Column(JSONB, nullable=True)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
started_at = Column(DateTime(timezone=True), nullable=True)
|
||||
completed_at = Column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
# Relationships
|
||||
assigned_agent = relationship("Agent", back_populates="tasks")
|
||||
workflow = relationship("Workflow", back_populates="tasks")
|
||||
execution = relationship("Execution", back_populates="tasks")
|
||||
@@ -44,6 +44,7 @@ class User(Base):
|
||||
# Relationships for authentication features
|
||||
api_keys = relationship("APIKey", back_populates="user", cascade="all, delete-orphan")
|
||||
refresh_tokens = relationship("RefreshToken", back_populates="user", cascade="all, delete-orphan")
|
||||
workflows = relationship("Workflow", back_populates="creator")
|
||||
|
||||
def verify_password(self, password: str) -> bool:
|
||||
"""Verify a password against the hashed password."""
|
||||
|
||||
Reference in New Issue
Block a user