Spaces:
Paused

miracle / scripts /retrigger_ai.py
dushyantsingh0
backend stabilization
09fa229
Raw History Blame Contribute Delete
2.32 kB
import asyncio
import uuid
import sys
import os
# Add project root to path for imports
sys.path.append(os.getcwd())
from sqlalchemy import select
from arq import create_pool
from arq.connections import RedisSettings
from core.database import async_session_factory
from core.config import get_settings
from models.ticket import Ticket
settings = get_settings()
async def reprocess_all_tickets(workspace_id=None):
"""
Finds tickets that are missing AI metadata and enqueues them for processing.
"""
async with async_session_factory() as db:
# Query for tickets that haven't been processed by AI yet
# (ai_metadata is NULL or doesn't have triage_success)
stmt = select(Ticket).where(
(Ticket.deleted_at.is_(None))
)
if workspace_id:
stmt = stmt.where(Ticket.workspace_id == uuid.UUID(workspace_id))
result = await db.execute(stmt)
tickets = result.scalars().all()
to_process = []
for t in tickets:
# Check if it HAS been processed
if not t.ai_metadata or "triage_success" not in t.ai_metadata:
to_process.append(t)
if not to_process:
print("No tickets found needing AI processing.")
return
print(f"Found {len(to_process)} tickets to re-trigger. Starting enqueue...")
redis = await create_pool(RedisSettings.from_dsn(settings.REDIS_URL))
for i, t in enumerate(to_process):
ticket_data = {
"id": str(t.id),
"workspace_id": str(t.workspace_id),
"title": t.title,
"description": t.description
}
# Enqueue to Arq (Correct Routing)
from core.tasks import enqueue_task
job = await enqueue_task(redis, 'run_ai_workflow', t.id, t.workspace_id, _job_id=f"retrigger_{t.id}")
print(f"[{i+1}/{len(to_process)}] Enqueued Ticket {t.id}")
await redis.close()
print("\nAll tasks enqueued successfully. Check the worker dashboard for progress.")
if __name__ == "__main__":
# Optional: pass workspace_id as arg
ws_id = sys.argv[1] if len(sys.argv) > 1 else None
asyncio.run(reprocess_all_tickets(ws_id))