-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin.go
More file actions
139 lines (112 loc) · 2.68 KB
/
Copy pathplugin.go
File metadata and controls
139 lines (112 loc) · 2.68 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
package service
import (
"context"
"log/slog"
"sync"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/v2/state/process"
)
const PluginName string = "service"
type Plugin struct {
// mu serializes explicit lifecycle operations. Groups protect configuration.
mu sync.Mutex
stopped bool
logger *slog.Logger
cfg Config
processes sync.Map // name -> *group
}
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) *slog.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 the user
p.cfg.InitDefault()
// save the logger
p.logger = log.NamedLogger(PluginName)
return nil
}
func (p *Plugin) Serve() chan error {
p.mu.Lock()
defer p.mu.Unlock()
errCh := make(chan error, 1)
for name, svc := range p.cfg.Services {
if _, exists := p.processes.Load(name); exists {
continue
}
g := newGroup(svc, name, p.logger)
p.processes.Store(name, g)
if err := g.start(); err != nil {
stopProcesses(g.pause())
errCh <- err
return errCh
}
}
return errCh
}
func (p *Plugin) Weight() uint {
return 10
}
func (p *Plugin) Reset() error {
p.mu.Lock()
defer p.mu.Unlock()
p.processes.Range(func(key, value any) bool {
g := value.(*group)
if err := g.restart(); err != nil {
g.stop()
p.processes.Delete(key)
p.logger.Error("unable to start the service", "name", key.(string), "error", err)
}
return true
})
return nil
}
func (p *Plugin) Workers() []*process.State {
states := make([]*process.State, 0, 5)
p.processes.Range(func(key, value any) bool {
k := key.(string)
procs := value.(*group).snapshot()
for i := range procs {
st, err := generalProcessState(procs[i].pid, procs[i].command.String())
if err != nil {
p.logger.Error("get process state", "name", k, "command", procs[i].command.String())
continue
}
states = append(states, st)
}
return true
})
return states
}
func (p *Plugin) Stop(context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
p.stopped = true
p.processes.Range(func(key, value any) bool {
g := value.(*group)
g.stop()
p.processes.Delete(key)
return true
})
return nil
}
// Name contains the service name.
func (p *Plugin) Name() string {
return PluginName
}
func (p *Plugin) RPC() any {
return &rpc{p: p}
}