-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.go
186 lines (148 loc) · 4.26 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
180
181
182
183
184
185
186
package server
import (
"context"
"fmt"
"os"
"os/user"
"strconv"
"strings"
"sync"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
"github.com/roadrunner-server/pool/pool"
staticPool "github.com/roadrunner-server/pool/pool/static_pool"
"github.com/roadrunner-server/pool/worker"
)
// Plugin manages worker
type Plugin struct {
mu sync.Mutex
cfg *Config
rpcCfg *RPCConfig
preparedCmd []string
preparedEnvs []string
log *zap.Logger
factory pool.Factory
}
// Init application provider.
func (p *Plugin) Init(cfg Configurer, log NamedLogger) error {
const op = errors.Op("server_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(PluginName, &p.cfg)
if err != nil {
return errors.E(op, errors.Init, err)
}
err = cfg.UnmarshalKey(RPCPluginName, &p.rpcCfg)
if err != nil {
return errors.E(op, errors.Init, err)
}
err = p.cfg.InitDefaults()
if err != nil {
return errors.E(op, errors.Init, err)
}
p.log = new(zap.Logger)
p.log = log.NamedLogger(PluginName)
// here we may have 2 cases: command declared as a space-separated string or as a slice
switch len(p.cfg.Command) {
// command defined as a space-separated string
case 1:
// we know that the len is 1, so we can safely use the first element
p.preparedCmd = append(p.preparedCmd, strings.Split(p.cfg.Command[0], " ")...)
default:
// we have a slice with a 2 or more elements
// first element is the command, the rest are arguments
p.preparedCmd = p.cfg.Command
}
p.preparedEnvs = append(os.Environ(), fmt.Sprintf(RrRelay+"=%s", p.cfg.Relay))
if p.rpcCfg != nil && p.rpcCfg.Listen != "" {
p.preparedEnvs = append(p.preparedEnvs, fmt.Sprintf("%s=%s", RrRPC, p.rpcCfg.Listen))
}
// set env variables from the config
if len(p.cfg.Env) > 0 {
for k, v := range p.cfg.Env {
p.preparedEnvs = append(p.preparedEnvs, fmt.Sprintf("%s=%s", strings.ToUpper(k), os.Expand(v, os.Getenv)))
}
}
p.preparedEnvs = append(p.preparedEnvs, fmt.Sprintf("%s=%s", RrVersion, cfg.RRVersion()))
p.factory, err = initFactory(p.log, p.cfg.Relay)
if err != nil {
return errors.E(op, err)
}
return nil
}
// Name contains the service name.
func (p *Plugin) Name() string {
return PluginName
}
// Serve (Start) server plugin (just a mock here to satisfy interface)
func (p *Plugin) Serve() chan error {
errCh := make(chan error, 1)
if p.cfg.OnInit != nil {
err := newCommand(p.log, p.cfg.OnInit).start()
if err != nil {
p.log.Error("on_init was finished with errors", zap.Error(err))
}
}
return errCh
}
// Stop used to stop all allocated pools
func (p *Plugin) Stop(_ context.Context) error {
p.mu.Lock()
defer p.mu.Unlock()
return p.factory.Close()
}
// NewWorker issues new standalone worker.
func (p *Plugin) NewWorker(ctx context.Context, env map[string]string) (*worker.Process, error) {
const op = errors.Op("server_plugin_new_worker")
spawnCmd := p.cmdFactory(env)
w, err := p.factory.SpawnWorkerWithContext(ctx, spawnCmd())
if err != nil {
return nil, errors.E(op, err)
}
return w, nil
}
// NewPool issues new worker pool.
func (p *Plugin) NewPool(ctx context.Context, cfg *pool.Config, env map[string]string, _ *zap.Logger) (*staticPool.Pool, error) {
p.mu.Lock()
defer p.mu.Unlock()
pl, err := staticPool.NewPool(ctx, pool.Command(p.customCmd(env)), p.factory, cfg, p.log, staticPool.WithQueueSize(cfg.MaxQueueSize))
if err != nil {
return nil, err
}
return pl, nil
}
// UID returns a user id (if specified by user)
func (p *Plugin) UID() int {
if p.cfg.User == "" {
return 0
}
usr, err := user.Lookup(p.cfg.User)
if err != nil {
p.log.Error("failed to get user", zap.String("id", p.cfg.User))
return 0
}
usrI32, err := strconv.ParseInt(usr.Uid, 10, 32)
if err != nil {
p.log.Error("failed to parse user id", zap.String("id", p.cfg.User))
return 0
}
return int(usrI32)
}
// GID returns a group id (if specified by user)
func (p *Plugin) GID() int {
if p.cfg.User == "" {
return 0
}
usr, err := user.Lookup(p.cfg.User)
if err != nil {
p.log.Error("failed to get user", zap.String("id", p.cfg.User))
return 0
}
grI32, err := strconv.ParseInt(usr.Gid, 10, 32)
if err != nil {
p.log.Error("failed to parse group id", zap.String("id", p.cfg.Group))
return 0
}
return int(grI32)
}