o
    h                     @   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   ^/var/www/html/aiguide_backend/venv/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#   
start_spanr   QUEUE_SUBMIT_ARQ)selffunctionargskwargshubold_enqueue_jobr1   r2   _sentry_enqueue_jobJ   s   $z.patch_enqueue_job.<locals>._sentry_enqueue_job)r   enqueue_job)rG   r1   rE   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#   
push_scope_nameclear_breadcrumbsr   r   QUEUE_TASK_ARQr   start_transaction)r@   job_idscorerD   scopetransactionold_run_jobr1   r2   _sentry_run_job[   s&   

"z&patch_run_job.<locals>._sentry_run_job)r   run_job)rX   r1   rV   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   abortedinternal_errorF)typehandled)client_options	mechanism)hint)r   r<   rT   rU   ARQ_CONTROL_FLOW_EXCEPTIONS
set_statusr   clientoptionsr#   r7   capture_event)exc_inforD   eventr`   r1   r1   r2   _capture_exceptions   s   

rh   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_namerU   tagsrR   arq_task_idjob_try   arq_task_retryextra)taskrB   rC   retryzarq-job)	r   r<   r   rT   rU   rJ   
setdefaultr   r   )rg   r`   rD   rj   ro   rB   ctxrC   r1   r2   event_processor   s$   
z._make_event_processor.<locals>.event_processorr1   )rt   rB   rC   ru   r1   rs   r2   _make_event_processor   s   rv   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 )Nri   )r   r<   r=   r#   rT   add_event_processorrv   	Exceptionsysrf   rh   r   )rt   rB   rC   rD   resultrf   	coroutinerJ   r1   r2   _sentry_coroutine   s     
z*_wrap_coroutine.<locals>._sentry_coroutiner1   )rJ   r|   r}   r1   r{   r2   _wrap_coroutine   s   r~   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   r   r1   _get_arq_cron_jobr   cron_jobr1   r1   r2   r      r   c                 S   r   r1   r   r   r1   r1   r2   r      r   c                 S   r   r1   r   r   r1   r1   r2   r      r   )r   r<   r=   r#   hasattrr   r   )rB   rC   rD   settings_clsold_create_workerr1   r2   _sentry_create_worker   s*   



z2patch_create_worker.<locals>._sentry_create_worker)r$   workercreate_worker)r   r1   r   r2   r0      s   r0   c                 C   s    t j| }t|j|j|_|S N)r$   r   r   r~   rJ   r|   )r   arq_funcr1   r1   r2   r      s   r   c                 C   s   t | j| j| _| S r   )r~   rJ   r|   )r   r1   r1   r2   r      s   r   )@
__future__r   ry   sentry_sdk._compatr   sentry_sdk._typesr   
sentry_sdkr   sentry_sdk.constsr   sentry_sdk.hubr   sentry_sdk.integrationsr   r	   sentry_sdk.integrations.loggingr
   sentry_sdk.tracingr   r   sentry_sdk.utilsr   r   r   r   
arq.workerr$   arq.versionr   r(   arq.connectionsr   r   r   r   r   ImportErrortypingr   r   r   r   r   r   r   r   arq.cronr   arq.jobsr    
arq.typingr!   r"   ra   r#   r.   r/   rh   rv   r~   r0   r   r   r1   r1   r1   r2   <module>   sH    
 $