o
    Df]j                     @   s  d Z ddlZddlZddlmZ ddlmZmZ ddl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 ddlmZ ddlmZmZmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z"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/m0Z0 ddl1m2Z2m3Z3m4Z4 ddl5m6Z6 dZ7e8edZ9e,e:Z;e;j<e;j=e;j>e;j?f\Z<Z=Z@Z?daAdaBdd ZCeC  e4jDZDejEjFZGejHjFZIe6jJZJe6jKZKe6jLZMe6jNZNG dd dZOeeMeKdefddZPdS ) zfTask request.

This module defines the :class:`Request` class, that specifies
how tasks are executed.
    N)datetime)	monotonictime)ref)TERM_SIGNAME)ExceptionWithTraceback)	safe_reprsafe_str)cached_property)current_appsignals)Context)fast_trace_task
trace_tasktrace_task_ret)BasePool)IgnoreInvalidTaskErrorRejectRetryTaskRevokedError
TerminatedTimeLimitExceededWorkerLostError)r   )maybe
maybe_listnoop)
get_logger)gethostname)get_pickled_exception)maybe_iso8601maybe_make_awaretimezone   )state)Requestpypy_version_infoFc                   C   s   t tjat tjad S N)loggerisEnabledForloggingDEBUG_does_debugINFO
_does_info r/   r/   N/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/worker/request.py__optimize__.   s   r1   c                   @   sF  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZesdZeddddddeddddeefddZed	d
 Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Z ed#d$ Z!ed%d& Z"ed'd( Z#ed)d* Z$e$j%d+d* Z$ed,d- Z&ed.d/ Z'ed0d1 Z(e(j%d2d1 Z(ed3d4 Z)ed5d6 Z*ed7d8 Z+ed9d: Z,e,j%d;d: Z,ed<d= Z-ed>d? Z.ed@dA Z/e/j%dBdA Z/edCdD Z0e0j%dEdD Z0edFdG Z1edHdI Z2edJdK Z3edLe4fdMdNZ5edLe6fdOdPZ7edQdR Z8dSe9fdTdUZ:ddVdWZ;dXdY Z<ddZd[Z=dd\d]Z>d^d_ Z?d`da Z@dbdc ZAddde ZBdfdg ZCdhdi ZDdjdk ZEdldm ZFddndoZGdpdq ZHddrdsZIddtduZJdvdw ZKdxdy ZLdzd{ ZMeNd|d} ZOeNd~d ZPeNdd ZQeNdd ZReNdd ZSeNdd ZTdS )r%   zA request for task execution.FN)NN)_app_typenameid_root_id
_parent_id_on_ack_body	_hostname_eventer_connection_errors_task_eta_expires_request_dict
_on_reject_utc_content_type_content_encoding	_argsrepr_kwargsrepr_args_kwargs_decoded	__payload__weakref____dict__Tc              
   K   s  || _ |d u r|j n| | _|
d u r|jn|
| _|| _|| _|| _|r-d  | _	| _
n	|j|j| _	| _
| jr<| jn|j| _| jd | _| jd  | _| _d| jv r]| jd p[| j| _| jd| _| jd| _| jdd }|rw|| _| jdd| _| jd	d| _|| _|	| _|pt | _|| _|pd
| _|p| jj| j | _| jdd| _ | jd}|d urz||}W n t!t"t#fy } z
t$d|d| d }~ww ||| j%| _&nd | _&| jd}|d urz||}W n t!t"t#fy } z
t$d|d| d }~ww ||| j%| _'nd | _'|j(pi }|j)p$i }|d|d|d|ddd| _*| j+||d|d| j| j*d | j\| jd< | jd< }| jd | _,| jd | _-d S )Nr5   taskshadowroot_id	parent_id	timelimitargsrepr 
kwargsreprr/   ignore_resultFetazinvalid ETA value z: expireszinvalid expires value exchangerouting_keypriorityredelivered)rX   rY   rZ   r[   reply_tocorrelation_id)
propertiesr\   r]   hostnamedelivery_infoargskwargs)._messageheaderscopyr@   bodyr9   r2   rB   rI   rC   rD   content_typecontent_encodingpayload_Request__payloadr5   r3   r4   getr6   r7   time_limitsrE   rF   r8   rA   r   r:   r;   r<   tasksr=   _ignore_resultAttributeError
ValueError	TypeErrorr   tzlocalr>   r?   r`   r^   _delivery_infoupdaterG   rH   )selfmessageon_ackr_   eventerappconnection_errorsrequest_dictrM   	on_rejectrf   rd   decodedutcr!   r    optsrQ   rV   excrW   r`   r^   _r/   r/   r0   __init__Z   s   




