def __init__(self, zk_server, zk_device, server_name): self.name_service = NameService(zk_server, zk_device) self.server_name = server_name if self.name_service.watch_service(server_name) == False: if __LOG__: printlog("Fatal", 'watch zk %s, %s:%s' % (server_name, zk_server, zk_device), 'error') raise ZkCreateError("watch zk %s , %s:%s,error" % (server_name, zk_server, zk_device)) if self.name_service.start() == False: if __LOG__: printlog("Fatal", 'zk start %s, %s:%s' % (server_name, zk_server, zk_device), 'fail') raise ZkCreateError("start zk %s, %s:%s, error" % (server_name, zk_server, zk_device)) if __LOG__: print 'zk start %s, %s:%s' % (server_name, zk_server, zk_device) printlog("Log", 'zk start %s, %s:%s' % (server_name, zk_server, zk_device), 'OK') time.sleep(2)
class ZkMonitor(object): def __init__(self, zk_server, zk_device, server_name): self.name_service = NameService(zk_server, zk_device) self.server_name = server_name if self.name_service.watch_service(server_name) == False: if __LOG__: printlog("Fatal", 'watch zk %s, %s:%s' % (server_name, zk_server, zk_device), 'error') raise ZkCreateError("watch zk %s , %s:%s,error" % (server_name, zk_server, zk_device)) if self.name_service.start() == False: if __LOG__: printlog("Fatal", 'zk start %s, %s:%s' % (server_name, zk_server, zk_device), 'fail') raise ZkCreateError("start zk %s, %s:%s, error" % (server_name, zk_server, zk_device)) if __LOG__: print 'zk start %s, %s:%s' % (server_name, zk_server, zk_device) printlog("Log", 'zk start %s, %s:%s' % (server_name, zk_server, zk_device), 'OK') time.sleep(2) def Stop(self): self.name_service.stop() def GetServerInfo(self): ret_str, server_host, server_port = self.name_service.get_service_host_and_port( self.server_name, self.name_service.EPOLICY_RANDOM) retry = 0 while ret_str != 'ok' and retry < 5: time.sleep(2) ret_str, server_host, server_port = self.name_service.get_service_host_and_port( self.server_name,self.name_service.EPOLICY_RANDOM) retry += 1 printlog("Error", 'cannot get zk info','retry %d' % retry) return server_host, server_port