o
    潐iV                     @   sH   d Z ddlZddlZddlZddlmZ G dd dZeddZeZdS )zW
Simple Queue Manager - No RQ dependency
Updated to handle both content and audio jobs
    N)datetimec                   @   sF   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dS )QueueManagercontent_audio_queuec                 C   s"   t jdddd| _|| _d| _d S )N	localhosti  r   )hostportdbzjob:)redisRedisr
queue_name
job_prefix)selfr    r   |/home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/audio_queue_manager.py__init__   s   
zQueueManager.__init__c                 C      d| dt  jdd  }|||ddt  | jd}| j| j	 | dt
| | j| j| td	|  |dd| j| j| jd
S )z!Add content creation job to queuecontent__N   content_creationqueuedjob_idhashiddatajob_typestatuscreatedqueueQ u   ✅ Content job queued: r   r   r   r   
queue_sizeuuiduuid4hexr   now	isoformatr   r   setexr   jsondumpsrpushprintllenr   r   
input_datar   job_datar   r   r   add_content_job   $   
 zQueueManager.add_content_jobc                 C   r   )zAdd audio creation job to queueaudio_r   Nr   audio_creationr   r   r    u   ✅ Audio job queued: r!   r#   r/   r   r   r   add_audio_job.   r3   zQueueManager.add_audio_jobc                 C   s$   |  ||}| ||}||ddS )z
        Add both content and audio jobs to the same queue.
        Content job will be processed first, then audio job.
        zNBoth content and audio jobs queued. Content will be created first, then audio.)content_job	audio_jobmessage)r2   r6   )r   r   r0   r7   r8   r   r   r   add_content_and_audio_jobsL   s   z'QueueManager.add_content_and_audio_jobsc                 C   s   t d d| dt jdd  }|||ddt  | jd}| j	| j
 | d	t| | j| j| t d
|  |dd| j| j| jdS )zs
        Legacy method for backward compatibility
        This mimics the old audio_queue_manager behavior
        uA   ⚠️ Using legacy add_job method - consider updating to new APIr4   r   Nr   r5   r   r   r    u   ✅ Legacy job queued: r!   )r-   r$   r%   r&   r   r'   r(   r   r   r)   r   r*   r+   r,   r.   r/   r   r   r   add_job^   s&   
 zQueueManager.add_jobc                 C   sz   | j | j | }|sddiS t|}|d dkr;| j | jdd}|r;dd |D }||v r;||d |d	< |S )
zGet job statusr   	not_foundr   r   c                 S   s   g | ]}| d qS )zutf-8)decode).0itemr   r   r   
<listcomp>   s    z+QueueManager.get_status.<locals>.<listcomp>   queue_position)r   getr   r*   loadslranger   index)r   r   r1   r   queue_itemsr   r   r   
get_status   s   
zQueueManager.get_statusc                 C   s8   | j | j| j tdd | jdD t  dS )zGet queue statisticsc                 S   s   g | ]}|qS r   r   )r?   kr   r   r   rA      s    z0QueueManager.get_queue_stats.<locals>.<listcomp>zworker:*)r   r"   workers	timestamp)r   r   r.   lenkeysr   r'   r(   )r   r   r   r   get_queue_stats   s
   
zQueueManager.get_queue_statsN)r   )
__name__
__module____qualname__r   r2   r6   r:   r;   rI   rO   r   r   r   r   r   
   s    
$r   r   )r   )__doc__r	   r*   r$   r   r   queue_manageraudio_queue_managerr   r   r   r   <module>   s     
