o
    ;iV                     @   sj  d dl m Z  d dlmZ d dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlmZ d d	lmZmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ eddddZedddddZej dddej!edde"dee" fddZ#ej dddej!edd d!deed"de"d#e$d$e$dee" fd%dZ%ej d&ddej!ded'd(e$dee" fd)d*Z&ej d+ddej!eedd,d-d.defedde"d/ee" d0e$d$e$dee" f
d1d+Z'ej d2d3dej!ed4efedde"d5e(d/ee" dee" fd6d2Z)ej d7d3dej!eedd8d9d:dfedde"d/ee" d;e$dee" fd<d=Z*dS )>    )datetime)OptionalN)ArgumentOptionTyper)LoadContext)Resolver)TaskContextsynchronizer)timestamp_to_localized_str)
ENV_OPTION
YES_OPTIONdisplay_table)_Client)
ensure_env)OutputManager)_Queue)api_pb2queueTz8Manage `modal.Queue` objects and inspect their contents.)nameno_args_is_helphelpz-pz--partitionzNName of the partition to use, otherwise use the default (anonymous) partition.)r   create
Management)r   rich_help_panel)envr   r   c             	      s   t j| |dd}t I dH }t }t 4 I dH }t|||d}|||I dH  W d  I dH  dS 1 I dH s<w   Y  dS )zTCreate a named Queue.

    Note: This is a no-op when the Queue already exists.
    T)environment_namecreate_if_missingN)clientr   task_context)r   	from_namer   from_envr   r	   r   load)r   r   qr   resolvertcload_context r'   D/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/queues.pyr   !   s   .deleteFz--allow-missingz'Don't error if the Queue doesn't exist.)allow_missingyesr   r*   r+   c                   sB   t |}|stjd|  dddd tjj| ||dI dH  dS )z)Delete a named Queue and all of its data.z=Are you sure you want to irrevocably delete the modal.Queue ''?FTdefaultabort)r   r*   N)r   typerconfirmr   objectsr)   )r   r*   r+   r   r'   r'   r(   r)   0   s   

list)jsonr   r4   c           	   	      s  t t I dH  dg dtdtf fdd}|t  I dH }	 |r-n|d jj	j
I dH }q* fd	d
D }g }t|D ]-\}}| I dH }||jt|j
 | |jt|j|jkrpt|jnd f qJtg d||  dS )zList all named Queues.Ni created_beforereturnc                    sP   d}t j|| d}t j|d} j|I d H }|j t|j|k S )Nd   )max_objectsr5   )r   
paginationtotal_size_limit)r   ListPaginationQueueListRequeststub	QueueListextendqueueslen)r5   max_page_sizer9   reqrespr   r   itemsmax_total_sizer'   r(   retrieve_pageT   s   zlist_.<locals>.retrieve_pageTc                    s"   g | ]}t j|j |jd dqS )T)is_another_app)r   _new_hydratedqueue_idmetadata).0item)r   r'   r(   
<listcomp>b   s   " zlist_.<locals>.<listcomp>>)Namez
Created atz
Created by
Partitionsz
Total size)r   r   r!   floatboolr   now	timestamprM   creation_info
created_atzipinfoappendr   r   
created_bystrnum_partitions
total_sizer   )	r4   r   rH   finishedr@   rowsobj	resp_datar[   r'   rE   r(   list_D   s2   	re   clearz-az--allz%Clear the contents of all partitions.	partitionallc                   sD   t j| |d}|stjd|  dddd |j||dI dH  dS )	z:Clear the contents of a queue by removing all of its data.r   zIAre you sure you want to irrevocably delete the contents of modal.Queue 'r,   FTr-   )rg   rh   N)r   r    r0   r1   rf   )r   rg   rh   r+   r   r#   r'   r'   r(   rf   s   s   
peek
Inspection   nc                   s\   t j| |d}t }d}|j|d2 z3 dH W }|| |d7 }||kr* dS q6 dS )zIPrint the next N items in the queue or queue partition (without removal).ri   r   )rg   Nrl   )r   r    r   getiterateprint)r   rm   rg   r   r#   outputirO   r'   r'   r(   rj      s   
rA   z-tz--totalz:Compute the sum of the queue lengths across all partitionstotalc                   s2   t j| |d}t |j||dI dH  dS )zLPrint the length of a queue partition or the total length of all partitions.ri   )rg   rs   N)r   r    r   rn   rp   rA   )r   rg   rs   r   r#   r'   r'   r(   len_   s   
"rt   )+r   typingr   r0   r   r   r   modal._load_contextr   modal._resolverr   modal._utils.async_utilsr	   r
   modal._utils.time_utilsr   modal.cli.utilsr   r   r   modal.clientr   modal.environmentsr   modal.outputr   modal.queuer   modal_protor   	queue_cliPARTITION_OPTIONcommandcreate_blockingr^   r   rU   r)   re   rf   intrj   rt   r'   r'   r'   r(   <module>   s    "-