<s> import os <EOL> import sys <EOL> import eventlet <EOL> from oslo_config import cfg <EOL> from eventlet import wsgi <EOL> from st2common import log as logging <EOL> from st2common . service_setup import setup as common_setup <EOL> from st2common . service_setup import teardown as common_teardown <EOL> from st2common . util . wsgi import shutdown_server_kill_pending_requests <EOL> from st2stream . signal_handlers import register_stream_signal_handlers <EOL> from st2stream . listener import get_listener_if_set <EOL> from st2stream import config <EOL> config . register_opts ( ) <EOL> from st2stream import app <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> eventlet . monkey_patch ( <EOL> os = True , <EOL> select = True , <EOL> socket = True , <EOL> thread = False if '<STR_LIT>' in sys . argv else True , <EOL> time = True ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> WSGI_SERVER_REQUEST_SHUTDOWN_TIME = <NUM_LIT:2> <EOL> def _setup ( ) : <EOL> common_setup ( service = '<STR_LIT>' , config = config , setup_db = True , register_mq_exchanges = True , <EOL> register_signal_handlers = True , register_internal_trigger_types = False , <EOL> run_migrations = False ) <EOL> def _run_server ( ) : <EOL> host = cfg . CONF . stream . host <EOL> port = cfg . CONF . stream . port <EOL> LOG . info ( '<STR_LIT>' , os . getpid ( ) , host , port ) <EOL> max_pool_size = eventlet . wsgi . DEFAULT_MAX_SIMULTANEOUS_REQUESTS <EOL> worker_pool = eventlet . GreenPool ( max_pool_size ) <EOL> sock = eventlet . listen ( ( host , port ) ) <EOL> def queue_shutdown ( signal_number , stack_frame ) : <EOL> eventlet . spawn_n ( shutdown_server_kill_pending_requests , sock = sock , <EOL> worker_pool = worker_pool , wait_time = WSGI_SERVER_REQUEST_SHUTDOWN_TIME ) <EOL> register_stream_signal_handlers ( handler_func = queue_shutdown ) <EOL> wsgi . server ( sock , app . setup_app ( ) , custom_pool = worker_pool ) <EOL> return <NUM_LIT:0> <EOL> def _teardown ( ) : <EOL> common_teardown ( ) <EOL> def main ( ) : <EOL> try : <EOL> _setup ( ) <EOL> return _run_server ( ) <EOL> except SystemExit as exit_code : <EOL> sys . exit ( exit_code ) <EOL> except KeyboardInterrupt : <EOL> listener = get_listener_if_set ( ) <EOL> if listener : <EOL> listener . shutdown ( ) <EOL> except Exception : <EOL> LOG . exception ( '<STR_LIT>' , os . getpid ( ) ) <EOL> return <NUM_LIT:1> <EOL> finally : <EOL> _teardown ( ) </s>
