class Scheduler(object): handlers = {} def __init__(self): base_dispatcher_config_path = os.path.split( os.path.realpath(__file__))[0] + "/recever.conf" self.config = Conf([base_dispatcher_config_path]) self.__rabbitmq = RabbitMq(self.config) def load(self, project_name): if project_name is None: return None # 1加载配置文件 project_config_path = os.path.split( os.path.realpath(__file__) )[0] + '/handler/' + project_name + '/' + project_name + '.conf' self.config.do([project_config_path]) # 2动态加载处理类 handler = importlib.import_module('recever.handler.' + project_name + '.' + project_name + '_handler') project_handler = handler.Handler(self.config) # 3handlers添加处理实例 self.handlers[str(project_name)] = project_handler # 4返回处理实例 return project_handler def handler(self, ch, method, properties, body): result = json.loads(body) project_name = result.get("project_name") t_hander = self.handlers.get(project_name) if t_hander is None: t_hander = self.load(project_name) if t_hander is not None: t_hander.handle(result) # 调用rabbitmq的回调函数方法启动接收信息 def start(self): self.__rabbitmq.get(self.handler)
class Scheduler(object): distributes = {} def __init__(self,project_name): base_dispatcher_config_path = os.path.split(os.path.realpath(__file__))[0] + "/dispatcher.conf" self.config = Conf([base_dispatcher_config_path]) self.__distribute = self.load(project_name) def load(self, project_name): if project_name is None: return None # 1加载配置文件 project_config_path = os.path.split(os.path.realpath(__file__))[0] + '/distribute/' + project_name + '/' + project_name + '.conf' self.config.do([project_config_path]) # 2动态加载处理类 distribute = importlib.import_module('dispatcher.distribute.' + project_name + '.' + project_name + '_distribute') project_distribute = distribute.Distribute(self.config) # 4返回处理实例 return project_distribute # 调用rabbitmq的回调函数方法启动接收信息 def start(self): self.__distribute.dispatch()