def _cloneModel(self, command):
        """ Handle the "cloneModel" command

    command: ModelCommand instance for the "cloneModel" command

    retval: ModelCommandResult instance
    """
        try:
            self._checkpointMgr.clone(self._modelID, command.args["modelID"])
        except model_checkpoint_mgr.ModelNotFound as e:
            errorMessage = "%r: Cloning failed - source model not found (%r)" % (
                self,
                e,
            )
            self._logger.error(errorMessage)
            return ModelCommandResult(commandID=command.commandID,
                                      method=command.method,
                                      status=htmengineerrno.ERR_NO_SUCH_MODEL,
                                      errorMessage=errorMessage)
        except model_checkpoint_mgr.ModelAlreadyExists as e:
            # Assuming it's the result of at-least-once delivery semantics
            self._logger.warn(
                "%r: Cloning destination already exists, suppressing (%r)",
                self, e)

        return ModelCommandResult(commandID=command.commandID,
                                  method=command.method,
                                  status=0)
  def testModelCommandResultSerializableStateWithNonZeroStatus(self):
    commandID = 1
    method = "testMethod"
    status = 1
    errorMessage = "something bad happened"
    commandResult = ModelCommandResult(commandID=commandID, method=method,
                                       status=status, errorMessage=errorMessage)
    self.assertEqual(commandResult.commandID, commandID)
    self.assertEqual(commandResult.method, method)
    self.assertEqual(commandResult.status, status)
    self.assertEqual(commandResult.errorMessage, errorMessage)
    self.assertIsNone(commandResult.args)
    self.assertIn("ModelCommandResult<", str(commandResult))
    self.assertIn("ModelCommandResult<", repr(commandResult))

    commandResult2 = _ModelRequestResultBase.__createFromState__(
      commandResult.__getstate__())

    self.assertEqual(commandResult2.commandID, commandID)
    self.assertEqual(commandResult2.method, method)
    self.assertEqual(commandResult2.status, status)
    self.assertEqual(commandResult2.errorMessage, errorMessage)
    self.assertIsNone(commandResult2.args)
    self.assertIn("ModelCommandResult<", str(commandResult2))
    self.assertIn("ModelCommandResult<", repr(commandResult2))
    def _deleteModel(self, command):
        """ Handle the "deleteModel" command

    command: ModelCommand instance for the "deleteModel" command

    retval: ModelCommandResult instance

    raises _ModelRunnerError
    """
        try:
            # Delete the model's checkpoint
            try:
                self._checkpointMgr.remove(modelID=self._modelID)
            except model_checkpoint_mgr.ModelNotFound:
                # Suppress it: could be side-effect of at-least-once delivery
                # guarantee, so treat it as success
                self._logger.warn(
                    "deleteModel: modelID=%s not found (side-effect "
                    "of at-least-once delivery guarantee?); "
                    "suppressing error", self._modelID)
            # Clean up model's resources in ModelSwapperInterface
            self._swapperAPI.cleanUpAfterModelDeletion(self._modelID)
        finally:
            self._archiver = _ModelArchiver(self._modelID)
            self._done = True

        return ModelCommandResult(commandID=command.commandID,
                                  method=command.method,
                                  status=0)
    def _processModelCommand(self, command):
        """
    command: ModelCommand instance

    Returns: a ModelCommandResult instance
    """
        self._logger.info("%r: Processing model command: %r", self, command)
        try:
            if command.method == "defineModel":
                return self._defineModel(command)
            elif command.method == "deleteModel":
                return self._deleteModel(command)
            elif command.method == "cloneModel":
                return self._cloneModel(command)
            else:
                raise _ModelRunnerError(errno=htmengineerrno.ERR_INVALID_ARG,
                                        msg="Unknown command method=%s" %
                                        (command.method, ))

        except (Exception, _ModelRunnerError) as e:  # pylint: disable=W0703
            self._logger.exception("%r: %r failed;", self, command)
            return ModelCommandResult(
                commandID=command.commandID,
                method=command.method,
                status=(e.errno if isinstance(e, _ModelRunnerError) else
                        htmengineerrno.ERR),
                errorMessage="%s - %r failed on modelID=%s: %s. (tb: ...%s)" %
                (datetime.utcnow().isoformat(), command, self._modelID, str(e)
                 or repr(e),
                 traceback.format_exc()[-self._MAX_TRACEBACK_TAIL:]))
  def _implModelCommandResultSerializableStateWithZeroStatus(self, args):
    commandID = 1
    method = "testMethod"
    status = 0
    commandResult = ModelCommandResult(commandID=commandID, method=method,
                                       status=status, args=copy.copy(args))
    self.assertEqual(commandResult.commandID, commandID)
    self.assertEqual(commandResult.method, method)
    self.assertEqual(commandResult.status, status)
    self.assertEqual(commandResult.args, args)
    self.assertIsNone(commandResult.errorMessage)

    commandResult2 = _ModelRequestResultBase.__createFromState__(
      commandResult.__getstate__())

    self.assertEqual(commandResult2.commandID, commandID)
    self.assertEqual(commandResult2.method, method)
    self.assertEqual(commandResult2.status, status)
    self.assertEqual(commandResult2.args, args)
    self.assertIsNone(commandResult2.errorMessage)
    self.assertIn("ModelCommandResult<", str(commandResult2))
    self.assertIn("ModelCommandResult<", repr(commandResult2))
    def _defineModel(self, command):
        """ Handle the "defineModel" command

    command: ModelCommand instance for the "defineModel" command

    retval: ModelCommandResult instance

    raises _ModelRunnerError
    """
        newModelDefinition = dict(
            modelParams=dict(modelConfig=command.args["modelConfig"],
                             inferenceArgs=command.args["inferenceArgs"]),
            inputSchema=command.args["inputRecordSchema"])

        try:
            self._checkpointMgr.define(modelID=self._modelID,
                                       definition=newModelDefinition)
            assert self._model is None
        except model_checkpoint_mgr.ModelAlreadyExists:
            # Check if the existing model has the same definition
            existingModelDefinition = self._checkpointMgr.loadModelDefinition(
                self._modelID)

            if newModelDefinition != existingModelDefinition:
                self._logger.error(
                    "defineModel: modelID=%s already exists with different "
                    "creation meta data; existing=%r; new=%r", self._modelID,
                    existingModelDefinition, newModelDefinition)

                raise _ModelRunnerError(
                    errno=htmengineerrno.ERR_MODEL_ALREADY_EXISTS,
                    msg="defineModel: modelID=%s already exists with different "
                    "creation meta data" % (self._modelID, ))
            else:
                # newModelDefinition is the same as existingModelDefinition; might be
                # side-effect of at-least-once delivery guarantee of the reliable data
                # path, so treat it as success
                self._logger.warn(
                    "defineModel: modelID=%s already exists with same creation meta "
                    "data (side-effect of at-least-once delivery guarantee?)",
                    self._modelID)

        return ModelCommandResult(commandID=command.commandID,
                                  method=command.method,
                                  status=0)