-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice_node.go
More file actions
164 lines (132 loc) · 3.1 KB
/
service_node.go
File metadata and controls
164 lines (132 loc) · 3.1 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
// ==========================
// service_node.go
// ==========================
package main
import (
"context"
"sync"
"time"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type ServiceNode struct {
// Core lifecycle
mu sync.Mutex
ctx context.Context
cancel context.CancelFunc
// App subsystems
stats *StatsManager
config *ServiceConfigStore
// Runtime state
peers map[string]*PeerInfo
services map[string]Service
// P2P
host host.Host
ps *pubsub.PubSub
topic *pubsub.Topic
}
func NewServiceNode() *ServiceNode {
ctx, cancel := context.WithCancel(context.Background())
config := NewServiceConfigStore()
LoadRecommendedServices(config)
sn := &ServiceNode{
ctx: ctx,
cancel: cancel,
config: config,
stats: NewStatsManager(),
peers: make(map[string]*PeerInfo),
services: make(map[string]Service),
}
sn.refreshServices()
return sn
}
func (sn *ServiceNode) SetWailsContext(ctx context.Context) {
sn.ctx = ctx
}
// --------------------------
// Docker
// --------------------------
func (sn *ServiceNode) CheckDockerStatus() {
runtime.EventsEmit(sn.ctx, "docker-status", DockerRunning())
}
// --------------------------
// Services (manual start)
// --------------------------
func (sn *ServiceNode) StartService(id, image, port string) string {
if !DockerRunning() {
return "Docker is not running"
}
exists, running := ContainerState(id)
if exists && running {
return "Service already running"
}
if !DockerImageExists(image) {
_ = PullDockerImage(sn.ctx, image, func(s string) {
runtime.EventsEmit(sn.ctx, "service-log", s)
})
}
if exists && !running {
if err := execDocker("start", id); err != nil {
return err.Error()
}
} else {
if err := RunContainer(id, image, port); err != nil {
return err.Error()
}
}
if err := WaitForRunning(id, 15*time.Second); err != nil {
return err.Error()
}
sn.refreshServices()
sn.BroadcastServices()
return "service started"
}
func (sn *ServiceNode) StopService(id string) string {
StopAndRemove(id)
sn.refreshServices()
sn.BroadcastServices()
return "service stopped"
}
func (sn *ServiceNode) ListServices() []Service {
sn.mu.Lock()
defer sn.mu.Unlock()
out := make([]Service, 0, len(sn.services))
for _, s := range sn.services {
out = append(out, s)
}
return out
}
// --------------------------
// Peers
// --------------------------
func (sn *ServiceNode) GetPeers() []PeerInfo {
sn.mu.Lock()
defer sn.mu.Unlock()
out := make([]PeerInfo, 0, len(sn.peers))
for _, p := range sn.peers {
out = append(out, *p)
}
return out
}
// --------------------------
// Internal
// --------------------------
func (sn *ServiceNode) refreshServices() {
svcs, err := ListRunningServices()
if err != nil {
return
}
stats := sn.stats.Snapshot()
sn.mu.Lock()
defer sn.mu.Unlock()
sn.services = make(map[string]Service)
for _, s := range svcs {
if st, ok := stats[s.ServiceID]; ok {
s.Requests = st.Requests
s.Errors = st.Errors
s.Bandwidth = st.Bandwidth
}
sn.services[s.ServiceID] = s
}
}