// execute non-terminal states
execute non-terminal states
// execute non - terminal states
func (s *stateMachine) exec(state *State) *Message {
	switch state.Code {
	case Runnable:
		return run(s.h, s.task, s.cmds)
	case Paused:
		return <-s.cmds
	case Sleeping:
		if state.Until == nil {
			metafora.Warnf("task=%q told to sleep without a time. Resuming.", s.task.ID())
			return RunMessage()
		}
		dur := state.Until.Sub(time.Now())
		metafora.Infof("task=%q sleeping for %s", s.task.ID(), dur)
		timer := time.NewTimer(dur)
		select {
		case <-timer.C:
			return RunMessage()
		case msg := <-s.cmds:
			timer.Stop()
			if msg.Code == Checkpoint || msg.Code == Release {
				msg.Until = state.Until
			}
			return msg
		}
	case Fault:
		var msg *Message
		msg, state.Errors = s.errHandler(s.task, state.Errors)
		return msg
	default:
		panic("invalid state: " + state.String())
	}
}
func ( s * stateMachine ) exec ( state * State ) * Message { switch state . Code { case Runnable : return run ( s . h , s . task , s . cmds ) case Paused : return <- s . cmds case Sleeping : if state . Until == nil { metafora . Warnf ( " " , s . task . ID ( ) ) return RunMessage ( ) } dur := state . Until . Sub ( time . Now ( ) ) metafora . Infof ( " " , s . task . ID ( ) , dur ) timer := time . NewTimer ( dur ) select { case <- timer . C : return RunMessage ( ) case msg := <- s . cmds : timer . Stop ( ) if msg . Code == Checkpoint || msg . Code == Release { msg . Until = state . Until } return msg } case Fault : var msg * Message msg , state . Errors = s . errHandler ( s . task , state . Errors ) return msg default : panic ( " " + state . String ( ) ) } }
execute non-terminal states
execute non - terminal states
// Runnable passes control to the stateful handler // Paused until a message arrives // Sleeping until the specified time (or a message) // Checkpoint & Release are special cases that shouldn't affect sleep // time, so maintain it across the state transition // Special case where we potentially trim the current state to keep // errors from growing without bound.