Skip to content

Commit

Permalink
auto advance will try to advance state machine once per specified per…
Browse files Browse the repository at this point in the history
…iod, instead of waiting for the period between attempts
  • Loading branch information
pvva committed Jan 25, 2019
1 parent c6aa523 commit 13bdec5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func (sm *StateMachine) EmergencySwitch(stateId string, triggerEvents ...bool) (

func (sm *StateMachine) AutoAdvance(tryPeriod time.Duration, terminalStates []string) interface{} {
for {
ct := time.Now()
result, err := sm.Advance()
if err != nil {
// stop state machine
Expand All @@ -253,7 +254,10 @@ func (sm *StateMachine) AutoAdvance(tryPeriod time.Duration, terminalStates []st
}
} else {
// cannot advance yet, wait
time.Sleep(tryPeriod)
passed := time.Now().Sub(ct)
if passed.Nanoseconds() < tryPeriod.Nanoseconds() {
time.Sleep(time.Duration(tryPeriod.Nanoseconds() - passed.Nanoseconds()))
}
}
}
}

0 comments on commit 13bdec5

Please sign in to comment.