def delete_node(node_path: str): """ 删除节点 :param node_path: :return: """ try: ZK.delete(node_path, recursive=True) except Exception as e: log.tag_error(ZkControlStatus.Delete, "delete node with error: " + str(e)) raise ActionError(KazooMsg.Delete_Failed)
def handle(func): """ wrap function of zk handle function :return: """ @wraps(func) def wrapper(*args, **kwargs): if not ZK.connected: ZK.start() return func(*args, **kwargs) ZK.stop() return wrapper
def update_node(node_path: str, new_node_data: b""): """ 更新节点 :param node_path: :param new_node_data: :return: """ with ZK.transaction(): try: ZK.set(node_path, new_node_data) except Exception as e: if type(e) is NoNodeError: log.tag_error(ZkControlStatus.Update, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Update, "update node with error: " + str(e)) raise ActionError(KazooMsg.Update_Failed) # 返回更新后的数据 data = get_node(node_path) return data
def create_node(node_path: str, node_data: b"" = b""): """ 创建新节点并set data, 目前只支持json格式的data :param node_path: 节点路径 :param node_data: 节点值 :return: """ with ZK.transaction(): try: ZK.create(node_path, node_data) except Exception as e: if type(e) == NodeExistsError: log.tag_error(ZkControlStatus.Create, "Node exist") raise ActionError(KazooMsg.Node_Exist) else: log.tag_error(ZkControlStatus.Create, "create node with error: " + str(e)) raise ActionError(KazooMsg.Create_Failed) # 返回创建后的数据 data = get_node(node_path) return data
def get_node(node_path: str): """ 获取节点信息 :param node_path: :return: """ try: data, _ = ZK.get(node_path) except Exception as e: if type(e) == NoNodeError: log.tag_error(ZkControlStatus.Get, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Get, "get node with error: " + str(e)) raise ActionError(KazooMsg.Get_Failed) return data
def get_children(node_path: str): """ get a list of child nodes of a path :param node_path: :return: """ try: children_list = ZK.get_children(node_path) except Exception as e: if type(e) is NoNodeError: log.tag_error(ZkControlStatus.Get, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Get, "inquire children list with error: " + str(e)) raise ActionError(KazooMsg.Delete_Failed.Get_Children_Failed) return children_list
def wrapper(*args, **kwargs): if not ZK.connected: ZK.start() return func(*args, **kwargs)