<s> import os <EOL> import sys <EOL> import signal <EOL> import eventlet <EOL> from st2common import log as logging <EOL> from st2reactor . container . process_container import ProcessSensorContainer <EOL> from st2common . services . sensor_watcher import SensorWatcher <EOL> from st2common . models . system . common import ResourceReference <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class SensorContainerManager ( object ) : <EOL> def __init__ ( self , sensors_partitioner ) : <EOL> self . _sensor_container = None <EOL> self . _sensors_watcher = SensorWatcher ( create_handler = self . _handle_create_sensor , <EOL> update_handler = self . _handle_update_sensor , <EOL> delete_handler = self . _handle_delete_sensor , <EOL> queue_suffix = '<STR_LIT>' ) <EOL> self . _container_thread = None <EOL> if not sensors_partitioner : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _sensors_partitioner = sensors_partitioner <EOL> def run_sensors ( self ) : <EOL> """<STR_LIT>""" <EOL> sensors = self . _sensors_partitioner . get_sensors ( ) <EOL> if sensors : <EOL> LOG . info ( '<STR_LIT>' , len ( sensors ) ) <EOL> LOG . info ( '<STR_LIT>' , [ self . _get_sensor_ref ( sensor ) for sensor in sensors ] ) <EOL> sensors_to_run = [ ] <EOL> for sensor in sensors : <EOL> sensors_to_run . append ( self . _to_sensor_object ( sensor ) ) <EOL> LOG . info ( '<STR_LIT>' , os . getpid ( ) ) <EOL> self . _setup_sigterm_handler ( ) <EOL> self . _spin_container_and_wait ( sensors_to_run ) <EOL> def _spin_container_and_wait ( self , sensors ) : <EOL> try : <EOL> self . _sensor_container = ProcessSensorContainer ( sensors = sensors ) <EOL> self . _container_thread = eventlet . spawn ( self . _sensor_container . run ) <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> self . _sensors_watcher . start ( ) <EOL> exit_code = self . _container_thread . wait ( ) <EOL> LOG . error ( '<STR_LIT>' , exit_code ) <EOL> LOG . error ( '<STR_LIT>' , os . getpid ( ) ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> self . _sensor_container . shutdown ( ) <EOL> self . _sensors_watcher . stop ( ) <EOL> LOG . info ( '<STR_LIT>' , os . getpid ( ) , <EOL> sys . exc_info ( ) [ <NUM_LIT:0> ] . __name__ ) <EOL> eventlet . kill ( self . _container_thread ) <EOL> self . _container_thread = None <EOL> return <NUM_LIT:0> <EOL> def _setup_sigterm_handler ( self ) : <EOL> def sigterm_handler ( signum = None , frame = None ) : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> signal . signal ( signal . SIGTERM , sigterm_handler ) <EOL> def _to_sensor_object ( self , sensor_db ) : <EOL> file_path = sensor_db . artifact_uri . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class_name = sensor_db . entry_point . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> sensor_obj = { <EOL> '<STR_LIT>' : sensor_db . pack , <EOL> '<STR_LIT>' : file_path , <EOL> '<STR_LIT>' : class_name , <EOL> '<STR_LIT>' : sensor_db . trigger_types , <EOL> '<STR_LIT>' : sensor_db . poll_interval , <EOL> '<STR_LIT>' : self . _get_sensor_ref ( sensor_db ) <EOL> } <EOL> return sensor_obj <EOL> def _handle_create_sensor ( self , sensor ) : <EOL> if not self . _sensors_partitioner . is_sensor_owner ( sensor ) : <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> return <EOL> if not sensor . enabled : <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> return <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> self . _sensor_container . add_sensor ( sensor = self . _to_sensor_object ( sensor ) ) <EOL> def _handle_update_sensor ( self , sensor ) : <EOL> if not self . _sensors_partitioner . is_sensor_owner ( sensor ) : <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> return <EOL> sensor_ref = self . _get_sensor_ref ( sensor ) <EOL> sensor_obj = self . _to_sensor_object ( sensor ) <EOL> if not sensor . enabled : <EOL> LOG . info ( '<STR_LIT>' , sensor_ref ) <EOL> self . _sensor_container . remove_sensor ( sensor = sensor_obj ) <EOL> return <EOL> LOG . info ( '<STR_LIT>' , sensor_ref ) <EOL> try : <EOL> self . _sensor_container . remove_sensor ( sensor = sensor_obj ) <EOL> except : <EOL> LOG . exception ( '<STR_LIT>' , sensor_ref ) <EOL> else : <EOL> self . _sensor_container . add_sensor ( sensor = sensor_obj ) <EOL> LOG . info ( '<STR_LIT>' , sensor_ref ) <EOL> def _handle_delete_sensor ( self , sensor ) : <EOL> if not self . _sensors_partitioner . is_sensor_owner ( sensor ) : <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> return <EOL> LOG . info ( '<STR_LIT>' , self . _get_sensor_ref ( sensor ) ) <EOL> self . _sensor_container . remove_sensor ( sensor = self . _to_sensor_object ( sensor ) ) <EOL> def _get_sensor_ref ( self , sensor ) : <EOL> return ResourceReference . to_string_reference ( pack = sensor . pack , name = sensor . name ) </s>
