
    ]jX              	       H   d dl mZmZ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 d dlmZ d dlmZmZmZmZmZmZ d d	l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 Z%ddZ&d Z'	 ddeeeeeeef      dee   fdZ(deee)e)f      dee   fdZ*d Z+d Z,d Z-y)    )castOptionalListTupleUnion)ImproperlyConfigured)connections)SentinelConnectionPool)send_stop_job_command)	Execution)Job)DeferredJobRegistryFailedJobRegistryFinishedJobRegistryScheduledJobRegistryStartedJobRegistryclean_registries)Worker)clean_worker_registry   )get_connectionget_queue_by_indexget_scheduler)QUEUES_LIST)to_localtimec                 "   	 t        | j                        }y# t        $ r\ ddlm} | j
                  j                  |j                  | j                              }|t        |j                               cY S dcY S t        $ r
}Y d}~yd}~ww xY w)a  Checks whether there's a scheduler-lock on a particular queue, and returns the PID.
        It Only works with RQ's Built-in RQScheduler.
        When RQ-Scheduler is available returns False
        If not, it checks the RQ's RQScheduler for a scheduler lock in the desired queue
        Note: result might have some delay (1-15 minutes) but it helps visualizing whether the setup is working correctly
    Fr   )RQSchedulerN)r   namer   rq.schedulerr   
connectiongetget_locking_keyintdecode	Exception)queue	schedulerr   pides        9/root/env/lib/python3.12/site-packages/django_rq/utils.pyget_scheduler_pidr+      sz    !%**-	 >, "";#>#>uzz#JK$'Os3::< == s    AB8B<B	Bc           	      :   g }t        t              D ]  \  }}t        |      }|j                  }|j                  j
                  }| rt        |       t        |       |j                  |j                  d      }|r |j                  |j                  d            nd }|r%t        |j                        j                  d      }	nd}	|j                  dd        |j                  dd        |j                  dd        |j                   |j"                  |	||t%        |      d}
t'        |j                         }t)        j"                  |	      |
d
<   t+        |j                   |      }t-        |j                   |      }t/        |j                   |      }t1        |j                   |      }t3        |j                   |      }t5        |      |
d<   t5        |      |
d<   t5        |      |
d<   t5        |      |
d<   t5        |      |
d<   |j7                  |
        d|iS )Nr   zutf-8z%Y-%m-%d, %H:%M:%S-connection_poolparser_classretry)r   jobsoldest_job_timestampindexconnection_kwargsscheduler_pid)r&   workersfinished_jobsstarted_jobsdeferred_jobsfailed_jobsscheduled_jobsqueues)	enumerater   r   r    r.   r4   r   r   lindexkey	fetch_jobr$   r   enqueued_atstrftimepopr   countr+   r   r   r   r   r   r   r   lenappend)run_maintenance_tasksr<   r3   configr&   r    r4   last_job_idlast_jobr2   
queue_datafinished_job_registrystarted_job_registrydeferred_job_registryfailed_job_registryscheduled_job_registrys                   r*   get_statisticsrQ   0   s   F";/ 0"v"5)%%
&66HH U#!%( !''		15CN5??;#5#5g#>?TX#/0D0D#E#N#NOc#d #&  	/6nd3gt, JJKK$8!2.u5

 $EJJ/
 &5 9
9 3EJJ
 K1%**jI 3EJJ
 K/

JG!5ejj*!M&)*?&@
?#%()=%>
>"&)*?&@
?#$'(;$<
=!'*+A'B
#$j!a0"d f    c            	      J   i } t        t              D ]  \  }}t        |      }t        |j                  j
                  t              rD|j                  j
                  j                  j                  d   }|j
                  j                  }n |j                  j
                  j                  }|j                  dd       d|j                  dd       d|j                  dd       }|| vs	 t        |d	         }|j                         |d
| |<    d| iS # t        $ r Y w xY w)Nr   hostNOHOST:porti  /dbr   )rD   r3   
schedulers)r=   r   r   
isinstancer    r.   r
   sentinel_manager	sentinelsr4   r!   r   rD   r   )rZ   r3   rH   r&   first_sentinelr    conn_keyr'   s           r*   get_scheduler_statisticsr`   g   s#   J";/ v #5)e&&668NO"--==NNXXYZ[N'77IIJ))99KKJ nnVX67qPT9U8VVWXbXfXfgkmnXoWpq:%)&.9	&__."'
8$( *%% ( s   +$D	D"!D"Nregistryreturnc                     t        j                  || j                  | j                        }g }t	        |      D ]0  \  }}||s|j                  ||           |j                  |       2 |S )zFetch jobs in bulk from Redis.
    1. If job data is not present in Redis, discard the result
    2. If `registry` argument is supplied, delete empty jobs from registry
    )r    
serializer)r   
fetch_manyr    rd   r=   removerF   )r&   job_idsra   r1   
valid_jobsijobs          r*   get_jobsrk      sl    " >>'e.>.>5K[K[\DJD/ #3;
+c"# rR   composite_keysc                     g }|D ]7  \  }}	 |j                  t        j                  ||| j                               9 |S # t        $ r Y Gw xY w)znFetch executions in bulk from Redis.
    1. If execution data is not present in Redis, discard the result
    )idjob_idr    )rF   r   fetchr    
ValueError)r&   rl   
executionsro   rn   s        r*   get_executionsrs      s^     J$ 
	iooFuO_O_`a
   		s   1A	AAc                     t        |t        t        f      r|n|g}g }g }|D ]*  }	 t        | j                  |       |j                  |       , ||fS # t
        $ r |j                  |       Y Mw xY wN)r[   listtupler   r    r%   rF   )r&   rg   stopped_job_idsfailed_to_stop_job_idsro   s        r*   	stop_jobsrz      s    #GdE];g'GO '	!%"2"2F; 	v&' 222	  	"))&1	s   AA0/A0c                  V    t        j                         D ]  } | j                           y ru   )r	   allclose)cs    r*   reset_db_connectionsr      s!    __ 		rR   c                 P   ddl }|j                  dd      |j                  dd      |j                  j                  j	                         |j                  j
                  j                         |j                  j                  j                         gd} |j                  | fi | y)a  
    Configure the Sentry client.

    The **options kwargs are passed straight from the command
    invocation - options relevant to Sentry configuration are
    extracted.

    In addition to the 'debug' and 'ca_certs' options, which can
    be passed in as command options, we add the RqIntegration and
    DjangoIntegration to the config.

    Raises ImportError if the sentry_sdk is not available.

    r   Nsentry_debugFsentry_ca_certs)debugca_certsintegrations)

sentry_sdkr!   r   redisRedisIntegrationrqRqIntegrationdjangoDjangoIntegrationinit)
sentry_dsnoptionsr   sentry_optionss       r*   configure_sentryr      s     ^U3KK 148##))::<##&&446##**<<>
N JOOJ1.1rR   )Fru   ).typingr   r   r   r   r   django.core.exceptionsr   	django.dbr	   redis.sentinelr
   
rq.commandr   rq.executionsr   rq.jobr   rq.registryr   r   r   r   r   r   	rq.workerr   rq.worker_registrationr   r<   r   r   r   settingsr   templatetags.django_rqr   r+   rQ   r`   rk   strrs   rz   r   r    rR   r*   <module>r      s    5 5 7 ! 1 , #    8 E E ! 0.4n&H 	  	 	
 
#Y:
$uS#X*? 
DO 
3
2rR   