diff options
Diffstat (limited to 'plugins/server/plugin.go')
-rw-r--r-- | plugins/server/plugin.go | 88 |
1 files changed, 46 insertions, 42 deletions
diff --git a/plugins/server/plugin.go b/plugins/server/plugin.go index e096708a..3411b007 100644 --- a/plugins/server/plugin.go +++ b/plugins/server/plugin.go @@ -25,117 +25,121 @@ type Plugin struct { } // Init application provider. -func (app *Plugin) Init(cfg config.Configurer, log log.Logger) error { +func (server *Plugin) Init(cfg config.Configurer, log log.Logger) error { const op = errors.Op("Init") - err := cfg.UnmarshalKey(ServiceName, &app.cfg) + err := cfg.UnmarshalKey(ServiceName, &server.cfg) if err != nil { return errors.E(op, errors.Init, err) } - app.cfg.InitDefaults() - app.log = log + server.cfg.InitDefaults() + server.log = log + + server.factory, err = server.initFactory() + if err != nil { + return errors.E(errors.Op("Init factory"), err) + } return nil } // Name contains service name. -func (app *Plugin) Name() string { +func (server *Plugin) Name() string { return ServiceName } -func (app *Plugin) Serve() chan error { +func (server *Plugin) 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 *Plugin) Stop() error { - if app.factory == nil { +func (server *Plugin) Stop() error { + if server.factory == nil { return nil } - return app.factory.Close(context.Background()) + return server.factory.Close(context.Background()) } // CmdFactory provides worker command factory assocated with given context. -func (app *Plugin) CmdFactory(env server.Env) (func() *exec.Cmd, error) { +func (server *Plugin) CmdFactory(env server.Env) (func() *exec.Cmd, error) { + const op = errors.Op("cmd factory") var cmdArgs []string // create command according to the config - cmdArgs = append(cmdArgs, strings.Split(app.cfg.Command, " ")...) - + cmdArgs = append(cmdArgs, strings.Split(server.cfg.Command, " ")...) + if len(cmdArgs) < 2 { + return nil, errors.E(op, errors.Str("should be in form of `php <script>")) + } + if cmdArgs[0] != "php" { + return nil, errors.E(op, errors.Str("first arg in command should be `php`")) + } return func() *exec.Cmd { - cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) + cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //nolint:gosec 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 server.cfg.User != "" { + err := util.ExecuteFromUser(cmd, server.cfg.User) if err != nil { return nil } } - cmd.Env = app.setEnv(env) + cmd.Env = server.setEnv(env) return cmd }, nil } // NewWorker issues new standalone worker. -func (app *Plugin) NewWorker(ctx context.Context, env server.Env) (roadrunner.WorkerBase, error) { +func (server *Plugin) NewWorker(ctx context.Context, env server.Env) (roadrunner.WorkerBase, error) { const op = errors.Op("new worker") - spawnCmd, err := app.CmdFactory(env) + spawnCmd, err := server.CmdFactory(env) if err != nil { return nil, errors.E(op, err) } - w, err := app.factory.SpawnWorkerWithContext(ctx, spawnCmd()) + w, err := server.factory.SpawnWorkerWithContext(ctx, spawnCmd()) if err != nil { return nil, errors.E(op, err) } - w.AddListener(app.collectLogs) + w.AddListener(server.collectLogs) return w, nil } // NewWorkerPool issues new worker pool. -func (app *Plugin) NewWorkerPool(ctx context.Context, opt roadrunner.PoolConfig, env server.Env) (roadrunner.Pool, error) { - spawnCmd, err := app.CmdFactory(env) +func (server *Plugin) NewWorkerPool(ctx context.Context, opt roadrunner.PoolConfig, env server.Env) (roadrunner.Pool, error) { + spawnCmd, err := server.CmdFactory(env) if err != nil { return nil, err } - p, err := roadrunner.NewPool(ctx, spawnCmd, app.factory, opt) + p, err := roadrunner.NewPool(ctx, spawnCmd, server.factory, opt) if err != nil { return nil, err } - p.AddListener(app.collectLogs) + p.AddListener(server.collectLogs) return p, nil } // creates relay and worker factory. -func (app *Plugin) initFactory() (roadrunner.Factory, error) { +func (server *Plugin) initFactory() (roadrunner.Factory, error) { const op = errors.Op("network factory init") - if app.cfg.Relay == "" || app.cfg.Relay == "pipes" { + if server.cfg.Relay == "" || server.cfg.Relay == "pipes" { return roadrunner.NewPipeFactory(), nil } - dsn := strings.Split(app.cfg.Relay, "://") + dsn := strings.Split(server.cfg.Relay, "://") if len(dsn) != 2 { return nil, errors.E(op, errors.Network, errors.Str("invalid DSN (tcp://:6001, unix://file.sock)")) } - lsn, err := util.CreateListener(app.cfg.Relay) + lsn, err := util.CreateListener(server.cfg.Relay) if err != nil { return nil, errors.E(op, errors.Network, err) } @@ -143,16 +147,16 @@ func (app *Plugin) initFactory() (roadrunner.Factory, error) { switch dsn[0] { // sockets group case "unix": - return roadrunner.NewSocketServer(lsn, app.cfg.RelayTimeout), nil + return roadrunner.NewSocketServer(lsn, server.cfg.RelayTimeout), nil case "tcp": - return roadrunner.NewSocketServer(lsn, app.cfg.RelayTimeout), nil + return roadrunner.NewSocketServer(lsn, server.cfg.RelayTimeout), nil default: return nil, errors.E(op, errors.Network, errors.Str("invalid DSN (tcp://:6001, unix://file.sock)")) } } -func (app *Plugin) setEnv(e server.Env) []string { - env := append(os.Environ(), fmt.Sprintf("RR_RELAY=%s", app.cfg.Relay)) +func (server *Plugin) setEnv(e server.Env) []string { + env := append(os.Environ(), fmt.Sprintf("RR_RELAY=%s", server.cfg.Relay)) for k, v := range e { env = append(env, fmt.Sprintf("%s=%s", strings.ToUpper(k), v)) } @@ -160,13 +164,13 @@ func (app *Plugin) setEnv(e server.Env) []string { return env } -func (app *Plugin) collectLogs(event interface{}) { +func (server *Plugin) collectLogs(event interface{}) { if we, ok := event.(roadrunner.WorkerEvent); ok { switch we.Event { case roadrunner.EventWorkerError: - app.log.Error(we.Payload.(error).Error(), "pid", we.Worker.Pid()) + server.log.Error(we.Payload.(error).Error(), "pid", we.Worker.Pid()) case roadrunner.EventWorkerLog: - app.log.Debug(strings.TrimRight(string(we.Payload.([]byte)), " \n\t"), "pid", we.Worker.Pid()) + server.log.Debug(strings.TrimRight(string(we.Payload.([]byte)), " \n\t"), "pid", we.Worker.Pid()) } } } |