%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/
Upload File :
Create Path :
Current File : //opt/alt/python37/lib/python3.7/site-packages/ssa/modules/__pycache__/processor.cpython-37.pyc

B

r�b}%�@s�dZddlZddlZddlZddlZddlZddlZddlmZmZm	Z	ddl
mZmZm
Z
ddlmZmZddlmZddlmZdd	lmZdd
lmZddlmZdd
lmZddlmZmZm Z eGdd�de��Z!dS)z-
This module contains RequestProcessor class
�N)�datetime�	timedelta�date)�Thread�RLock�current_thread)�Callable�Any�)�
AutoTracer)�Common)�
DecisionMaker)�StatisticsSender�)�storage_file)�SSAError)�	singleton�	url_split�switch_schedstatscseZdZdZ�fdd�Zedd��Zed�dd�Zd	d
�dd�Z	d	d
�d
d�Z
eed�dd�Z
d-ed�dd�Zdd�Zed
�dd�Zeeed�dd��Zd	d
�dd�Zeed	d�d d!�Zeed	d"�d#d$�Zeeeeed	d%�d&d'�Zed	d(�d)d*�Zd+d,�Z�ZS).�RequestProcessorz^
    SSA Request processor implementation.
    Only one instance is allowed to be created
    cstt���t�d�|_|j�dt�tdd�|��\|_	}|sH|�
�t�|_t
�|_t�|_t�|_|��dS)NZ
req_processorzProcessor enabled: %sT)�enabled)�super�__init__�logging�	getLogger�logger�info�__package__rZ
load_stats�total_struct�cleanupr�_lockr
�decision_makerr�senderr�auto_tracer�start_background_routine)�selfZtotal_struct_date)�	__class__��F/opt/alt/python37/lib/python3.7/site-packages/ssa/modules/processor.pyr)s

zRequestProcessor.__init__cCs
|jdS)zp
        Return config file value multiplied by 1000000,
        as we receive duration in microseconds
        i@B)Zrequests_duration)r%r'r'r(�configured_duration8sz$RequestProcessor.configured_duration)�reportc
CsJy|j�|�Wn4tk
rD}z|j�dt|��Wdd}~XYnXdS)z(
        Call Statistics Sender
        zStatisticsSender failed: %sN)r"�sendrr�error�str)r%r*�er'r'r(�
send_stats@szRequestProcessor.send_statsN)�returncCs*t|jdd�}|��|j�d|j�dS)zA
        Start dumper|DecisionMaker thread in background
        T)�target�daemonz[%s] Routine startedN)r�background_routine�startrr�name)r%�tr'r'r(r$Isz)RequestProcessor.start_background_routinecCs�x�t��}|jdkr�|j�dt�j|�|��|jdkr�|j�dt�j|�|�	|j
�|j�dt�j|�|�	|j�}|��|�	|j
|�tdd�|�d�q|�|j�qWdS)	z�
        Dumps collected stats to file once an hour.
        Runs DecisionMaker once a day
        Cleanup storage after DecisionMaker run
        rz'[%s] Routine thread launching dump (%s)z-[%s] Routine thread launching AutoTracer (%s)z0[%s] Routine thread launching DecisionMaker (%s)T)r�<N)r�now�minuterrrr5�dump_collected_stats_to_file�hour�
