Spaces:
3SCError404
/
Paused

File size: 2,237 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
            job = await redis.enqueue_job('run_ai_workflow', ticket_data)
            print(f"[{i+1}/{len(to_process)}] Enqueued Ticket {t.id} -> Job {job.job_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))