o
    wvXjp  ã                   @   s²   d Z ddlmZmZ ddlZddl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 d	d
lmZ dZeeƒZejejejZZZG dd„ deƒZG dd„ deƒZdS )zWorker Pidbox (remote control).é    )Úabsolute_importÚunicode_literalsN)Úignore_errors)Úsafe_str)ÚAttributeDict)Úpass1)Ú
get_loggeré   )Úcontrol)ÚPidboxÚgPidboxc                   @   sT   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )r   zWorker mailbox.Nc              	   C   s^   || _ |j| _|jjjjt|jƒtjjt	|j|j||j
jrtntd�d�| _| j jjj| _d S )N)ÚappÚhostnameÚconsumerÚtset)ÚhandlersÚstate)Úcr   r   r
   ÚmailboxÚNoder   ÚPanelÚdatar   Ú
controllerÚuse_eventloopr   ÚsetÚnodeÚclockÚforwardÚ_forward_clock©Úselfr   © r!   úQ/var/www/html/myproject/venv/lib/python3.10/site-packages/celery/worker/pidbox.pyÚ__init__   s   
üý	zPidbox.__init__c              
   C   sˆ   |   ¡  z
| j ||¡ W d S  ty& } ztd|ƒ W Y d }~d S d }~w tyC } ztd|dd� |  ¡  W Y d }~d S d }~ww )NzNo such control command: %szControl command error: %rT)Úexc_info)r   r   Úhandle_messageÚKeyErrorÚerrorÚ	ExceptionÚreset)r    ÚbodyÚmessageÚexcr!   r!   r"   Ú
on_message)   s   €€þzPidbox.on_messagec                 C   s.   |j  ¡ | j_| jj| jd�| _|j| j_d S ©N)Úcallback)Ú
connectionÚchannelr   Úlistenr-   r   Úon_decode_errorr   r!   r!   r"   Ústart5   s   zPidbox.startc                 C   s   d S ©Nr!   ©r    r!   r!   r"   Úon_stop:   s   zPidbox.on_stopc                 C   s   |   ¡  |  |¡| _d S r5   )r7   Ú_close_channelr   r   r!   r!   r"   Ústop=   s   zPidbox.stopc                 C   s   |   | j¡ |  | j¡ d S r5   )r9   r   r4   r6   r!   r!   r"   r)   A   s   zPidbox.resetc                 C   s*   | j r| j jrt|| j jjƒ d S d S d S r5   )r   r1   r   Úcloser   r!   r!   r"   r8   E   s   ÿzPidbox._close_channelc                 C   s4   |   ¡  | jrtdƒ t|| jjƒ |  | j¡ d S )NzCanceling broadcast consumer...)r7   r   Údebugr   Úcancelr9   r   r   r!   r!   r"   ÚshutdownI   s
   zPidbox.shutdown)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r-   r4   r7   r9   r)   r8   r=   r!   r!   r!   r"   r      s    r   c                   @   sD   e Zd ZdZdZdZdZdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ ZdS )r   zWorker pidbox (greenlet).Nr   c                 C   s   |j  | j|¡ d S r5   )ÚpoolÚspawn_nÚloopr   r!   r!   r"   r4   X   s   zgPidbox.startc                 C   s6   | j r| j ¡  tdƒ | j  ¡  d  | _ | _d S d S )Nz+Waiting for broadcast thread to shutdown...)Ú_node_stoppedÚ_node_shutdownr   r;   Úwaitr6   r!   r!   r"   r7   [   s   

üzgPidbox.on_stopc                 C   s   |  j d7  _ d S )Nr	   )Ú_resetsr6   r!   r!   r"   r)   b   s   zgPidbox.resetc                 C   s6   |   |¡ | ¡ | j_| jj| jd�| _| j ¡  d S r.   )r8   r1   r   r2   r-   r   Úconsume)r    r   r0   r!   r!   r"   Ú	_do_resete   s   
zgPidbox._do_resetc              	   C   sú   | j g}t ¡  }| _t ¡  }| _ze| ¡ �K}td| ¡ ƒ |  ||¡ | 	¡ s[|j
r[|d | j k rA|d  d7  < |  ||¡ z|jdd� W n
 tjyS   Y nw | 	¡ s[|j
s,W d   ƒ n1 sew   Y  W | ¡  d S W | ¡  d S | ¡  w )Nzpidbox: Connected to %s.r   r	   g      ð?)Útimeout)rH   Ú	threadingÚEventrF   rE   Úconnection_for_readÚinfoÚas_urirJ   Úis_setr0   Údrain_eventsÚsocketrK   r   )r    r   Úresetsr=   Ústoppedr0   r!   r!   r"   rD   k   s,   
ÿú€ýôzgPidbox.loop)r>   r?   r@   rA   rF   rE   rH   r4   r7   r)   rJ   rD   r!   r!   r!   r"   r   Q   s    r   )rA   Ú
__future__r   r   rS   rL   Úkombu.commonr   Úkombu.utils.encodingr   Úcelery.utils.collectionsr   Úcelery.utils.functionalr   Úcelery.utils.logr   Ú r
   Ú__all__r>   Úloggerr;   r'   rO   Úobjectr   r   r!   r!   r!   r"   Ú<module>   s    ;