zRequest.__init__c                 C      | j S r'   )rs   ru   r/   r/   r0   r`         zRequest.delivery_infoc                 C   r   r'   )rc   r   r/   r/   r0   rv      r   zRequest.messagec                 C   r   r'   r@   r   r/   r/   r0   r{      r   zRequest.request_dictc                 C   r   r'   )r9   r   r/   r/   r0   rf      r   zRequest.bodyc                 C   r   r'   )r2   r   r/   r/   r0   ry      r   zRequest.appc                 C   r   r'   )rB   r   r/   r/   r0   r~      r   zRequest.utcc                 C   r   r'   )rC   r   r/   r/   r0   rg      r   zRequest.content_typec                 C   r   r'   )rD   r   r/   r/   r0   rh      r   zRequest.content_encodingc                 C   r   r'   )r3   r   r/   r/   r0   type   r   zRequest.typec                 C   r   r'   )r6   r   r/   r/   r0   rO      r   zRequest.root_idc                 C   r   r'   )r7   r   r/   r/   r0   rP      r   zRequest.parent_idc                 C   r   r'   )rE   r   r/   r/   r0   rR      r   zRequest.argsreprc                 C   r   r'   )rG   r   r/   r/   r0   ra      r   zRequest.argsc                 C   r   r'   )rH   r   r/   r/   r0   rb      r   zRequest.kwargsc                 C   r   r'   )rF   r   r/   r/   r0   rT      r   zRequest.kwargsreprc                 C   r   r'   )r8   r   r/   r/   r0   rw      r   zRequest.on_ackc                 C   r   r'   rA   r   r/   r/   r0   r|      r   zRequest.on_rejectc                 C   
   || _ d S r'   r   ru   valuer/   r/   r0   r|         
c                 C   r   r'   )r:   r   r/   r/   r0   r_      r   zRequest.hostnamec                 C   r   r'   )rn   r   r/   r/   r0   rU      r   zRequest.ignore_resultc                 C   r   r'   r;   r   r/   r/   r0   rx      r   zRequest.eventerc                 C   r   r'   r   )ru   rx   r/   r/   r0   rx     r   c                 C   r   r'   )r<   r   r/   r/   r0   rz     r   zRequest.connection_errorsc                 C   r   r'   )r=   r   r/   r/   r0   rM   
  r   zRequest.taskc                 C   r   r'   )r>   r   r/   r/   r0   rV     r   zRequest.etac                 C   r   r'   r?   r   r/   r/   r0   rW     r   zRequest.expiresc                 C   r   r'   r   r   r/   r/   r0   rW     r   c                 C   s   | j d u r| jjj| _ | j S r'   )_tzlocalr2   confr"   r   r/   r/   r0   rr     s   
zRequest.tzlocalc                 C   s   | j j p| j jS r'   )rM   rU   store_errors_even_if_ignoredr   r/   r/   r0   store_errors   s   
zRequest.store_errorsc                 C   r   r'   r5   r   r/   r/   r0   task_id%     zRequest.task_idc                 C   r   r'   r   r   r/   r/   r0   r   *  r   c                 C   r   r'   r4   r   r/   r/   r0   	task_name.  r   zRequest.task_namec                 C   r   r'   r   r   r/   r/   r0   r   3  r   c                 C   
   | j d S )Nr\   r   r   r/   r/   r0   r\   7     
