def get_context(cls): """ 获取context .. Note:: 此方法为从本地内存中读取context, 不涉及外部调用,应在context加载后使用 :return: GuardianContext对象 :rtype: GuardianContext :raises EInvalidOperation: 非法操作 """ if not cls._context: raise exception.EInvalidOperation( "guardian context is not init yet") return cls._context
def save_operation(self, operation): """ 持久化状态机信息 :param operation: :return: """ if not self.lock: log.e("current guardian instance no privilege to save operation") raise exception.EInvalidOperation( "current guardian instance no privilege to save operation") operation_path = config.GuardianConfig.get_persistent_path("operations") + "/" + operation.operation_id if not persistence.PersistenceDriver().exists(operation_path): persistence.PersistenceDriver().create_node(path=operation_path) persistence.PersistenceDriver().save_data(operation_path, pickle.dumps(operation)) log.d("save operation_id:{} success".format(operation.operation_id))
def add_node(self, node): """ 增加节点 :param Node node: 节点 :return: None :raises ETypeMismatch: 节点类型错误 :raises EInvalidOperation: 操作不合法 """ if not isinstance(node, Node): raise exception.ETypeMismatch("param node type must be Node") elif node in self._nodes: raise exception.EInvalidOperation( "node {} already added".format(node.name)) else: self._nodes.append(node) self._nodes_process[node.name] = False
def save_context(self): """ 运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败 :return: 无返回 :rtype: None :raises EInvalidOperation: 非法操作 """ if not self.lock: log.e("current guardian instance no privilege to save context") raise exception.EInvalidOperation( "current guardian instance no privilege to save context") context_path = config.GuardianConfig.get_persistent_path("context") context_to_persist = copy.deepcopy(self) # 考虑反序列化时可能异常,不使用del删除属性operations context_to_persist.operations = {} persistence.PersistenceDriver().save_data( context_path, pickle.dumps(context_to_persist)) log.i("save context success")
def save_context(self): """ 运行数据持久化,当当前Guardian为主(lock属性为True)时,可持久化数据,否则失败 :return: 无返回 :rtype: None :raises EInvalidOperation: 非法操作 """ if not self.lock: log.e("current guardian instance no privilege to save context") raise exception.EInvalidOperation( "current guardian instance no privilege to save context") context_path = config.GuardianConfig.get_persistent_path("context") context_to_persist = self operations_tmp = self.operations context_to_persist.operations = {} try: persistence.PersistenceDriver().save_data(context_path, pickle.dumps(context_to_persist)) except Exception as e: self.operations = operations_tmp log.r(e, "save context fail") self.operations = operations_tmp log.d("save context success")