o
    Zh                     @   s  d dl m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mZ d d	lmZ d d
lmZmZ d dlmZmZmZmZ zd dlZd dlmZ d dlm Z  d dlm!Z!m"Z"m#Z#m$Z$ W n e%ys   edw erd dl&m'Z'm(Z(m)Z)m*Z* d dlm+Z+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4 d dlm5Z5 e!e"e#fZ6G dd deZ7dd Z8dd Z9dd Z:dd Z;d d! Z<d"d# Z=d$d% Z>d&d' Z?dS )(    )absolute_importN)reraise)TYPE_CHECKING)Hub)OP)_should_send_default_pii)DidNotEnableIntegration)ignore_logger)TransactionTRANSACTION_SOURCE_TASK)capture_internal_exceptionsevent_from_exceptionSENSITIVE_DATA_SUBSTITUTEparse_version)VERSION)ArqRedis)JobExecutionFailedRetryRetryJobWorkerzArq is not installed)AnyDictOptionalUnion)EventProcessorEventExcInfoHint)CronJob)Job)WorkerCoroutine)Functionc                   @   s   e Zd ZdZedd ZdS )ArqIntegrationarqc               	   C   s   zt ttrtt} ntjd d } W n ttfy    d } Y nw | d u r,tdt| dk r4tdt	  t
  t  td d S )N   zUnparsable arq version: {})r      zarq 0.23 or newer required.z
arq.worker)
isinstanceARQ_VERSIONstrr   version	TypeError
ValueErrorr   formatpatch_enqueue_jobpatch_run_jobpatch_create_workerr
   )r*    r1   R/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/arq.py
setup_once,   s    

zArqIntegration.setup_onceN)__name__
__module____qualname__
identifierstaticmethodr3   r1   r1   r1   r2   r#   )   s    r#   c                        t j  fdd} | t _d S )Nc                    s   t j}|td u r | |g|R i |I d H S |jtj|d  | |g|R i |I d H W  d    S 1 s<w   Y  d S )N)opdescription)r   currentget_integrationr#   Z
start_spanr   ZQUEUE_SUBMIT_ARQ)selffunctionargskwargshubZold_enqueue_jobr1   r2   _sentry_enqueue_jobJ   s   $z.patch_enqueue_job.<locals>._sentry_enqueue_job)r   Zenqueue_job)rD   r1   rC   r2   r.   F   s   

r.   c                     r9   )Nc              	      s   t t j}|td u r | ||I d H S | >}d|_|  tddtj	t
d}||  | ||I d H W  d    W  d    S 1 sKw   Y  W d    d S 1 s[w   Y  d S )Nr$   zunknown arq taskok)namestatusr:   source)r   r<   r=   r#   Z
push_scope_nameZclear_breadcrumbsr   r   ZQUEUE_TASK_ARQr   Zstart_transaction)r>   job_idZscorerB   scopetransactionZold_run_jobr1   r2   _sentry_run_job[   s&   

"z&patch_run_job.<locals>._sentry_run_job)r   Zrun_job)rN   r1   rM   r2   r/   W   s   
r/   c                 C   sx   t j}|jjd ur| d tv r|jjd d S |jjd t| |jr(|jjnd t	j
ddd\}}|j||d d S )Nr   ZabortedZinternal_errorF)typeZhandled)Zclient_optionsZ	mechanism)hint)r   r<   rK   rL   ARQ_CONTROL_FLOW_EXCEPTIONSZ
set_statusr   clientoptionsr#   r7   Zcapture_event)exc_inforB   eventrP   r1   r1   r2   _capture_exceptions   s   

rV   c                    s    fdd}|S )Nc                    s   t j}t M |jjd urd |jj_d | d< | di }d |d< d dk|d< | d	i }d t r< ntt rBntd d
|d< W d    | S 1 sVw   Y  | S )Njob_namerL   tagsrJ   Zarq_task_idZjob_try   Zarq_task_retryextra)taskr@   rA   retryzarq-job)	r   r<   r   rK   rL   rF   
setdefaultr   r   )rU   rP   rB   rX   rZ   r@   ctxrA   r1   r2   event_processor   s$   
z._make_event_processor.<locals>.event_processorr1   )r_   r@   rA   r`   r1   r^   r2   _make_event_processor   s   ra   c                    s    fdd}|S )Nc                    s   t j}|td u r | g|R i |I d H S |jti | dig|R i | z | g|R i |I d H }W |S  tyU   t	 }t
| t|  Y |S w )NrW   )r   r<   r=   r#   rK   Zadd_event_processorra   	ExceptionsysrT   rV   r   )r_   r@   rA   rB   resultrT   	coroutinerF   r1   r2   _sentry_coroutine   s     
z*_wrap_coroutine.<locals>._sentry_coroutiner1   )rF   rf   rg   r1   re   r2   _wrap_coroutine   s   rh   c                     s    t jj  fdd} | t j_d S )Nc                     s   t j}|td u r | i |S | d }t|dr#dd |jD |_t|dr1dd |jD |_d|v r@dd |d D |d< d|v rOdd |d D |d<  | i |S )	Nr   	functionsc                 S      g | ]}t |qS r1   _get_arq_function.0funcr1   r1   r2   
<listcomp>       zFpatch_create_worker.<locals>._sentry_create_worker.<locals>.<listcomp>	cron_jobsc                 S   rj   r1   _get_arq_cron_jobrn   cron_jobr1   r1   r2   rp      rq   c                 S   rj   r1   rk   rm   r1   r1   r2   rp      rq   c                 S   rj   r1   rs   ru   r1   r1   r2   rp      rq   )r   r<   r=   r#   hasattrri   rr   )r@   rA   rB   Zsettings_clsZold_create_workerr1   r2   _sentry_create_worker   s*   



z2patch_create_worker.<locals>._sentry_create_worker)r$   workerZcreate_worker)ry   r1   rx   r2   r0      s   r0   c                 C   s    t j| }t|j|j|_|S N)r$   rz   ro   rh   rF   rf   )ro   Zarq_funcr1   r1   r2   rl      s   rl   c                 C   s   t | j| j| _| S r{   )rh   rF   rf   )rv   r1   r1   r2   rt      s   rt   )@
__future__r   rc   Zsentry_sdk._compatr   Zsentry_sdk._typesr   Z
sentry_sdkr   Zsentry_sdk.constsr   Zsentry_sdk.hubr   Zsentry_sdk.integrationsr   r	   Zsentry_sdk.integrations.loggingr
   Zsentry_sdk.tracingr   r   Zsentry_sdk.utilsr   r   r   r   Z
arq.workerr$   Zarq.versionr   r(   Zarq.connectionsr   r   r   r   r   ImportErrortypingr   r   r   r   r   r   r   r   Zarq.cronr   Zarq.jobsr    Z
arq.typingr!   r"   rQ   r#   r.   r/   rV   ra   rh   r0   rl   rt   r1   r1   r1   r2   <module>   sH    
 $