def __init__(self, path_list=[]): ############################## # TODO: Add error handling??? ############################## self._service_ld = ServiceLoader(path_list) self._service_ld.load_services() # Build a dictionary of services, # where key is a service class name (string) and value is service object self._services = {service.__class__.__name__: service for service in self._service_ld} map(logging.info, self._service_ld) logging.info('Total loaded services: {0}'.format(len(self._service_ld))) self._autostart_services()
class ServiceController(object): """This class is responsible for loading/starting/stoping services""" def __init__(self, path_list=[]): ############################## # TODO: Add error handling??? ############################## self._service_ld = ServiceLoader(path_list) self._service_ld.load_services() # Build a dictionary of services, # where key is a service class name (string) and value is service object self._services = {service.__class__.__name__: service for service in self._service_ld} map(logging.info, self._service_ld) logging.info('Total loaded services: {0}'.format(len(self._service_ld))) self._autostart_services() def _autostart_services(self): """Some tasks inside services might be autostartable""" try: for svc_name, _ in self._services.iteritems(): self._services[svc_name].autostart() except Exception as ex: logging.exception('Exception during service [{0}] autostart: {1}'.format(svc_name, ex)) def start_service(self, service_name): try: self._services[service_name].start() except KeyError: logging.error('Unknown service [{0}]'.format(service_name)) def stop_service(self, service_name): try: self._services[service_name].stop() except KeyError: logging.error('Unknown service [{0}]'.format(service_name)) def restart_service(self, service_name): pass def stop_all(self): ############################################## # TODO: Redo this method, it's ugly ############################################## for name, svc in self._services.iteritems(): try: svc.stop() except Exception: pass def assert_service_exists(func): def _assert_service_exists(self, *args, **kwargs): service_name = args[0] if service_name not in self._services: msg = 'Unknown service `{0}`'.format(service_name) logging.error(msg) raise Exception(msg) return func(self, *args, **kwargs) return _assert_service_exists @assert_service_exists def start_task(self, service_name, task_name): return self._services[service_name].start_task(task_name) @assert_service_exists def stop_task(self, service_name, task_name): return self._services[service_name].stop_task(task_name) def get_services(self): return [self._services[svc].json_friendly() for svc in self._services] @assert_service_exists def get_tasks(self, service_name): return {task[0]: task[1] for task in self._services[service_name].get_tasks()} @assert_service_exists def get_properties(self, service_name, task): return self._services[service_name].get_properties(task) @assert_service_exists def set_property(self, service_name, task, prop, value): return self._services[service_name].set_property(task, prop, value) @assert_service_exists def get_commands(self, service_name, task): return self._services[service_name].get_commands(task) @assert_service_exists def call_command(self, service_name, task, cmd, args=None): return self._services[service_name].call_command(task, cmd, args) @assert_service_exists def get_state(self, service_name, task): return self._services[service_name].get_state(task)