<s> from datetime import datetime <EOL> import os <EOL> from random import randint <EOL> import signal <EOL> from ssl import SSLError <EOL> import sys <EOL> import time <EOL> import traceback <EOL> from gunicorn import util <EOL> from gunicorn . workers . workertmp import WorkerTmp <EOL> from gunicorn . reloader import Reloader <EOL> from gunicorn . http . errors import ( <EOL> InvalidHeader , InvalidHeaderName , InvalidRequestLine , InvalidRequestMethod , <EOL> InvalidHTTPVersion , LimitRequestLine , LimitRequestHeaders , <EOL> ) <EOL> from gunicorn . http . errors import InvalidProxyLine , ForbiddenProxyRequest <EOL> from gunicorn . http . wsgi import default_environ , Response <EOL> from gunicorn . six import MAXSIZE <EOL> class Worker ( object ) : <EOL> SIGNALS = [ getattr ( signal , "<STR_LIT>" % x ) <EOL> for x in "<STR_LIT>" . split ( ) ] <EOL> PIPE = [ ] <EOL> def __init__ ( self , age , ppid , sockets , app , timeout , cfg , log ) : <EOL> """<STR_LIT>""" <EOL> self . age = age <EOL> self . ppid = ppid <EOL> self . sockets = sockets <EOL> self . app = app <EOL> self . timeout = timeout <EOL> self . cfg = cfg <EOL> self . booted = False <EOL> self . aborted = False <EOL> self . reloader = None <EOL> self . nr = <NUM_LIT:0> <EOL> jitter = randint ( <NUM_LIT:0> , cfg . max_requests_jitter ) <EOL> self . max_requests = cfg . max_requests + jitter or MAXSIZE <EOL> self . alive = True <EOL> self . log = log <EOL> self . tmp = WorkerTmp ( cfg ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . pid <EOL> @ property <EOL> def pid ( self ) : <EOL> return os . getpid ( ) <EOL> def notify ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tmp . notify ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def init_process ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . cfg . reload : <EOL> def changed ( fname ) : <EOL> self . log . info ( "<STR_LIT>" , fname ) <EOL> os . kill ( self . pid , signal . SIGQUIT ) <EOL> self . reloader = Reloader ( callback = changed ) <EOL> self . reloader . start ( ) <EOL> if self . cfg . env : <EOL> for k , v in self . cfg . env . items ( ) : <EOL> os . environ [ k ] = v <EOL> util . set_owner_process ( self . cfg . uid , self . cfg . gid ) <EOL> util . seed ( ) <EOL> self . PIPE = os . pipe ( ) <EOL> for p in self . PIPE : <EOL> util . set_non_blocking ( p ) <EOL> util . close_on_exec ( p ) <EOL> [ util . close_on_exec ( s ) for s in self . sockets ] <EOL> util . close_on_exec ( self . tmp . fileno ( ) ) <EOL> self . log . close_on_exec ( ) <EOL> self . init_signals ( ) <EOL> self . cfg . post_worker_init ( self ) <EOL> self . load_wsgi ( ) <EOL> self . booted = True <EOL> self . run ( ) <EOL> def load_wsgi ( self ) : <EOL> try : <EOL> self . wsgi = self . app . wsgi ( ) <EOL> except SyntaxError as e : <EOL> if not self . cfg . reload : <EOL> raise <EOL> self . log . exception ( e ) <EOL> try : <EOL> exc_type , exc_val , exc_tb = sys . exc_info ( ) <EOL> self . reloader . add_extra_file ( exc_val . filename ) <EOL> tb_string = traceback . format_tb ( exc_tb ) <EOL> self . wsgi = util . make_fail_app ( tb_string ) <EOL> finally : <EOL> del exc_tb <EOL> def init_signals ( self ) : <EOL> [ signal . signal ( s , signal . SIG_DFL ) for s in self . SIGNALS ] <EOL> signal . signal ( signal . SIGQUIT , self . handle_quit ) <EOL> signal . signal ( signal . SIGTERM , self . handle_exit ) <EOL> signal . signal ( signal . SIGINT , self . handle_quit ) <EOL> signal . signal ( signal . SIGWINCH , self . handle_winch ) <EOL> signal . signal ( signal . SIGUSR1 , self . handle_usr1 ) <EOL> signal . signal ( signal . SIGABRT , self . handle_abort ) <EOL> if hasattr ( signal , '<STR_LIT>' ) : <EOL> signal . siginterrupt ( signal . SIGTERM , False ) <EOL> signal . siginterrupt ( signal . SIGUSR1 , False ) <EOL> def handle_usr1 ( self , sig , frame ) : <EOL> self . log . reopen_files ( ) <EOL> def handle_exit ( self , sig , frame ) : <EOL> self . alive = False <EOL> def handle_quit ( self , sig , frame ) : <EOL> self . alive = False <EOL> self . cfg . worker_int ( self ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def handle_abort ( self , sig , frame ) : <EOL> self . alive = False <EOL> self . cfg . worker_abort ( self ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def handle_error ( self , req , client , addr , exc ) : <EOL> request_start = datetime . now ( ) <EOL> addr = addr or ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> if isinstance ( exc , ( InvalidRequestLine , InvalidRequestMethod , <EOL> InvalidHTTPVersion , InvalidHeader , InvalidHeaderName , <EOL> LimitRequestLine , LimitRequestHeaders , <EOL> InvalidProxyLine , ForbiddenProxyRequest , <EOL> SSLError ) ) : <EOL> status_int = <NUM_LIT> <EOL> reason = "<STR_LIT>" <EOL> if isinstance ( exc , InvalidRequestLine ) : <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> elif isinstance ( exc , InvalidRequestMethod ) : <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> elif isinstance ( exc , InvalidHTTPVersion ) : <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> elif isinstance ( exc , ( InvalidHeaderName , InvalidHeader , ) ) : <EOL> mesg = "<STR_LIT:%s>" % str ( exc ) <EOL> if not req and hasattr ( exc , "<STR_LIT>" ) : <EOL> req = exc . req <EOL> elif isinstance ( exc , LimitRequestLine ) : <EOL> mesg = "<STR_LIT:%s>" % str ( exc ) <EOL> elif isinstance ( exc , LimitRequestHeaders ) : <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> elif isinstance ( exc , InvalidProxyLine ) : <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> elif isinstance ( exc , ForbiddenProxyRequest ) : <EOL> reason = "<STR_LIT>" <EOL> mesg = "<STR_LIT>" <EOL> status_int = <NUM_LIT> <EOL> elif isinstance ( exc , SSLError ) : <EOL> reason = "<STR_LIT>" <EOL> mesg = "<STR_LIT>" % str ( exc ) <EOL> status_int = <NUM_LIT> <EOL> msg = "<STR_LIT>" <EOL> self . log . debug ( msg . format ( ip = addr [ <NUM_LIT:0> ] , error = str ( exc ) ) ) <EOL> else : <EOL> if hasattr ( req , "<STR_LIT>" ) : <EOL> self . log . exception ( "<STR_LIT>" , req . uri ) <EOL> status_int = <NUM_LIT> <EOL> reason = "<STR_LIT>" <EOL> mesg = "<STR_LIT>" <EOL> if req is not None : <EOL> request_time = datetime . now ( ) - request_start <EOL> environ = default_environ ( req , client , self . cfg ) <EOL> environ [ '<STR_LIT>' ] = addr [ <NUM_LIT:0> ] <EOL> environ [ '<STR_LIT>' ] = str ( addr [ <NUM_LIT:1> ] ) <EOL> resp = Response ( req , client , self . cfg ) <EOL> resp . status = "<STR_LIT>" % ( status_int , reason ) <EOL> resp . response_length = len ( mesg ) <EOL> self . log . access ( resp , req , environ , request_time ) <EOL> try : <EOL> util . write_error ( client , status_int , reason , mesg ) <EOL> except : <EOL> self . log . debug ( "<STR_LIT>" ) <EOL> def handle_winch ( self , sig , fname ) : <EOL> return </s>
