all repos — erl @ main

Execute Reload Loop

state/state.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package state

import (
	"context"
	"log"
	"time"

	"alin.ovh/erl/command"
)

type State int

const (
	NotStarted State = iota
	Running
	Stopping
	Exited
)

func (s State) String() string {
	switch s {
	case NotStarted:
		return "NotStarted"
	case Running:
		return "Running"
	case Stopping:
		return "Stopping"
	case Exited:
		return "Exited"
	default:
		return "Unknown"
	}
}

type Event int

const (
	Start Event = iota
	Shutdown
	Exit
	Restart
)

func (e Event) String() string {
	switch e {
	case Start:
		return "Start"
	case Shutdown:
		return "Shutdown"
	case Exit:
		return "Stopped"
	case Restart:
		return "Restart"
	default:
		return "Unknown"
	}
}

type Action func()

type StateMachine struct {
	log     *log.Logger
	timeout time.Duration

	currentState State
	transitions  map[State]map[Event]State
	actions      map[State]map[Event]Action
}

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:     opts.Logger,
		timeout: opts.Timeout,

		currentState: NotStarted,
		transitions:  make(map[State]map[Event]State),
		actions:      make(map[State]map[Event]Action),
	}

	sm.transitions[NotStarted] = map[Event]State{
		Start: Running,
	}
	sm.transitions[Running] = map[Event]State{
		Exit:     Exited,
		Shutdown: Stopping,
		Restart:  Stopping,
	}
	sm.transitions[Stopping] = map[Event]State{
		Shutdown: Exited,
		Exit:     Exited,
	}
	sm.transitions[Exited] = map[Event]State{
		Start:   Running,
		Restart: Running,
	}

	var waiting chan struct{}
	wait := func(cmd command.Command) {
		err := cmd.Wait()
		if err != nil {
			log.Printf("Error waiting for command: %v\n", err)
		}
		sm.SendEvent(Exit)

		close(waiting)
	}
	start := func() {
		waiting = make(chan struct{}, 1)
		err := cmd.Start()
		if err != nil {
			log.Printf("Failed to start command: %v", err)
		}

		go wait(cmd)
	}
	stop := func() {
		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)
		}
	}
	restart := func() {
		stop()
		<-waiting
		sm.SendEvent(Restart)
	}

	sm.actions[NotStarted] = map[Event]Action{
		Start: start,
	}
	sm.actions[Running] = map[Event]Action{
		Shutdown: stop,
		Restart:  restart,
	}
	sm.actions[Exited] = map[Event]Action{
		Start:   start,
		Restart: start,
	}

	return sm
}

func (sm *StateMachine) SendEvent(ev Event) {
	currentState := sm.currentState
	if nextState, ok := sm.transitions[currentState][ev]; ok {
		sm.currentState = nextState
		if action, ok := sm.actions[currentState][ev]; ok {
			action()
		}
	}
}