<s> import logging <EOL> import uuid <EOL> LIFECYCLE_STATE_STARTING = "<STR_LIT>" <EOL> LIFECYCLE_STATE_CONNECTED = "<STR_LIT>" <EOL> LIFECYCLE_STATE_DISCONNECTED = "<STR_LIT>" <EOL> LIFECYCLE_STATE_SHUTTING_DOWN = "<STR_LIT>" <EOL> LIFECYCLE_STATE_SHUTDOWN = "<STR_LIT>" <EOL> class LifecycleService ( object ) : <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> state = None <EOL> def __init__ ( self , config ) : <EOL> self . _listeners = { } <EOL> for listener in config . lifecycle_listeners : <EOL> self . add_listener ( listener ) <EOL> self . is_live = True <EOL> self . fire_lifecycle_event ( LIFECYCLE_STATE_STARTING ) <EOL> def add_listener ( self , on_lifecycle_change ) : <EOL> id = str ( uuid . uuid4 ( ) ) <EOL> self . _listeners [ id ] = on_lifecycle_change <EOL> return id <EOL> def remove_listener ( self , registration_id ) : <EOL> try : <EOL> self . _listeners . pop ( registration_id ) <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def fire_lifecycle_event ( self , new_state ) : <EOL> if new_state == LIFECYCLE_STATE_SHUTTING_DOWN : <EOL> self . is_live = False <EOL> self . state = new_state <EOL> self . logger . debug ( "<STR_LIT>" , new_state ) <EOL> for listener in self . _listeners . values ( ) : <EOL> try : <EOL> listener ( new_state ) <EOL> except : <EOL> self . logger . exception ( "<STR_LIT>" ) </s>
