<s> """<STR_LIT>""" <EOL> import logging <EOL> import pprint <EOL> import SocketServer <EOL> import threading <EOL> from . import __version__ <EOL> from aggregator import DefaultAggregator <EOL> from aliveness import AlivenessHandler <EOL> from collector import UDPCollector <EOL> from metrics_store import GraphiteStore <EOL> from util import deep_merge , resolve_class_string <EOL> BANNER = """<STR_LIT>""" <EOL> class Statsite ( object ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_SETTINGS = { <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:class>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT:host>" : "<STR_LIT>" , <EOL> "<STR_LIT:port>" : <NUM_LIT> <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:class>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT:store>" : { <EOL> "<STR_LIT:class>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { } <EOL> } <EOL> def __init__ ( self , settings = { } ) : <EOL> """<STR_LIT>""" <EOL> super ( Statsite , self ) . __init__ ( ) <EOL> self . settings = deep_merge ( self . DEFAULT_SETTINGS , settings ) <EOL> for component in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:store>" ] : <EOL> key = "<STR_LIT>" % component <EOL> value = resolve_class_string ( self . settings [ component ] [ "<STR_LIT:class>" ] ) <EOL> del self . settings [ component ] [ "<STR_LIT:class>" ] <EOL> setattr ( self , key , value ) <EOL> self . logger = logging . getLogger ( "<STR_LIT>" ) <EOL> self . logger . info ( BANNER % { <EOL> "<STR_LIT:version>" : __version__ , <EOL> "<STR_LIT>" : self . _collector_cls , <EOL> "<STR_LIT>" : self . _aggregator_cls , <EOL> "<STR_LIT>" : self . _store_cls , <EOL> "<STR_LIT>" : pprint . pformat ( self . settings , width = <NUM_LIT> , indent = <NUM_LIT:2> ) <EOL> } ) <EOL> self . logger . debug ( "<STR_LIT>" % self . _store_cls ) <EOL> self . store = self . _store_cls ( ** self . settings [ "<STR_LIT:store>" ] ) <EOL> self . settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . store <EOL> self . logger . debug ( "<STR_LIT>" % self . _aggregator_cls ) <EOL> self . aggregator = self . _create_aggregator ( ) <EOL> self . settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . aggregator <EOL> self . logger . debug ( "<STR_LIT>" % self . _collector_cls ) <EOL> self . collector = self . _collector_cls ( ** self . settings [ "<STR_LIT>" ] ) <EOL> self . aliveness_check = None <EOL> self . timer = None <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> self . _reset_timer ( ) <EOL> if self . settings [ "<STR_LIT>" ] [ "<STR_LIT>" ] : <EOL> self . _enable_aliveness_check ( ) <EOL> self . collector . start ( ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> if self . timer : <EOL> self . timer . cancel ( ) <EOL> self . _disable_aliveness_check ( ) <EOL> self . collector . shutdown ( ) <EOL> def _enable_aliveness_check ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . aliveness_check : <EOL> self . aliveness_check . shutdown ( ) <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> host = self . settings [ "<STR_LIT>" ] [ "<STR_LIT:host>" ] <EOL> port = int ( self . settings [ "<STR_LIT>" ] [ "<STR_LIT:port>" ] ) <EOL> self . aliveness_check = SocketServer . TCPServer ( ( host , port ) , AlivenessHandler ) <EOL> thread = threading . Thread ( target = self . aliveness_check . serve_forever ) <EOL> thread . daemon = True <EOL> thread . start ( ) <EOL> def _disable_aliveness_check ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> if self . aliveness_check : <EOL> self . aliveness_check . shutdown ( ) <EOL> self . aliveness_check = None <EOL> def _on_timer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _reset_timer ( ) <EOL> self . _flush_and_switch_aggregator ( ) <EOL> def _flush_and_switch_aggregator ( self ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> old_aggregator = self . aggregator <EOL> self . aggregator = self . _create_aggregator ( ) <EOL> self . collector . set_aggregator ( self . aggregator ) <EOL> thread = threading . Thread ( target = old_aggregator . flush ) <EOL> thread . daemon = True <EOL> thread . start ( ) <EOL> def _create_aggregator ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _aggregator_cls ( metrics_settings = self . settings [ "<STR_LIT>" ] , ** self . settings [ "<STR_LIT>" ] ) <EOL> def _reset_timer ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . timer : <EOL> self . timer . cancel ( ) <EOL> self . timer = threading . Timer ( int ( self . settings [ "<STR_LIT>" ] ) , self . _on_timer ) <EOL> self . timer . start ( ) </s>
