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
|
package app
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"go.uber.org/zap"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/util"
)
const ServiceName = "app"
type Env map[string]string
// WorkerFactory creates workers for the application.
type WorkerFactory interface {
CmdFactory(env Env) (func() *exec.Cmd, error)
NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error)
NewWorkerPool(ctx context.Context, opt roadrunner.Config, env Env) (roadrunner.Pool, error)
}
// App manages worker
type App struct {
cfg Config
log *zap.Logger
factory roadrunner.Factory
}
// Init application provider.
func (app *App) Init(cfg config.Provider, log *zap.Logger) error {
err := cfg.UnmarshalKey(ServiceName, &app.cfg)
if err != nil {
return err
}
app.cfg.InitDefaults()
app.log = log
return nil
}
// Name contains service name.
func (app *App) Name() string {
return ServiceName
}
func (app *App) Serve() chan error {
errCh := make(chan error, 1)
var err error
app.factory, err = app.initFactory()
if err != nil {
errCh <- errors.E(errors.Op("init factory"), err)
}
return errCh
}
func (app *App) Stop() error {
if app.factory == nil {
return nil
}
return app.factory.Close(context.Background())
}
// CmdFactory provides worker command factory assocated with given context.
func (app *App) CmdFactory(env Env) (func() *exec.Cmd, error) {
var cmdArgs []string
// create command according to the config
cmdArgs = append(cmdArgs, strings.Split(app.cfg.Command, " ")...)
return func() *exec.Cmd {
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
util.IsolateProcess(cmd)
// if user is not empty, and OS is linux or macos
// execute php worker from that particular user
if app.cfg.User != "" {
err := util.ExecuteFromUser(cmd, app.cfg.User)
if err != nil {
return nil
}
}
cmd.Env = app.setEnv(env)
return cmd
}, nil
}
// NewWorker issues new standalone worker.
func (app *App) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) {
spawnCmd, err := app.CmdFactory(env)
if err != nil {
return nil, err
}
w, err := app.factory.SpawnWorkerWithContext(ctx, spawnCmd())
if err != nil {
return nil, err
}
w.AddListener(app.collectLogs)
return w, nil
}
// NewWorkerPool issues new worker pool.
func (app *App) NewWorkerPool(ctx context.Context, opt roadrunner.Config, env Env) (roadrunner.Pool, error) {
spawnCmd, err := app.CmdFactory(env)
if err != nil {
return nil, err
}
p, err := roadrunner.NewPool(ctx, spawnCmd, app.factory, opt)
if err != nil {
return nil, err
}
p.AddListener(app.collectLogs)
return p, nil
}
// creates relay and worker factory.
func (app *App) initFactory() (roadrunner.Factory, error) {
if app.cfg.Relay == "" || app.cfg.Relay == "pipes" {
return roadrunner.NewPipeFactory(), nil
}
dsn := strings.Split(app.cfg.Relay, "://")
if len(dsn) != 2 {
return nil, errors.E(errors.Str("invalid DSN (tcp://:6001, unix://file.sock)"))
}
lsn, err := util.CreateListener(app.cfg.Relay)
if err != nil {
return nil, err
}
switch dsn[0] {
// sockets group
case "unix":
return roadrunner.NewSocketServer(lsn, app.cfg.RelayTimeout), nil
case "tcp":
return roadrunner.NewSocketServer(lsn, app.cfg.RelayTimeout), nil
default:
return nil, errors.E(errors.Str("invalid DSN (tcp://:6001, unix://file.sock)"))
}
}
func (app *App) setEnv(e Env) []string {
env := append(os.Environ(), fmt.Sprintf("RR_RELAY=%s", app.cfg.Relay))
for k, v := range e {
env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(k), v))
}
return env
}
func (app *App) collectLogs(event interface{}) {
if we, ok := event.(roadrunner.WorkerEvent); ok {
switch we.Event {
case roadrunner.EventWorkerError:
app.log.Error(we.Payload.(error).Error(), zap.Int64("pid", we.Worker.Pid()))
case roadrunner.EventWorkerLog:
app.log.Debug(strings.TrimRight(string(we.Payload.([]byte)), " \n\t"), zap.Int64("pid", we.Worker.Pid()))
}
}
}
|