-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.go
179 lines (137 loc) · 3.72 KB
/
plugin.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
package service
import (
"context"
"sync"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/state/process"
"go.uber.org/zap"
)
const PluginName string = "service"
type Plugin struct {
mu sync.Mutex
logger *zap.Logger
cfg Config
// all processes attached to the service
processes sync.Map // key -> []*Process
}
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if a config section exists.
Has(name string) bool
}
type Logger interface {
NamedLogger(name string) *zap.Logger
}
func (p *Plugin) Init(cfg Configurer, log Logger) error {
const op = errors.Op("service_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(errors.Disabled)
}
err := cfg.UnmarshalKey(PluginName, &p.cfg.Services)
if err != nil {
return errors.E(op, err)
}
// init default parameters if not set by user
p.cfg.InitDefault()
// save the logger
p.logger = log.NamedLogger(PluginName)
return nil
}
func (p *Plugin) Serve() chan error {
errCh := make(chan error, 1)
// start processing
go func() {
// lock here, because Stop command might be invoked during the Serve
p.mu.Lock()
defer p.mu.Unlock()
for k := range p.cfg.Services {
// create needed number of the processes
procs := make([]*Process, p.cfg.Services[k].ProcessNum)
for i := 0; i < p.cfg.Services[k].ProcessNum; i++ {
// create processor structure, which will process all the services
procs[i] = NewServiceProcess(p.cfg.Services[k], k, p.logger)
}
// store all the processes idents
p.processes.Store(k, procs)
}
p.processes.Range(func(key, value any) bool {
procs := value.([]*Process)
for i := 0; i < len(procs); i++ {
cmdStr := procs[i].service.Command
err := procs[i].start()
if err != nil {
errCh <- err
return false
}
p.logger.Info("service was started", zap.String("name", key.(string)), zap.String("command", cmdStr))
}
return true
})
}()
return errCh
}
func (p *Plugin) Weight() uint {
return 10
}
func (p *Plugin) Reset() error {
p.processes.Range(func(key, value any) bool {
procs := value.([]*Process)
newProcs := make([]*Process, len(procs))
for i := 0; i < len(procs); i++ {
procs[i].stop()
p.processes.Delete(key)
service := &Service{}
*service = *(procs[i]).service
newProc := NewServiceProcess(service, key.(string), p.logger)
err := newProc.start()
if err != nil {
p.logger.Error("unable to start the service", zap.String("name", key.(string)))
return true
}
newProcs[i] = newProc
}
p.processes.Store(key, newProcs)
return true
})
return nil
}
func (p *Plugin) Workers() []*process.State {
p.mu.Lock()
defer p.mu.Unlock()
states := make([]*process.State, 0, 5)
p.processes.Range(func(key, value interface{}) bool {
k := key.(string)
procs := value.([]*Process)
for i := 0; i < len(procs); i++ {
st, err := generalProcessState(procs[i].pid, procs[i].command.String())
if err != nil {
p.logger.Error("get process state", zap.String("name", k), zap.String("command", procs[i].command.String()))
return true
}
states = append(states, st)
}
return true
})
return states
}
func (p *Plugin) Stop(context.Context) error {
p.processes.Range(func(key, value interface{}) bool {
k := key.(string)
procs := value.([]*Process)
for i := 0; i < len(procs); i++ {
procs[i].stop()
p.logger.Info("service was stopped", zap.String("name", k), zap.String("command", procs[i].service.Command))
p.processes.Delete(key)
}
return true
})
return nil
}
// Name contains service name.
func (p *Plugin) Name() string {
return PluginName
}
func (p *Plugin) RPC() any {
return &rpc{p: p}
}