<s> """<STR_LIT>""" <EOL> from helper import config as helper_config <EOL> from tornado import httpserver <EOL> from tornado import ioloop <EOL> import logging <EOL> import multiprocessing <EOL> import signal <EOL> import socket <EOL> import ssl <EOL> from tornado import version as tornado_version <EOL> from tinman import application <EOL> from tinman import config <EOL> from tinman import exceptions <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> class Process ( multiprocessing . Process ) : <EOL> """<STR_LIT>""" <EOL> CERT_REQUIREMENTS = { config . NONE : ssl . CERT_NONE , <EOL> config . OPTIONAL : ssl . CERT_OPTIONAL , <EOL> config . REQUIRED : ssl . CERT_REQUIRED } <EOL> def __init__ ( self , group = None , target = None , name = None , args = ( ) , kwargs = { } ) : <EOL> """<STR_LIT>""" <EOL> super ( Process , self ) . __init__ ( group , target , name , args , kwargs ) <EOL> self . namespace = kwargs [ '<STR_LIT>' ] <EOL> self . port = kwargs [ '<STR_LIT:port>' ] <EOL> self . app = None <EOL> self . http_server = None <EOL> self . request_counters = dict ( ) <EOL> self . logging_config = None <EOL> if self . newrelic_ini_path : <EOL> self . setup_newrelic ( ) <EOL> def create_application ( self ) : <EOL> """<STR_LIT>""" <EOL> return application . Application ( self . settings , <EOL> self . namespace . routes , <EOL> self . port ) <EOL> def create_http_server ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . start_http_server ( self . port , self . http_config ) <EOL> @ property <EOL> def http_config ( self ) : <EOL> """<STR_LIT>""" <EOL> return { config . NO_KEEP_ALIVE : <EOL> self . namespace . server . get ( config . NO_KEEP_ALIVE , False ) , <EOL> config . SSL_OPTIONS : self . ssl_options , <EOL> config . XHEADERS : self . namespace . server . get ( config . XHEADERS , <EOL> False ) } <EOL> def on_sigabrt ( self , signal_unused , frame_unused ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . info ( '<STR_LIT>' ) <EOL> self . http_server . stop ( ) <EOL> self . ioloop . stop ( ) <EOL> def on_sighup ( self , signal_unused , frame_unused ) : <EOL> """<STR_LIT>""" <EOL> for setting in self . http_config : <EOL> if getattr ( self . http_server , setting ) != self . http_config [ setting ] : <EOL> LOGGER . debug ( '<STR_LIT>' , setting ) <EOL> setattr ( self . http_server , setting , self . http_config [ setting ] ) <EOL> for setting in self . settings : <EOL> if self . app . settings [ setting ] != self . settings [ setting ] : <EOL> LOGGER . debug ( '<STR_LIT>' , setting ) <EOL> self . app . settings [ setting ] = self . settings [ setting ] <EOL> self . app . handlers = [ ] <EOL> self . app . named_handlers = { } <EOL> routes = self . namespace . config . get ( config . ROUTES ) <EOL> self . app . add_handlers ( "<STR_LIT>" , self . app . prepare_routes ( routes ) ) <EOL> LOGGER . info ( '<STR_LIT>' ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . debug ( '<STR_LIT>' ) <EOL> self . logging_config = self . setup_logging ( ) <EOL> self . setup_signal_handlers ( ) <EOL> try : <EOL> self . app = self . create_application ( ) <EOL> except exceptions . NoRoutesException : <EOL> return <EOL> self . http_server = self . create_http_server ( ) <EOL> self . ioloop = ioloop . IOLoop . instance ( ) <EOL> try : <EOL> self . ioloop . start ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> @ property <EOL> def settings ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( self . namespace . config ) <EOL> def setup_logging ( self ) : <EOL> return helper_config . LoggingConfig ( self . namespace . logging ) <EOL> @ property <EOL> def newrelic_ini_path ( self ) : <EOL> return self . namespace . config . get ( config . NEWRELIC ) <EOL> def setup_newrelic ( self ) : <EOL> """<STR_LIT>""" <EOL> import newrelic . agent <EOL> newrelic . agent . initialize ( self . newrelic_ini_path ) <EOL> def setup_signal_handlers ( self ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . debug ( '<STR_LIT>' ) <EOL> signal . signal ( signal . SIGABRT , self . on_sigabrt ) <EOL> @ property <EOL> def ssl_options ( self ) : <EOL> """<STR_LIT>""" <EOL> opts = self . namespace . server . get ( config . SSL_OPTIONS ) or dict ( ) <EOL> if config . CERT_REQS in opts : <EOL> opts [ config . CERT_REQS ] = self . CERT_REQUIREMENTS [ opts [ config . CERT_REQS ] ] <EOL> return opts or None <EOL> def start_http_server ( self , port , args ) : <EOL> """<STR_LIT>""" <EOL> LOGGER . info ( "<STR_LIT>" , <EOL> tornado_version , port , args ) <EOL> http_server = httpserver . HTTPServer ( self . app , ** args ) <EOL> http_server . bind ( port , family = socket . AF_INET ) <EOL> http_server . start ( <NUM_LIT:1> ) <EOL> return http_server </s>