zRequest.reply_toc                 C   s   | j ddS )Nreplaced_task_nestingr   r@   rk   r   r/   r/   r0   r   <     zRequest.replaced_task_nestingc                 C   s   | j dg S )Ngroupsr   r   r/   r/   r0   r   @  r   zRequest.groupsreturnc                 C   s   | j dpg S )Nstamped_headersr   r   r/   r/   r0   r   D  s   zRequest.stamped_headersc                    s$   | j dpi   fdd| jD S )Nstampsc                    s   i | ]}|  |qS r/   )rk   ).0headerr   r/   r0   
<dictcomp>K  s    z"Request.stamps.<locals>.<dictcomp>)r@   rk   r   r   r/   r   r0   r   H  s   zRequest.stampsc                 C   r   )Nr]   r   r   r/   r/   r0   r]   M  r   zRequest.correlation_idpoolc           	      K   s   | j }| j}|  rt|| j\}}| jjrtnt}|j	|| j
|| j| j| j| jf| j| j| j| j|p6|j|p:|j|d	}tt|| _|S )a  Used by the worker to send this task to the pool.

        Arguments:
            pool (~celery.concurrency.base.TaskPool): The execution pool
                used to execute this request.

        Raises:
            celery.exceptions.TaskRevokedError: if the task was revoked.
        ra   accept_callbacktimeout_callbackcallbackerror_callbacksoft_timeouttimeoutr]   )r5   r=   revokedr   rl   r2   use_fast_trace_taskr   r   apply_asyncr3   r@   r9   rC   rD   on_accepted
on_timeout
on_success
on_failuresoft_time_limit
time_limitr   r   _apply_result)	ru   r   rb   r   rM   r   r   traceresultr/   r/   r0   execute_using_poolR  s*   

zRequest.execute_using_poolc              
   C   s   |   rdS | jjs|   | j\}}}| j}|j||ddfi |p#i  t| j| j| j	| j
|| j| jj| jd\}}}}|rH| jdd |S |   |S )zExecute the task in a :func:`~celery.app.trace.trace_task`.

        Arguments:
            loglevel (int): The loglevel used by the task.
            logfile (str): The logfile used by the task.
        NF)loglevellogfileis_eager)r_   loaderry   requeue)r   rM   	acks_lateacknowledge_payloadr@   rt   r   r5   rG   rH   r:   r2   r   reject)ru   r   r   r   embedrequestretvalIr/   r/   r0   executes  s,   
zRequest.executec                 C   s6   | j rt| j j}|| j krt| j dS dS dS )z%If expired, mark the task as revoked.TN)rW   r   nowtzinforevoked_tasksaddr5   )ru   r   r/   r/   r0   maybe_expire  s   
zRequest.maybe_expirec                 C   sn   t |pt}| jr|| j| | dd|d n||f| _| jd ur3|  }|d ur5|	| d S d S d S )N
terminatedTF)
_signalssignumr   
time_startterminate_job
worker_pid_announce_revoked_terminate_on_ackr   	terminateru   r   signalobjr/   r/   r0   r     s   

zRequest.terminatec                 C   sZ   t |pt}| jr|| j| |   | jd ur)|  }|d ur+|| d S d S d S r'   )	r   r   r   r   r   r   _announce_cancelledr   r   r   r/   r/   r0   cancel  s   
zRequest.cancelc                 C   sn   t |  | d d}t|d}| jjj| j|| jd | j|| j| j	| j
d  d| _t| j| jd d d S )Nztask-cancelledzcancelled by Celery)rv   )r   T)r   einfo)
task_ready
send_eventr   rM   backendmark_as_retryr5   _contexton_retryra   rb   _already_cancelled
send_retry)ru   reasonr   r/   r/   r0   r     s   

zRequest._announce_cancelledc                 C   s^   t |  | jd|||d | jjj| j|| j| jd |   d| _	t
| j| j|||d d S )Nztask-revoked)r   r   expiredr   store_resultT)r   r   r   r   )r   r   rM   r   mark_as_revokedr5   r   r   r   _already_revokedsend_revoked)ru   r   r   r   r   r/   r/   r0   r     s   


zRequest._announce_revokedc           
      C   s  d}| j rdS | jr|  }| jtv }d\}}|s_| jr_| jD ]?}|tv r^t| }| jjd | }t	|t
