-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefs.go
198 lines (165 loc) · 3.59 KB
/
refs.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package packaged
import (
"context"
"errors"
"maps"
"sort"
"sync"
"time"
)
var (
_ Service = &Unimplemented{}
_ Group = &group{}
_ sort.Interface = &entriesSorter{}
)
type (
ServiceType int32
Restart int32
Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
Service interface {
mustEmbedUnimplemented()
Name() string
Type() ServiceType
OnInstall() error
OnStart() error
OnStop() error
}
Group interface {
context.Context
Name() string
Set(key string, value any)
Del(key string)
Get(key string) (any, bool)
GetString(key string) (string, bool)
Values() map[string]any
Entries() []Service
EnvManager
}
Unit struct {
Setup bool
Index, MaxRetry int32
RetryDelay time.Duration
GroupName string
Description string
Entry Service
RestartPolicy Restart
}
Units []*Unit
Context struct {
context.Context
cancelFunc context.CancelCauseFunc
Group
stopOnce sync.Once
}
Unimplemented struct{}
group struct {
context.Context
name string
rw sync.RWMutex
values map[string]any
services []Service
EnvManager
}
entriesSorter struct {
entries Units
desc bool
}
)
var (
ErrQuitUnexpectedly = errors.New("quit unexpectedly")
)
const DefaultGroupName = "__group__"
const (
ServiceTypeIgnore ServiceType = iota
ServiceTypeBlocking
ServiceTypeAsync
)
const (
RestartIgnore Restart = iota
RestartAlways
RestartRetry
)
func (h Unimplemented) mustEmbedUnimplemented() {}
func (h Unimplemented) Name() string { return "Unnamed-Service" }
func (h Unimplemented) Type() ServiceType { return ServiceTypeIgnore }
func (h Unimplemented) OnInstall() error { return nil }
func (h Unimplemented) OnStart() error { return errors.New("unimplemented OnStart") }
func (h Unimplemented) OnStop() error { return nil }
func (n *group) Name() string { return n.name }
func (n *group) setValueWithLock(action func()) {
n.rw.Lock()
defer n.rw.Unlock()
action()
}
func (n *group) Set(key string, value any) {
n.setValueWithLock(func() {
n.values[key] = value
})
}
func (n *group) Del(key string) {
n.setValueWithLock(func() {
delete(n.values, key)
})
}
func (n *group) Get(key string) (any, bool) {
n.rw.RLock()
defer n.rw.RUnlock()
v, ok := n.values[key]
return v, ok
}
func (n *group) GetString(key string) (string, bool) {
return Assert[string](key, n)
}
func (n *group) Values() map[string]any {
n.rw.RLock()
defer n.rw.RUnlock()
return maps.Clone(n.values)
}
func (n *group) Entries() []Service {
return n.services
}
func (c *Context) Stop() {
c.stopOnce.Do(func() {
c.cancelFunc(ErrQuitUnexpectedly)
})
}
func newGroup(ctx context.Context, name string) Group {
var env EnvManager
if name == DefaultGroupName {
env = lookupPrefix("")
} else {
env = lookupPrefix(name)
}
return &group{
Context: ctx,
name: name,
values: make(map[string]any, 16),
services: make([]Service, 0, 8),
EnvManager: env,
}
}
func (s entriesSorter) Len() int { return len(s.entries) }
func (s entriesSorter) Less(i, j int) bool {
return (s.entries[i].Index > s.entries[j].Index) == s.desc
}
func (s entriesSorter) Swap(i, j int) {
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
}
func (u Units) Sort(desc bool) {
fastForward := true
for _, unit := range u {
if unit.Index != 0 && fastForward {
fastForward = false
}
}
if fastForward {
// keep raw order
return
}
sort.Sort(entriesSorter{entries: u, desc: desc})
}