_safe_execr#r!rr/r�
_simple_sleep�_sleep_till_next_hour)r%Ztickr*r'r'r(r3Qs(


z#RequestProcessor.background_routine)�actionr0cGsRy||�Stk
rLt��\}}}|jjdt|�||dt��id�YnXdS)zACall requested Callable with given args and capture any exceptionz%s failed with exception %s, %sZorig_traceback)�extraN)�	Exception�sys�exc_infor�criticalr-�	traceback�
format_exc)r%r?�args�etZev�_r'r'r(r<os
zRequestProcessor._safe_exec�)�to_sleepcCs"|j�dt�j|�t�|�dS)zP
        Log and sleep given number of seconds or 15 minutes by default
        z%[%s] Routine thread sleeping for (%s)N)rrrr5�time�sleep)r%rKr'r'r(r=yszRequestProcessor._simple_sleepcCs*tdd�t|d���}|�t|��dS)zF
        Sleep the number of minutes remaining till next hour
        r
)�hours)�minutesN)r�
total_secondsr=�int)r%Zstart_minuteZ	sleep_forr'r'r(r>�s
z&RequestProcessor._sleep_till_next_hourcCs�|j��|j�dt�j�|��yLt�t��8}x|j	�
�D]\}}|||<q<Wdd�|�
�D�}WdQRXWn@tk
r�}z"|jjddt
|�id�t�}Wdd}~XYnXWdQRX|j�dt�j�|S)	z.
        Dump collected stats to file
        z [%s] Acquires lock to dump statscSsi|]\}}||�qSr'r')�.0�k�vr'r'r(�
<dictcomp>�szARequestProcessor.dump_collected_stats_to_file.<locals>.<dictcomp>NzFailed to dump data�err)r@z [%s] Released lock to dump stats)r r�debugrr5�add_current_date�shelve�openrr�items�OSErrorr,r-�dict)r%�db�item�valueZdump_resultr.r'r'r(r:�s"
 "
z-RequestProcessor.dump_collected_stats_to_file)�	timestampr0cCst�|�jS)zN
        Takes an hour of a day, to which the given timestamp belongs
        )r�
fromtimestampr;)rar'r'r(�get_interval_for�sz!RequestProcessor.get_interval_forcCst��|jd<dS)zW
        Adds current date to the 'total struct' dict before dumping to a file
        Zcurrent_dateN)r�todayr)r%r'r'r(rX�sz!RequestProcessor.add_current_date)r5�is_wpr0cCs>||jkr:|j�dt�j|�ttdgd�|d�|j|<dS)zH
        Add new domain sub-struct if it is not already present
        z[%s] New domain received: %sr�)�domain_total_reqsZis_a_wordpress_domainN)rrrWrr5r]�list)r%r5rer'r'r(�
add_domain�s
zRequestProcessor.add_domain)�domain�urlr0cCs`||j|kr\|j�dt�j|�ttdgd�tdgd�tdgd�t�d�|j||<dS)zE
        Add new URL sub-struct if it is not already present
        z[%s] New URL received: %srrf)�url_total_reqs�
url_slow_reqs�url_throttled_reqs�	durationsN)rrrWrr5r]rh)r%rjrkr'r'r(�add_url�szRequestProcessor.add_url)rjrkra�duration�	throttledr0cCs|�|�}|j|d|d7<|j||d|d7<||jkrj|j||d|d7<|r�d|j||kr�tdgd�|j||d<|j||d|d7<|j||d�|�|j�d	t�j|�|j�	d
t�j|j�dS)za
        Update request counters for given domain and url,
        save request duration
        rgr
rlrmrnrrfroz[%s] Request to %s processedz[%s] %sN)
rcrr)rh�appendrrrr5rW)r%rjrkrarqrr�intervalr'r'r(�update_data�s

zRequestProcessor.update_data)�datar0c
Cs�|�d�}|�|�r&|j�d|�dSt|�\}}|j�n|j�dt�j�|j|t	|�d��d�|�
||�|j||t|�d��t
|�d��t	|�d	��d
�WdQRX|j�dt�j�dS)z,
        Process given request data
        rkz
%s ignoredNz-[%s] Acquires lock to handle request countersZ	wordpress)rerarqZhitting_limits)rjrkrarqrrz-[%s] Released lock to handle request counters)�getZ
is_ignoredrrWrr rr5ri�boolrprurQ�float)r%rvrkrj�urir'r'r(�handle�s$


zRequestProcessor.handlecCsJ|j�d�yt�t�Wn tk
r:|j�d�YnX|j��dS)z2
        Cleanup storage and total_struct
        zRequestProcessor cleanup...zAlready no storage fileN)rr�os�unlinkrr\r�clear)r%r'r'r(r�szRequestProcessor.cleanup)rJ)�__name__�
__module__�__qualname__�__doc__r�propertyr)r]r/r$r3rr	r<rQr=r>r:�staticmethodrcrXr-rxrirpryrur{r�
__classcell__r'r')r&r(r"s&	
r)"r�rr|rYrBrLrErrr�	threadingrrr�typingrr	Z
autotracerr�commonrr!r
Zstat_senderr�internal.constantsr�internal.exceptionsr�internal.utilsrrrrr'r'r'r(�<module>
s$

Zerion Mini Shell 1.0