-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
346 lines (301 loc) · 7.95 KB
/
registry.go
File metadata and controls
346 lines (301 loc) · 7.95 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package process
import (
"os"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
"dappco.re/go/core"
coreio "dappco.re/go/core/io"
coreerr "dappco.re/go/core/log"
)
// DaemonEntry records a running daemon in the registry.
//
// Example:
//
// entry := process.DaemonEntry{Code: "app", Daemon: "serve", PID: os.Getpid()}
type DaemonEntry struct {
Code string `json:"code"`
Daemon string `json:"daemon"`
PID int `json:"pid"`
Health string `json:"health,omitempty"`
Project string `json:"project,omitempty"`
Binary string `json:"binary,omitempty"`
Started time.Time `json:"started,omitempty"`
StartedAt time.Time `json:"startedAt,omitempty"`
Config map[string]string `json:"config,omitempty"`
}
// Registry tracks running daemons via JSON files in a directory.
type Registry struct {
dir string
}
// NewRegistry creates a registry backed by the given directory.
//
// Example:
//
// reg := process.NewRegistry("/tmp/daemons")
func NewRegistry(dir string) *Registry {
return &Registry{dir: dir}
}
// DefaultRegistry returns a registry using ~/.core/daemons/.
//
// Example:
//
// reg := process.DefaultRegistry()
func DefaultRegistry() *Registry {
home, err := os.UserHomeDir()
if err != nil {
home = os.TempDir()
}
return NewRegistry(core.Path(home, ".core", "daemons"))
}
// Register writes a daemon entry to the registry directory.
// If Started and StartedAt are zero, they are set to the current time.
// The directory is created if it does not exist.
//
// Example:
//
// _ = reg.Register(entry)
func (r *Registry) Register(entry DaemonEntry) error {
now := time.Now()
if entry.StartedAt.IsZero() {
entry.StartedAt = now
}
if entry.Started.IsZero() {
entry.Started = entry.StartedAt
}
if entry.Config != nil {
entry.Config = cloneDaemonConfig(entry.Config)
}
entryPath := r.entryPath(entry.Code, entry.Daemon)
if err := coreio.Local.EnsureDir(filepath.Dir(entryPath)); err != nil {
return coreerr.E("Registry.Register", "failed to create registry directory", err)
}
r1 := core.JSONMarshal(entry)
if !r1.OK {
return coreerr.E("Registry.Register", "failed to marshal entry", r1.Value.(error))
}
payload := string(r1.Value.([]byte))
if err := coreio.Local.Write(entryPath, payload); err != nil {
return coreerr.E("Registry.Register", "failed to write entry file", err)
}
legacyPath := r.legacyEntryPath(entry.Code, entry.Daemon)
if legacyPath != entryPath {
_ = coreio.Local.Write(legacyPath, payload)
}
return nil
}
// Unregister removes a daemon entry from the registry.
//
// Example:
//
// _ = reg.Unregister("app", "serve")
func (r *Registry) Unregister(code, daemon string) error {
paths := r.lookupPaths(code, daemon)
for _, path := range paths {
if err := coreio.Local.Delete(path); err != nil {
if core.Is(err, os.ErrNotExist) {
continue
}
return coreerr.E("Registry.Unregister", "failed to delete entry file", err)
}
}
return nil
}
// Get reads a single daemon entry and checks whether its process is alive.
// If the process is dead, stale files are removed and (nil, false) is returned.
//
// Example:
//
// entry, ok := reg.Get("app", "serve")
func (r *Registry) Get(code, daemon string) (*DaemonEntry, bool) {
var candidate DaemonEntry
candidateSet := false
var candidatePath string
var stalePaths []string
for _, path := range r.lookupPaths(code, daemon) {
data, err := coreio.Local.Read(path)
if err != nil {
continue
}
var entry DaemonEntry
if r1 := core.JSONUnmarshalString(data, &entry); !r1.OK {
stalePaths = append(stalePaths, path)
continue
}
normalizeDaemonEntry(&entry)
if !isAlive(entry.PID) {
stalePaths = append(stalePaths, path)
continue
}
if !candidateSet || entryStartedAt(entry).After(entryStartedAt(candidate)) {
if candidatePath != "" {
stalePaths = append(stalePaths, candidatePath)
}
candidate = entry
candidateSet = true
candidatePath = path
}
}
for _, path := range stalePaths {
_ = coreio.Local.Delete(path)
}
if !candidateSet {
return nil, false
}
return &candidate, true
}
// List returns all alive daemon entries, pruning any with dead PIDs.
//
// Example:
//
// entries, err := reg.List()
func (r *Registry) List() ([]DaemonEntry, error) {
matches, err := r.findEntryPaths()
if err != nil {
return nil, coreerr.E("Registry.List", "failed to scan registry files", err)
}
byKey := make(map[string]DaemonEntry)
for _, path := range matches {
data, err := coreio.Local.Read(path)
if err != nil {
continue
}
var entry DaemonEntry
if r1 := core.JSONUnmarshalString(data, &entry); !r1.OK {
_ = coreio.Local.Delete(path)
continue
}
normalizeDaemonEntry(&entry)
if !isAlive(entry.PID) {
_ = coreio.Local.Delete(path)
continue
}
key := entry.Code + "\x00" + entry.Daemon
existing, ok := byKey[key]
if !ok || entryStartedAt(entry).After(entryStartedAt(existing)) {
byKey[key] = entry
}
}
alive := make([]DaemonEntry, 0, len(byKey))
for _, entry := range byKey {
alive = append(alive, entry)
}
slices.SortFunc(alive, func(a, b DaemonEntry) int {
ta := entryStartedAt(a)
tb := entryStartedAt(b)
if ta.Equal(tb) {
if a.Code == b.Code {
if a.Daemon == b.Daemon {
return 0
}
if a.Daemon < b.Daemon {
return -1
}
return 1
}
if a.Code < b.Code {
return -1
}
return 1
}
if ta.Before(tb) {
return -1
}
return 1
})
return alive, nil
}
// findEntryPaths returns all JSON files below the registry directory recursively.
func (r *Registry) findEntryPaths() ([]string, error) {
entries := make([]string, 0)
_, err := os.Stat(r.dir)
if os.IsNotExist(err) {
return entries, nil
}
if err != nil {
return nil, err
}
if err := filepath.WalkDir(r.dir, func(path string, dirEntry os.DirEntry, err error) error {
if err != nil {
return nil
}
if dirEntry.IsDir() {
return nil
}
if strings.EqualFold(filepath.Ext(path), ".json") {
entries = append(entries, path)
}
return nil
}); err != nil {
return nil, err
}
return entries, nil
}
// lookupPaths returns both modern and legacy layout paths for the daemon key.
func (r *Registry) lookupPaths(code, daemon string) []string {
paths := []string{r.entryPath(code, daemon)}
legacy := r.legacyEntryPath(code, daemon)
if legacy != paths[0] {
paths = append(paths, legacy)
}
return paths
}
// entryPath returns the filesystem path for a daemon entry.
// RFC layout: {dir}/{code}/{daemon}.json.
func (r *Registry) entryPath(code, daemon string) string {
return core.Path(r.dir, sanitizeRegistrySegment(code), core.Concat(sanitizeRegistrySegment(daemon), ".json"))
}
// legacyEntryPath returns the old flat layout for compatibility.
func (r *Registry) legacyEntryPath(code, daemon string) string {
name := core.Concat(core.Replace(code, "/", "-"), "-", core.Replace(daemon, "/", "-"), ".json")
name = strings.ReplaceAll(name, `\`, "-")
return core.Path(r.dir, name)
}
func entryStartedAt(entry DaemonEntry) time.Time {
if !entry.StartedAt.IsZero() {
return entry.StartedAt
}
return entry.Started
}
func normalizeDaemonEntry(entry *DaemonEntry) {
if entry == nil {
return
}
if entry.StartedAt.IsZero() {
entry.StartedAt = entry.Started
}
if entry.Started.IsZero() {
entry.Started = entry.StartedAt
}
entry.Config = cloneDaemonConfig(entry.Config)
}
func cloneDaemonConfig(cfg map[string]string) map[string]string {
if len(cfg) == 0 {
return nil
}
out := make(map[string]string, len(cfg))
for key, value := range cfg {
out[key] = value
}
return out
}
func sanitizeRegistrySegment(value string) string {
sanitized := core.Trim(value)
if sanitized == "" {
sanitized = "default"
}
return strings.ReplaceAll(sanitized, "..", "-")
}
// isAlive checks whether a process with the given PID is running.
func isAlive(pid int) bool {
if pid <= 0 {
return false
}
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
return proc.Signal(syscall.Signal(0)) == nil
}