dimanche 19 juillet 2020

executing preconditions at any stage in workflow

My workflow is defined by these states:

enum State {
    case Idle 
    case ...
    case CreatingSession
    case RefreshingToken
    case ...
    case ...
    case Downloading
    case ...
    case Done
}

Precondition: there must be an active session and valid token (which needs to be refreshed when a new session is created), before following states can execute related task.

Let's say the current state is downloading and suddenly the active session is lost. The workflow must roll back the state to perform createSession and refreshToken before returning to downloading.

How can I design a non-linear state machine, or implement state pattern, that is capable of performing such precondition, while maintaining last state (downloading in my example) to resume to, after preconditions are met?

Possible solutions:

  • store currentState and previousState. if workflow rolls back state to execute preconditions, include logic that checks if previousState is greater than currentState (refreshToken) state, if so, that indicates and references the state to return to.

  • Separate states for 2 state machines. First state machine will include precondition states. If preconditions need to be executed, first state machine will rollback from Done, while second state machine with the other half of states maintains its current state.

These are very poor solutions, that dont work for reasons I could list out. Any pointers or code example of how to implement state machine to account for such pre-validation/preconditions.

Thanks

Aucun commentaire:

Enregistrer un commentaire