B ��edy�@s�dZddlZddlZddlZddlZddlmZmZddlmZm Z m Z ddl m Z m Z ddlmZddlmZdd lmZdd lmZd d lmZmZmZmZd d lmZd dlmZmZm Z eGdd�de��Z!dS)z- This module contains RequestProcessor class �N)�datetime� timedelta)�Thread�RLock�current_thread)�Callable�Any�)� AutoTracer)�Common)� DecisionMaker)�StatisticsSender�)� session_scope�setup_database� RequestResult�cleanup_old_data)�SSAError)� singleton� url_split�switch_schedstatscs�eZdZdZdZd#�fdd� Zedd��Zed�d d �Z dd �d d �Z dd �dd�Z e e d�dd�Zd$ed�dd�Zdd�Zeeed�dd��Zdd�Zd%dd�Zedd �d!d"�Z�ZS)&�RequestProcessorz^ SSA Request processor implementation. Only one instance is allowed to be created �dNcsxt���t�d�|_|j�dt�tdd�|r6|nt�|_ t �|_ t |d�|_ t�|_t|d�|_|��g|_dS)NZ req_processorzProcessor enabled: %sT)�enabled)�engine)�super�__init__�logging� getLogger�logger�info� __package__rrrr�_lockr �decision_makerr �senderr � auto_tracer�start_background_routine�_buffer)�selfr)� __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_duration=sz$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_statsEszRequestProcessor.send_stats)�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&Nsz)RequestProcessor.start_background_routinecCs�x�t��}|jdkr�|jdkr�|j�dt�j|�|�|j �|j�dt�j|�|�|j �|j�dt�j|�|�|j �}|j�dt�j|�t |j �|�|j|�tdd�|�d�q|�|j�qWd S) z� Dumps collected stats to file once an hour. Runs DecisionMaker once a day Cleanup storage after DecisionMaker run rz2[%s] Routine thread launching buffer flushing (%s)z-[%s] Routine thread launching AutoTracer (%s)z0[%s] Routine thread launching DecisionMaker (%s)z*[%s] Routine thread launching cleanup (%s)T)r�<N)r�now�minute�hourrr rr8� _safe_exec�flush_with_lockr%r#rrr2r� _simple_sleep�_sleep_till_next_hour)r(Ztickr-r*r*r+r6Vs0            z#RequestProcessor.background_routine)�actionr3c GsRy||�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� exceptionr0� traceback� format_exc)r(rB�args�et�ev�_r*r*r+r>ys 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)rr rr8�time�sleep)r(rOr*r*r+r@�s 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+rA�s z&RequestProcessor._sleep_till_next_hour)� timestampr3cCs t�|�jS)zN Takes an hour of a day, to which the given timestamp belongs )r� fromtimestampr=)rVr*r*r+�get_interval_for�sz!RequestProcessor.get_interval_forc Cs4|j�|jdd�}g|_WdQRX|�|�dS)N)r"r'� flush_buffer)r(�objectsr*r*r+r?�sz RequestProcessor.flush_with_lockc Cs:|dkr|j}|sdSt|j��}|�|�WdQRXdS)z6 Save in-memory buffer into database. N)r'rrZbulk_save_objects)r(rZ�dbr*r*r+rY�s  zRequestProcessor.flush_buffer)�datar3cCs�|�d�}|�|�r&|j�d|�dSt|�\}}|j�dt�j�g}|j�n|j� t |||d|d|d|j k|d|d|d |d d � �t |j�|j kr�|jdd�}g|_WdQRX|�|�|j�d t�j�dS) z, Process given request data �urlz %s ignoredNz-[%s] Acquires lock to handle request countersrV�duration�hitting_limits�throttled_time�io_throttled_time� wordpress) �domain�pathrVr^Zis_slow_requestr_r`rarbz-[%s] Released lock to handle request counters)�getZ is_ignoredr�debugrrr8r"r'�appendrr,�len� BUFFER_SIZErY)r(r\r]rc�uriZobjects_per_threadr*r*r+�handle�s4      zRequestProcessor.handle)N)rN)N)�__name__� __module__� __qualname__�__doc__rir�propertyr,�dictr2r&r6rrr>rUr@rA� staticmethodrXr?rYrk� __classcell__r*r*)r)r+r s  #  r)"rorrErPrHrr� threadingrrr�typingrrZ autotracerr �commonr r#r Z stat_senderr r[rrrr�internal.exceptionsr�internal.utilsrrrrr*r*r*r+�<module> s