-
Notifications
You must be signed in to change notification settings - Fork 3
/
rpc.go
233 lines (184 loc) · 5.76 KB
/
rpc.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
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
package service
import (
"fmt"
"sync"
"time"
shared "github.com/roadrunner-server/api/v4/build/common/v1"
serviceV1 "github.com/roadrunner-server/api/v4/build/service/v1"
"go.uber.org/zap"
)
type rpc struct {
mu sync.RWMutex
p *Plugin
}
func (r *rpc) Create(in *serviceV1.Create, out *serviceV1.Response) error {
r.p.logger.Debug("create service", zap.String("name", in.GetName()), zap.Uint64("restart_sec", in.GetRestartSec()), zap.String("command", in.GetCommand()), zap.Int64("process number", in.GetProcessNum()))
if in.GetProcessNum() == 0 {
return fmt.Errorf("the service with %s name should have at least 1 process", in.GetName())
}
r.mu.Lock()
defer r.mu.Unlock()
_, ok := r.p.processes.Load(in.GetName())
if ok {
return fmt.Errorf("the service with %s name already exists", in.GetName())
}
procs := make([]*Process, 0, in.GetProcessNum())
for i := 0; i < int(in.GetProcessNum()); i++ {
// create processor structure, which will process all the services
proc := NewServiceProcess(&Service{
Command: in.GetCommand(),
ProcessNum: int(in.GetProcessNum()),
ExecTimeout: time.Second * time.Duration(in.GetExecTimeout()),
RemainAfterExit: in.GetRemainAfterExit(),
RestartSec: in.GetRestartSec(),
UseServiceName: in.GetServiceNameInLogs(),
TimeoutStopSec: in.GetTimeoutStopSec(),
Env: in.GetEnv(),
}, in.GetName(), r.p.logger)
err := proc.start()
if err != nil {
// if some process from the group failed -> deallocate the whole group
if len(procs) > 0 {
r.p.logger.Warn("stopping already allocated processes")
for i := 0; i < len(procs); i++ {
procs[i].stop()
}
}
return err
}
procs = append(procs, proc)
}
// store all the processes idents
r.p.processes.Store(in.GetName(), procs)
out.Ok = true
return nil
}
func (r *rpc) Terminate(in *serviceV1.Service, out *serviceV1.Response) error {
r.p.logger.Debug("terminate service", zap.String("name", in.GetName()))
r.mu.Lock()
defer r.mu.Unlock()
_, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("the service with %s name doesn't exist", in.GetName())
}
procInterface, ok := r.p.processes.LoadAndDelete(in.GetName())
if !ok {
return fmt.Errorf("no such service: %s", in.GetName())
}
procs := procInterface.([]*Process)
for i := 0; i < len(procs); i++ {
procs[i].stop()
}
out.Ok = true
return nil
}
func (r *rpc) Restart(in *serviceV1.Service, out *serviceV1.Response) error {
r.p.logger.Debug("restart service", zap.String("name", in.GetName()))
r.mu.Lock()
defer r.mu.Unlock()
_, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("the service with %s name doesn't exist", in.GetName())
}
procInterface, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("no such service: %s", in.GetName())
}
procs := procInterface.([]*Process)
newProcs := make([]*Process, len(procs))
for i := 0; i < len(procs); i++ {
procs[i].stop()
service := &Service{}
*service = *(procs[i]).service
newProc := NewServiceProcess(service, in.GetName(), r.p.logger)
err := newProc.start()
if err != nil {
r.p.processes.Delete(in.GetName())
return err
}
newProcs[i] = newProc
r.p.processes.Delete(in.GetName())
}
r.p.processes.Store(in.GetName(), newProcs)
out.Ok = true
return nil
}
// Status returns status for the service
// Deprecated: use Statuses to get correct info
func (r *rpc) Status(in *serviceV1.Service, out *serviceV1.Status) error {
r.p.logger.Debug("service status", zap.String("name", in.GetName()))
r.mu.RLock()
defer r.mu.RUnlock()
_, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("the service with %s name doesn't exist", in.GetName())
}
procInterface, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("no such service: %s", in.GetName())
}
procs := procInterface.([]*Process)
for i := 0; i < len(procs); i++ {
state, err := generalProcessState(procs[i].pid, procs[i].command.String())
if err != nil {
return err
}
out.Pid = int32(state.Pid) //nolint:gosec
out.Command = state.Command
out.CpuPercent = float32(state.CPUPercent)
out.MemoryUsage = state.MemoryUsage
}
return nil
}
// Statuses returns status for the service with all processes
func (r *rpc) Statuses(in *serviceV1.Service, out *serviceV1.Statuses) error {
r.p.logger.Debug("service status", zap.String("name", in.GetName()))
r.mu.RLock()
defer r.mu.RUnlock()
_, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("the service with %s name doesn't exist", in.GetName())
}
procInterface, ok := r.p.processes.Load(in.GetName())
if !ok {
return fmt.Errorf("no such service: %s", in.GetName())
}
procs := procInterface.([]*Process)
for i := 0; i < len(procs); i++ {
state, err := generalProcessState(procs[i].pid, procs[i].command.String())
if err != nil {
/*
in case of error, just add the error status + common info (pid, command)
*/
out.Status = append(out.Status, &serviceV1.Status{
CpuPercent: 0,
Pid: int32(procs[i].pid), //nolint:gosec
MemoryUsage: 0,
Command: procs[i].command.String(),
Status: &shared.Status{
Code: 0,
Message: err.Error(),
},
})
continue
}
out.Status = append(out.Status, &serviceV1.Status{
CpuPercent: float32(state.CPUPercent),
Pid: int32(state.Pid), //nolint:gosec
MemoryUsage: state.MemoryUsage,
Command: state.Command,
Status: nil,
})
}
return nil
}
func (r *rpc) List(_ *serviceV1.Service, out *serviceV1.List) error {
r.mu.RLock()
defer r.mu.RUnlock()
r.p.processes.Range(func(key, _ any) bool {
r.p.logger.Debug("services list", zap.String("service", key.(string)))
out.Services = append(out.Services, key.(string))
return true
})
return nil
}