def pause(self): """ 暂停状态机 :return: None :raises EStatusMismatch: 状态不匹配 """ if self._status == self.Status.RUNNING: self._status = self.Status.PAUSED else: raise exception.EStatusMismatch( "Only in running status can call this method. " "current status:{}".format(self._status))
def resume(self): """ 恢复状态机 :return: None :raises EStatusMismatch: 状态不匹配 """ if self._status == self.Status.PAUSED: self._status = self.Status.RUNNING else: raise exception.EStatusMismatch( "Only in paused status can call this method." "current status:{}".format(self._status))
def prepare(self): """ 状态机创建之后初次检查, 并设置当前状态节点 :return: None :raises EUnInited: 未初始化 :raises EStatusMismatch: 状态不匹配 """ if self._status != self.Status.CREATED \ and self._status != self.Status.INITED: raise exception.EStatusMismatch( "Only in created or inited status can call " "this method.current status:{}".format(self._status)) if not self._nodes: raise exception.EUnInited("nodes list length is 0") self.status = self.Status.INITED self._current_node = self._nodes[0].name
def start(self): """ 启动状态机 :return: None :raises EStatusMismatch: 状态不匹配 :raises Exception: 通用异常 """ self.prepare() if self._status == self.Status.INITED \ or self._status == self.Status.PAUSED: self._status = self.Status.RUNNING else: raise exception.EStatusMismatch( "Only in inited or paused status can call " "this method.current status:{}".format(self._status)) while self._status == self.Status.RUNNING: try: self.run_next() except Exception as e: self._status = self.Status.FAILED raise e