tfrL|D ]}|t|v rJd}||i} nq:nt|t|v ||kg}||i} nqt|||frd}	|rp|	d| 7 }	t|	| j| j | |r~dnddd	| dS dS )
z%If revoked, skip task and mark state.FT)FNr   zDiscarding revoked task: %s[%s]z (revoked by header: %s)r   r   N)r   rW   r   r5   r   r   revoked_stampsrc   rd   
isinstancelisttupler   anyinfor4   r   )
ru   r   revoked_by_idrevoked_by_headerrevoking_headerstamprevoked_headerstamped_headerstamped_valuelog_msgr/   r/   r0   r     sJ   



zRequest.revokedc                 K   s@   | j r| j jr| jjr| j j|fd| ji| d S d S d S d S )Nuuid)r;   enabledrM   send_eventssendr5   )ru   r   fieldsr/   r/   r0   r     s   zRequest.send_eventc                 C   sn   || _ t t |  | _t|  | jjs|   | d t	r(t
d| j| j| | jdur5| j| j  dS dS )z4Handler called when task is accepted by worker pool.ztask-startedzTask accepted: %s[%s] pid:%rN)r   r   r   r   task_acceptedrM   r   r   r   r,   debugr4   r5   r   r   )ru   pidtime_acceptedr/   r/   r0   r     s   

zRequest.on_acceptedc                 C   s|   |rt d|| j| j dS t|  td|| j| j t|}| jjj| j|| j	| j
d | jjr:| jjr<|   dS dS dS )z%Handler called if the task times out.z)Soft time limit (%ss) exceeded for %s[%s]z)Hard time limit (%ss) exceeded for %s[%s]r   N)warnr4   r5   r   errorr   rM   r   mark_as_failurer   r   r   acks_on_failure_or_timeoutr   )ru   softr   r   r/   r/   r0   r   	  s    


zRequest.on_timeoutc                 K   st   |\}}}|r"|j }t|tr|j}t|ttfr|| j|ddS t| dd | jj	r0| 
  | jd||d dS )z6Handler called if the task was successfully processed.T	return_ok)
successfultask-succeededr   runtimeN)	exceptionr   r   r   
SystemExitKeyboardInterruptr   r   rM   r   r   r   ru   failed__retval__runtimerb   failedr   r  r   r/   r/   r0   r     s   

zRequest.on_successc                 C   s2   | j jr|   | jdt|jjt|jd dS )z-Handler called if the task should be retried.ztask-retriedr  	tracebackN)	rM   r   r   r   r   r  r   r	   r  )ru   exc_infor/   r/   r0   r   -  s   

zRequest.on_retryc           
   	   C   s  t |  |j}t|tr|j}t|t}|r(| js&| js&| ddt	|d dS t|t
r4t
d| t|tr@| j|jdS t|trI|  S t|trS| |S d}t|t}| jjr| jjoc|}| jj}	|rud}| j|d d}n|	r||   n| jdd |s|s|s| jjj| j|| j| jd tjj| j| j|| j| j |j!|d |r| j"d	t#t$|j|j!d
 |st%d||j&d dS dS )z/Handler called if the task raised an exception.r   TFNzProcess got: r   r   )senderr   r  ra   rb   r  r   ztask-failedr  zTask handler raised error: %r)r  )'r   r  r   r   r   r   r   r   r   strMemoryErrorr   r   r   r   r   r   r   r   rM   r   reject_on_worker_lostr  r   r  r5   r   r   r   task_failurer   ra   rb   r  r   r   r   r  r  )
ru   r  send_failed_eventr	  r   is_terminatedr   is_worker_lostr   ackr/   r/   r0   r   6  sn   











zRequest.on_failurec                 C   s"   | j s| t| j d| _ dS dS )zAcknowledge task.TN)acknowledgedr8   r(   r<   r   r/   r/   r0   r     s   
zRequest.acknowledgec                 C   s2   | j s| t| j| d| _ | jd|d d S d S )NTztask-rejectedr   )r   rA   r(   r<   r   )ru   r   r/   r/   r0   r     s
   zRequest.rejectc                 C   sB   | j | j|s	| jn| j|s| jn| j| j| j| j| j	| j
