def OnInit(self,app,settings): self.app=app self.settings=settings self.server=RPCServer() if settings.has_key("Rabbitmq"): self.server.NewRemoteRPCServer(settings["Rabbitmq"]) err = app.RegisterLocalClient(settings["Id"], self.server.get_local_server()) if err != None : log.warning("RegisterLocalClient: id(%s) error(%s)", settings["Id"], err) log.info("RPCServer init success id(%s)", settings["Id"])
def OnDestroy(self): for id, session in self.serverList.items(): err = session.Rpc.shutdown() if err != None: log.warning("RPCClient close fail type(%s) id(%s)", session.Stype, id) else: log.info("RPCClient close success type(%s) id(%s)", session.Stype, id) return None
def OnInit(self, settings): self.settings = settings modules = settings["Module"] for Type, ModuleInfos in modules.items(): for moduel in ModuleInfos: if self.serverList.has_key(moduel["Id"]): #如果Id已经存在,说明有两个相同Id的模块,这种情况不能被允许,这里就直接抛异常 强制崩溃以免以后调试找不到问题 m = self.serverList[moduel["Id"]] raise Exception( "ServerId (%s) Type (%s) of the modules already exist Can not be reused ServerId (%s) Type (%s)" % (m.Id, m.Stype, moduel["Id"], Type)) client = RPCClient() if moduel.has_key("Rabbitmq"): #如果远程的rpc存在则创建一个对应的客户端 client.NewRemoteClient(moduel["Rabbitmq"]) session = ServerSession(moduel["Id"], Type, client) self.serverList[moduel["Id"]] = session log.info("RPCClient create success type(%s) id(%s)" % (Type, moduel["Id"]))
def Init(self,app,ProcessID): self.app=app self.ProcessID=ProcessID log.info("This service ProcessID is [%s]", ProcessID) self.CheckModuleSettings() modules=app.GetSettings()["Module"] for mod in self.mods : for Type, modSettings in modules.items(): if mod.GetType() == Type : #匹配 for setting in modSettings: #这里可能有BUG 公网IP和局域网IP处理方式可能不一样,先不管 if ProcessID == setting["ProcessID"] : self.runMods.append(mod) #这里加入能够运行的组件 mod.settings=setting break #跳出内部循环 for module in self.runMods: module.OnInit(app,module.settings) module.start()
def Run(self, debug, *modules): usage = "usage: %prog [options] arg" parser = OptionParser(usage) parser.add_option("--conf", default=os.getcwd() + os.sep + "conf/server.conf", help="Server configuration file path", dest="conf") parser.add_option("--pid", default="development", help="Server ProcessID?", dest="pid") parser.add_option("--log", default=os.getcwd() + os.sep + "logs", help="Log file directory?", dest="log") (options, args) = parser.parse_args() # if len(args) != 1: # parser.error("incorrect number of arguments") ProcessID = options.pid init_log_config(debug, ProcessID, options.log) log.info("Server configuration file path [%s]" % options.conf) log.info("pymqant %s starting up", pymqant.version) f = open(options.conf, "r") strings = [] list_of_all_the_lines = f.readlines() for line in list_of_all_the_lines: line = line.strip() if line.startswith("//") == False: strings.append(line) f.close() settings = json.loads("".join(strings)) manager = ModuleManager() for mod in modules: manager.Register(mod) self.OnInit(settings) manager.Init(self, ProcessID) stop = Event() def signal_handler(signum, frame): exit() log.error("Forced exit process!") def handler(signum, frame): self.sig = signum stop.set() signal.signal(signal.SIGALRM, signal_handler) # 14 signal.alarm(5) #5秒后强制退出 signal.signal(signal.SIGINT, handler) signal.signal(signal.SIGTERM, handler) stop.wait() manager.Destroy() self.OnDestroy() log.info("pymqant closing down (signal: %d)", self.sig)
def OnDestroy(self): self.server.shutdown() log.info("RPCServer close success id(%s)", self.settings["Id"])