<s> import logging <EOL> import Queue <EOL> import time <EOL> from tornado . ioloop import PeriodicCallback <EOL> from tornado . httpclient import AsyncHTTPClient , HTTPRequest <EOL> MAX_TRIES = <NUM_LIT:10> <EOL> class ProjectsUpdateTask ( object ) : <EOL> def __init__ ( self , application , main_loop ) : <EOL> self . application = application <EOL> self . main_loop = main_loop <EOL> def start ( self ) : <EOL> periodic_task = PeriodicCallback ( <EOL> self . update , <EOL> self . application . config . UPDATE_PERIOD * <NUM_LIT:1000> , <EOL> io_loop = self . main_loop <EOL> ) <EOL> periodic_task . start ( ) <EOL> def update ( self ) : <EOL> if self . application . load_project_keys ( ) is None : <EOL> logging . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class SendToSentryTask ( object ) : <EOL> def __init__ ( self , application , main_loop ) : <EOL> self . application = application <EOL> self . main_loop = main_loop <EOL> self . start_time = None <EOL> self . last_sent = None <EOL> def start ( self ) : <EOL> periodic_task = PeriodicCallback ( <EOL> self . update , <EOL> <NUM_LIT:20> , <EOL> io_loop = self . main_loop <EOL> ) <EOL> periodic_task . start ( ) <EOL> def get_handle_request ( self , project_id ) : <EOL> def handle_request ( response ) : <EOL> response_time = response . request_time <EOL> logging . debug ( "<STR_LIT>" , response_time ) <EOL> self . application . last_requests . append ( response_time ) <EOL> self . application . last_requests = self . application . last_requests [ <EOL> max ( <NUM_LIT:0> , len ( self . application . last_requests ) - self . application . config . MAX_REQUESTS_TO_AVERAGE ) : ] <EOL> self . application . average_request_time = self . mean ( self . application . last_requests ) * <NUM_LIT:1000> <EOL> self . application . percentile_request_time = self . calculate_percentile ( ) * <NUM_LIT:1000> <EOL> self . application . storage . mark_as_done ( project_id ) <EOL> if response . error : <EOL> logging . error ( "<STR_LIT>" , response . error ) <EOL> else : <EOL> logging . debug ( "<STR_LIT:OK>" ) <EOL> return handle_request <EOL> def update ( self ) : <EOL> try : <EOL> app_request_time = self . application . percentile_request_time is None and self . application . config . MAX_DUMP_INTERVAL or min ( self . application . percentile_request_time , self . application . config . MAX_DUMP_INTERVAL ) <EOL> logging . debug ( "<STR_LIT>" , app_request_time ) <EOL> if self . application . percentile_request_time : <EOL> logging . debug ( "<STR_LIT>" , self . application . percentile_request_time ) <EOL> if self . last_sent : <EOL> logging . debug ( "<STR_LIT>" , self . last_sent ) <EOL> logging . debug ( "<STR_LIT>" , ( time . time ( ) - self . last_sent ) ) <EOL> if self . application . percentile_request_time is not None and self . last_sent is not None and time . time ( ) - self . last_sent < ( self . application . percentile_request_time / <NUM_LIT:1000> ) : <EOL> return <EOL> if not self . application . storage . total_size : <EOL> return <EOL> logging . debug ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:U+002CU+0020>" . join ( self . application . storage . available_queues ) <EOL> ) <EOL> msg = self . application . storage . get_next_message ( ) <EOL> if not msg : <EOL> return <EOL> project_id , method , headers , url , body = msg <EOL> request = HTTPRequest ( url = url , headers = headers , method = method , body = body ) <EOL> logging . debug ( "<STR_LIT>" , url ) <EOL> self . start_time = time . time ( ) <EOL> self . last_sent = time . time ( ) <EOL> http_client = AsyncHTTPClient ( io_loop = self . main_loop ) <EOL> http_client . fetch ( request , self . get_handle_request ( project_id ) ) <EOL> except Queue . Empty : <EOL> pass <EOL> def mean ( self , items ) : <EOL> return float ( sum ( items ) ) / len ( items ) if len ( items ) > <NUM_LIT:0> else float ( <NUM_LIT:0> ) <EOL> def calculate_percentile ( self ) : <EOL> logging . debug ( "<STR_LIT>" , len ( self . application . last_requests ) ) <EOL> sorted_times = list ( reversed ( sorted ( self . application . last_requests ) ) ) <EOL> last_message = int ( round ( len ( self . application . last_requests ) * <NUM_LIT> ) ) <EOL> return self . mean ( sorted_times [ len ( self . application . last_requests ) - last_message : ] ) </s>
