code: def update_state(self, identifier, state):
          
          # Get model run to ensure that it exists
          model_run = self.get_object(identifier)
          if model_run is None:
              return None
          # Set timestamp of state change. Raise exception if state change results
          # in invalid life cycle
          timestamp = str(datetime.datetime.utcnow().isoformat())
          if state.is_idle:
              raise ValueError('invalid state change: run cannot become idle')
          elif state.is_running:
              # Current state is required to be IDLE
              if not model_run.state.is_idle:
                  raise ValueError('invalid state change: finished run cannot start again')
              model_run.schedule[RUN_STARTED] = timestamp
          elif state.is_failed:
              # Current state is required to be RUNNING
              if not (model_run.state.is_running or model_run.state.is_idle):
                  raise ValueError('invalid state change: cannot fail finished run')
              model_run.schedule[RUN_FINISHED] = timestamp
          elif state.is_success:
              # Current state is required to be RUNNING
              if not model_run.state.is_running:
                  raise ValueError('invalid state change: cannot finish inactive run')
              model_run.schedule[RUN_FINISHED] = timestamp
          # Update model run state and replace object in database
          model_run.state = state
          model_run.properties[datastore.PROPERTY_STATE] = str(state)
          self.replace_object(model_run)
          # Return modified model run
          return model_run
docs: Update state of identified model run.
  
          Raises exception if state change results in invalid run life cycle.
  
          Parameters
          ----------
          identifier : string
              Unique model run identifier
          state : ModelRunState
              Object representing new run state
  
          Returns
          -------
          ModelRunHandle
              Modified model run handle or None if no run with given identifier
              exists