File size: 1,500 Bytes
81ff144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging
from typing import Optional, List
from .supabase_service import supabase

logger = logging.getLogger(__name__)

class TaskQueueService:
    @staticmethod
    async def queue_task(task_id: str):
        """
        Marks a task as 'queued' in the database.
        """
        try:
            result = supabase.table("tasks").update({"status": "queued"}).eq("id", task_id).execute()
            return result
        except Exception as e:
            logger.error(f"Error queueing task {task_id}: {e}")
            return None

    @staticmethod
    async def get_next_queued_task():
        """
        Fetches the next available task from the queue.
        """
        try:
            # Fetch one task that is in 'queued' status, ordered by priority and created_at
            result = supabase.table("tasks") \
                .select("*") \
                .eq("status", "queued") \
                .order("priority", desc=True) \
                .order("created_at") \
                .limit(1) \
                .execute()
            
            if result.data:
                return result.data[0]
            return None
        except Exception as e:
            logger.error(f"Error fetching next queued task: {e}")
            return None

    @staticmethod
    async def mark_in_progress(task_id: str):
        """
        Marks a task as 'in_progress'.
        """
        return supabase.table("tasks").update({"status": "in_progress"}).eq("id", task_id).execute()