all repos — erl @ b63d8d33dc0b786fdc9937770645d4c6e914a9dc

Execute Reload Loop

move timeout handling to state machine

Alan Pearce
commit

b63d8d33dc0b786fdc9937770645d4c6e914a9dc

parent

a18da5045554871e0aeb6fdd65eb62eb5e51d0df

1 file changed, 25 insertions(+), 4 deletions(-)

changed files
M state/state.gostate/state.go
@@ -1,7 +1,9 @@
package state import ( + "context" "log" + "time" "alin.ovh/erl/command" )
@@ -57,15 +59,31 @@
type Action func() type StateMachine struct { - log *log.Logger + log *log.Logger + timeout time.Duration + currentState State transitions map[State]map[Event]State actions map[State]map[Event]Action } -func New(cmd command.Command, log *log.Logger) *StateMachine { +const DefaultTimeout = 1 * time.Second + +type Options struct { + Timeout time.Duration + Logger *log.Logger +} + +func New(cmd command.Command, opts Options) *StateMachine { + if opts.Timeout == 0 { + opts.Timeout = DefaultTimeout + } + log := opts.Logger + sm := &StateMachine{ - log: log, + log: opts.Logger, + timeout: opts.Timeout, + currentState: NotStarted, transitions: make(map[State]map[Event]State), actions: make(map[State]map[Event]Action),
@@ -108,7 +126,10 @@
go wait(cmd) } stop := func() { - err := cmd.Stop() + ctx, cancel := context.WithTimeout(context.Background(), sm.timeout) + defer cancel() + + err := cmd.Stop(ctx) if err != nil { log.Printf("Error stopping command: %v\n", err) }