/// <summary> /// Marks a command to be processed. /// </summary> /// <param name="sessionGroupId">The GUID that's used as the unique Id of the subject session group.</param> /// <param name="command">The command to be marked as processed.</param> public void MarkCommandProcessed(Guid sessionGroupId, PipelineSyncCommand command) { using (RuntimeEntityModel context = RuntimeEntityModel.CreateInstance()) { int commandValue = (int)command; int processingStateValue = (int)PipelineSyncCommandState.Processing; var cmdQuery = from c in context.RTOrchestrationCommandSet where c.Command == commandValue && c.Status == processingStateValue && c.SessionGroup.GroupUniqueId.Equals(sessionGroupId) select c; if (cmdQuery.Count() == 0) { return; } foreach (var cmd in cmdQuery) { cmd.Status = (int)PipelineSyncCommandState.Processed; } context.TrySaveChanges(); } }
/// <summary> /// Try transitting from source State to destination State with the given Command /// </summary> /// <remarks>Must call CommandTransitFinsihed when this call returns TRUE and any transition specific actions have been taken.</remarks> /// <param name="command"></param> /// <returns></returns> public bool TryTransit(PipelineSyncCommand command) { lock (m_lock) { PipelineState newState = PipelineState.Default; bool retVal = false; switch (m_currState) { case PipelineState.Paused: retVal = TransitionAlgorithm.TransitAtPaused(command, out newState); break; case PipelineState.Pausing: retVal = TransitionAlgorithm.TransitAtPausing(command, out newState); break; case PipelineState.Running: retVal = TransitionAlgorithm.TransitAtStarted(command, out newState); break; case PipelineState.Starting: retVal = TransitionAlgorithm.TransitAtStarting(command, out newState); break; case PipelineState.StoppingSingleTrip: retVal = TransitionAlgorithm.TransitAtStoppingSingleTrip(command, out newState); break; case PipelineState.Stopped: retVal = TransitionAlgorithm.TransitAtStopped(command, out newState); break; case PipelineState.StoppedSingleTrip: retVal = TransitionAlgorithm.TransitAtStoppedSingleTrip(command, out newState); break; case PipelineState.Stopping: retVal = TransitionAlgorithm.TransitAtStopping(command, out newState); break; case PipelineState.Default: retVal = TransitionAlgorithm.TransitAtDefault(command, out newState); break; default: retVal = false; break; } if (retVal) { m_currState = newState; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } return(retVal); } }
/// <summary> /// Transit from an intermittent state, e.g. Stopping, to a stable state, e.g. Stopped /// </summary> /// <param name="command"></param> public void CommandTransitFinished(PipelineSyncCommand command) { lock (m_lock) { switch (command) { case PipelineSyncCommand.FINISH: case PipelineSyncCommand.STOP: if (m_currState == PipelineState.Stopping) { m_currState = PipelineState.Stopped; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } break; case PipelineSyncCommand.PAUSE: if (m_currState == PipelineState.Pausing) { m_currState = PipelineState.Paused; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } break; case PipelineSyncCommand.PAUSE_FOR_CONFLICT: if (m_currState == PipelineState.PausingForConflict) { m_currState = PipelineState.PausedByConflict; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } break; case PipelineSyncCommand.RESUME: case PipelineSyncCommand.START: case PipelineSyncCommand.START_NEW_TRIP: if (m_currState == PipelineState.Starting) { m_currState = PipelineState.Running; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } break; case PipelineSyncCommand.STOP_CURRENT_TRIP: if (m_currState == PipelineState.StoppingSingleTrip) { m_currState = PipelineState.StoppedSingleTrip; m_syncStateManager.SaveCurrentState(m_ownerType, m_ownerUniqueId, m_currState); } break; default: return; } } }
public virtual bool TransitAtStarted(PipelineSyncCommand command, out PipelineState currentState) { bool transitSucceeded = false; currentState = PipelineState.Default; switch (command) { case PipelineSyncCommand.FINISH: currentState = PipelineState.Stopping; transitSucceeded = true; break; case PipelineSyncCommand.PAUSE: currentState = PipelineState.Pausing; transitSucceeded = true; break; case PipelineSyncCommand.PAUSE_FOR_CONFLICT: currentState = PipelineState.PausingForConflict; transitSucceeded = true; break; case PipelineSyncCommand.RESUME: currentState = PipelineState.Running; transitSucceeded = true; break; case PipelineSyncCommand.START: currentState = PipelineState.Running; transitSucceeded = true; break; case PipelineSyncCommand.START_NEW_TRIP: currentState = PipelineState.Running; transitSucceeded = true; break; case PipelineSyncCommand.STOP: currentState = PipelineState.Stopping; transitSucceeded = true; break; case PipelineSyncCommand.STOP_CURRENT_TRIP: currentState = PipelineState.StoppingSingleTrip; transitSucceeded = true; break; case PipelineSyncCommand.DEFAULT: default: break; } return(transitSucceeded); }
public virtual bool TransitAtStopped(PipelineSyncCommand command, out PipelineState currentState) { bool transitSucceeded = false; currentState = PipelineState.Default; switch (command) { case PipelineSyncCommand.FINISH: transitSucceeded = true; currentState = PipelineState.Stopped; break; case PipelineSyncCommand.PAUSE: break; case PipelineSyncCommand.PAUSE_FOR_CONFLICT: break; case PipelineSyncCommand.RESUME: break; case PipelineSyncCommand.START: break; case PipelineSyncCommand.START_NEW_TRIP: break; case PipelineSyncCommand.STOP: transitSucceeded = true; currentState = PipelineState.Stopped; break; case PipelineSyncCommand.STOP_CURRENT_TRIP: break; case PipelineSyncCommand.DEFAULT: default: break; } return(transitSucceeded); }
/// <summary> /// Add a new sync command to the queue for processing. /// </summary> /// <param name="sessionGroupId">The GUID that's used as the unique Id of the subject session group.</param> /// <param name="newCmd">The new command to be appended to the queue.</param> public void AddCommand(Guid sessionGroupId, PipelineSyncCommand newCmd) { using (RuntimeEntityModel context = RuntimeEntityModel.CreateInstance()) { var sessionGroupQuery = from sg in context.RTSessionGroupSet where sg.GroupUniqueId.Equals(sessionGroupId) select sg; Debug.Assert(sessionGroupQuery.Count() == 1, "sessionGroupQuery.Count() != 1"); if (sessionGroupQuery.Count() != 1) { // [teyang] TODO add exception desc throw new InvalidOperationException(); } var rtSessionGroup = sessionGroupQuery.First(); RTOrchestrationCommand command = RTOrchestrationCommand.CreateRTOrchestrationCommand( 0, // identity column id is auto-generated (int)newCmd, (int)PipelineSyncCommandState.New); command.SessionGroup = rtSessionGroup; context.AddToRTOrchestrationCommandSet(command); context.TrySaveChanges(); } }