| jd
S )N)
r5   r4   ra   rb   r   r_   r   r   r`   r   )r5   r4   rG   rE   rH   rF   r3   r:   r   r   r`   r   )ru   safer/   r/   r0   r     s   zRequest.infoc                 C   s
   d | S )Nz{0.name}[{0.id}])formatr   r/   r/   r0   	humaninfo  s   
zRequest.humaninfoc                 C   s@   d |  | jrd| j dnd| jrd| j dndg S )z``str(self)``. z ETA:[]rS   z
 expires:[)joinr#  r>   r?   stripr   r/   r/   r0   __str__  s   zRequest.__str__c                 C   s   d t| j|  | j| jS )z``repr(self)``.z<{}: {} {} {}>)r"  r   __name__r#  rE   rF   r   r/   r/   r0   __repr__  s   zRequest.__repr__c                 C   r   r'   )rj   r   r/   r/   r0   r     r   zRequest._payloadc                 C      | j \}}}|dS )Nchordr   rk   ru   r   r   r/   r/   r0   r,       
zRequest.chordc                 C   r+  )Nerrbacksr-  r.  r/   r/   r0   r0    r/  zRequest.errbacksc                 C      | j dS )Ngroupr   r   r/   r/   r0   r2    s   zRequest.groupc                 C   s.   | j }| j\}}}|jdi |pi  t|S )z9Context (:class:`~celery.app.task.Context`) of this task.Nr/   )r@   r   rt   r   )ru   r   r   r   r/   r/   r0   r     s   zRequest._contextc                 C   r1  )Ngroup_indexr   r   r/   r/   r0   r3    s   zRequest.group_indexr'   )TF)F)Ur)  
__module____qualname____doc__r   r   r   rl   r   r   r   r   r   IS_PYPY	__slots__r   r!   r    r   propertyr`   rv   r{   rf   ry   r~   rg   rh   r   rO   rP   rR   ra   rb   rT   rw   r|   setterr_   rU   rx   rz   rM   rV   rW   rr   r   r   r   r\   r   r   r   r   dictr   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#  r(  r*  r
   r   r,  r0  r2  r   r3  r/   r/   r/   r0   r%   C   s   	
T





































!"

)
	P






	r%   c
              	      s^   |j |j|j|j |o|jd u r|	jrtntG  fddd| }
|
S )Nc                       s0   e Zd ZfddZ fddZdS )z#create_request_cls.<locals>.Requestc                    sp   | j }|  rt|| j\}} | j|| j| j| j| jf| j	| j
| j| j|p)|p,|d	}t|| _|S )Nr   )r   r   r   rl   r   r{   rf   rg   rh   r   r   r   r   r   r   )ru   r   rb   r   r   r   r   )r   default_soft_time_limitdefault_time_limitr   r   r/   r0   r     s&   
z6create_request_cls.<locals>.Request.execute_using_poolc                    st   |\}}}|r"|j }t|tr|j}t|ttfr|| j|ddS |   r,|   r8| jd||d d S d S )NTr  r  r  )	r  r   r   r   r  r  r   r   r   r  )r   eventsr   r/   r0   r     s    


z.create_request_cls.<locals>.Request.on_successN)r)  r4  r5  r   r   r/   r   r   r<  r=  r>  r   r   r   r/   r0   r%     s    r%   )r   r   r   r   r   r   r   r   )baserM   r   r_   rx   r   r   r   r   ry   r%   r/   r?  r0   create_request_cls  s   
",rA  )Qr6  r*   sysr   r   r   weakrefr   billiard.commonr   billiard.einfor   kombu.utils.encodingr   r	   kombu.utils.objectsr
   celeryr   r   celery.app.taskr   celery.app.tracer   r   r   celery.concurrency.baser   celery.exceptionsr   r   r   r   r   r   r   r   celery.platformsr   celery.utils.functionalr   r   r   celery.utils.logr   celery.utils.nodenamesr   celery.utils.serializationr   celery.utils.timer    r!   r"   rS   r$   __all__hasattrr7  r)  r(   r   r   warningr  r  r.   r,   r1   tz_or_localtask_revokedr   r   
task_retryr   r   r   r   r   r   r%   rA  r/   r/   r/   r0   <module>   s`    (
     !