summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-10 16:42:15 +0400
committerGitHub <[email protected]>2018-09-10 16:42:15 +0400
commita554a98dda0d793da09db17314ef3977a2f3a465 (patch)
tree64b506898d283c39babc48da5a29df203cb57b49
parentea97c188a4a74c00b585cb50fa1ed4db7d190e09 (diff)
parent46a06a4d104802fb4271e06da487f74f23edd10c (diff)
Merge pull request #33 from spiral/feature/env-setterv1.2.0
Feature/env setter
-rw-r--r--CHANGELOG.md15
-rwxr-xr-xbuild.sh2
-rw-r--r--cmd/rr/cmd/root.go36
-rw-r--r--cmd/rr/http/debug.go20
-rw-r--r--cmd/rr/main.go11
-rw-r--r--error_buffer.go7
-rw-r--r--git0
-rw-r--r--server_config.go38
-rw-r--r--server_config_test.go44
-rw-r--r--service/container.go11
-rw-r--r--service/env/environment.go (renamed from service/env/provider.go)5
-rw-r--r--service/env/service.go23
-rw-r--r--service/env/service_test.go27
-rw-r--r--service/http/config.go24
-rw-r--r--service/http/handler_test.go58
-rw-r--r--service/http/rpc.go4
-rw-r--r--service/http/rpc_test.go7
-rw-r--r--service/http/service.go4
-rw-r--r--service/http/service_test.go4
-rw-r--r--service/rpc/service.go17
-rw-r--r--service/rpc/service_test.go10
-rw-r--r--service/static/config.go8
22 files changed, 300 insertions, 75 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d201b38f..2cf8fdce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,21 @@
CHANGELOG
=========
+v1.2.0 (10.09.2018)
+-------
+- added ability to request `*logrus.Logger`, `logrus.StdLogger`, `logrus.FieldLogger` dependency
+in container
+- added ability to set env values using `env.Enviroment`
+- `env.Provider` renamed to `env.Enviroment`
+- rr does not throw warning when service config is missing, instead debug level is used
+- rr server config now support default value set (shorter configs)
+- debug handlers has been moved from root command and now can be defined by each service
+- bugfix: panic when using debug mode without http service registered
+- `rr.Verbose` and `rr.Debug` are not public
+- rpc service now exposes it's address to underlying workers to simplify the connection
+- env service construction has been simplified in order to unify it with other services
+- more tests
+
v1.1.1 (26.07.2018)
-------
- added support for custom env variables
diff --git a/build.sh b/build.sh
index 34dc79f6..0f7b8ef3 100755
--- a/build.sh
+++ b/build.sh
@@ -2,7 +2,7 @@
cd $(dirname "${BASH_SOURCE[0]}")
OD="$(pwd)"
# Pushes application version into the build information.
-RR_VERSION=1.1.1
+RR_VERSION=1.2.0
# Hardcode some values to the core package
LDFLAGS="$LDFLAGS -X github.com/spiral/roadrunner/cmd/rr/cmd.Version=${RR_VERSION}"
diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go
index 595395c0..5e936580 100644
--- a/cmd/rr/cmd/root.go
+++ b/cmd/rr/cmd/root.go
@@ -24,17 +24,20 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
- "github.com/spiral/roadrunner/cmd/rr/debug"
"github.com/spiral/roadrunner/cmd/rr/utils"
"github.com/spiral/roadrunner/service"
- "github.com/spiral/roadrunner/service/http"
"os"
)
// Service bus for all the commands.
var (
- cfgFile string
- verbose, debugMode bool
+ cfgFile string
+
+ // Verbose enables verbosity mode (container specific).
+ Verbose bool
+
+ // Debug enables debug mode (service specific).
+ Debug bool
// Logger - shared logger.
Logger = logrus.New()
@@ -78,6 +81,13 @@ func (w *ViperWrapper) Unmarshal(out interface{}) error {
// Execute adds all child commands to the CLI command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the CLI.
func Execute() {
+ if cfg := initConfig(cfgFile, []string{"."}, ".rr"); cfg != nil {
+ if err := Container.Init(cfg); err != nil {
+ utils.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err)
+ os.Exit(1)
+ }
+ }
+
if err := CLI.Execute(); err != nil {
utils.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err)
os.Exit(1)
@@ -85,26 +95,14 @@ func Execute() {
}
func init() {
- CLI.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
- CLI.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "debug mode")
+ CLI.PersistentFlags().BoolVarP(&Verbose, "Verbose", "v", false, "Verbose output")
+ CLI.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "debug mode")
CLI.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is .rr.yaml)")
cobra.OnInitialize(func() {
- if verbose {
+ if Verbose {
Logger.SetLevel(logrus.DebugLevel)
}
-
- if debugMode {
- svc, _ := Container.Get(http.ID)
- svc.(*http.Service).AddListener(debug.Listener(Logger))
- }
-
- if cfg := initConfig(cfgFile, []string{"."}, ".rr"); cfg != nil {
- if err := Container.Init(cfg); err != nil {
- utils.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err)
- os.Exit(1)
- }
- }
})
}
diff --git a/cmd/rr/http/debug.go b/cmd/rr/http/debug.go
new file mode 100644
index 00000000..f69e10a8
--- /dev/null
+++ b/cmd/rr/http/debug.go
@@ -0,0 +1,20 @@
+package http
+
+import (
+ rr "github.com/spiral/roadrunner/cmd/rr/cmd"
+
+ "github.com/spf13/cobra"
+ "github.com/spiral/roadrunner/cmd/rr/debug"
+ "github.com/spiral/roadrunner/service/http"
+)
+
+func init() {
+ cobra.OnInitialize(func() {
+ if rr.Debug {
+ svc, _ := rr.Container.Get(http.ID)
+ if svc, ok := svc.(*http.Service); ok {
+ svc.AddListener(debug.Listener(rr.Logger))
+ }
+ }
+ })
+}
diff --git a/cmd/rr/main.go b/cmd/rr/main.go
index 01a5aaf3..18e22cdd 100644
--- a/cmd/rr/main.go
+++ b/cmd/rr/main.go
@@ -23,9 +23,7 @@
package main
import (
- // colorful logging
"github.com/sirupsen/logrus"
-
rr "github.com/spiral/roadrunner/cmd/rr/cmd"
// services (plugins)
@@ -34,19 +32,18 @@ import (
"github.com/spiral/roadrunner/service/rpc"
"github.com/spiral/roadrunner/service/static"
- // additional command handlers
+ // additional command and debug handlers
_ "github.com/spiral/roadrunner/cmd/rr/http"
)
func main() {
- rr.Logger.Formatter = &logrus.TextFormatter{ForceColors: true}
-
- rr.Container.Register(env.ID, env.NewService(map[string]string{"rr": rr.Version}))
-
+ rr.Container.Register(env.ID, &env.Service{})
rr.Container.Register(rpc.ID, &rpc.Service{})
rr.Container.Register(http.ID, &http.Service{})
rr.Container.Register(static.ID, &static.Service{})
+ rr.Logger.Formatter = &logrus.TextFormatter{ForceColors: true}
+
// you can register additional commands using cmd.CLI
rr.Execute()
}
diff --git a/error_buffer.go b/error_buffer.go
index 211fe25f..fec789a9 100644
--- a/error_buffer.go
+++ b/error_buffer.go
@@ -6,11 +6,12 @@ import (
)
const (
- // EventStderrOutput - is triggered when worker sends data into stderr. The context is error message ([]byte).
+ // EventStderrOutput - is triggered when worker sends data into stderr. The context
+ // is error message ([]byte).
EventStderrOutput = 1900
- // WaitDuration - for how long error buffer should attempt to aggregate error messages before merging output
- // together since lastError update (required to keep error update together).
+ // WaitDuration - for how long error buffer should attempt to aggregate error messages
+ // before merging output together since lastError update (required to keep error update together).
WaitDuration = 100 * time.Millisecond
)
diff --git a/git b/git
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/git
diff --git a/server_config.go b/server_config.go
index 88d15e1f..e2f9266b 100644
--- a/server_config.go
+++ b/server_config.go
@@ -33,6 +33,44 @@ type ServerConfig struct {
env []string
}
+// SetDefaults sets missing values to their default values.
+func (cfg *ServerConfig) SetDefaults() {
+ if cfg.Relay == "" {
+ cfg.Relay = "pipes"
+ }
+
+ if cfg.RelayTimeout == 0 {
+ cfg.RelayTimeout = time.Minute
+ }
+
+ if cfg.Pool == nil {
+ cfg.Pool = &Config{}
+ }
+
+ if cfg.Pool.AllocateTimeout == 0 {
+ cfg.Pool.AllocateTimeout = time.Minute
+ }
+
+ if cfg.Pool.DestroyTimeout == 0 {
+ cfg.Pool.DestroyTimeout = time.Minute
+ }
+}
+
+// UpscaleDurations converts duration values from nanoseconds to seconds.
+func (cfg *ServerConfig) UpscaleDurations() {
+ if cfg.RelayTimeout < time.Microsecond {
+ cfg.RelayTimeout = time.Second * time.Duration(cfg.RelayTimeout.Nanoseconds())
+ }
+
+ if cfg.Pool.AllocateTimeout < time.Microsecond {
+ cfg.Pool.AllocateTimeout = time.Second * time.Duration(cfg.Pool.AllocateTimeout.Nanoseconds())
+ }
+
+ if cfg.Pool.DestroyTimeout < time.Microsecond {
+ cfg.Pool.DestroyTimeout = time.Second * time.Duration(cfg.Pool.DestroyTimeout.Nanoseconds())
+ }
+}
+
// Differs returns true if configuration has changed but ignores pool or cmd changes.
func (cfg *ServerConfig) Differs(new *ServerConfig) bool {
return cfg.Relay != new.Relay || cfg.RelayTimeout != new.RelayTimeout
diff --git a/server_config_test.go b/server_config_test.go
index 1831ae95..e116323d 100644
--- a/server_config_test.go
+++ b/server_config_test.go
@@ -4,6 +4,7 @@ import (
"github.com/stretchr/testify/assert"
"runtime"
"testing"
+ "time"
)
func Test_ServerConfig_PipeFactory(t *testing.T) {
@@ -90,3 +91,46 @@ func Test_ServerConfig_Cmd(t *testing.T) {
cmd := cfg.makeCommand()
assert.NotNil(t, cmd)
}
+
+func Test_ServerConfig_SetEnv(t *testing.T) {
+ cfg := &ServerConfig{
+ Command: "php php-src/tests/client.php pipes",
+ }
+
+ cfg.SetEnv("key", "value")
+
+ cmd := cfg.makeCommand()
+ assert.NotNil(t, cmd)
+
+ c := cmd()
+
+ assert.Contains(t, c.Env, "KEY=value")
+}
+
+func Test_ServerConfigDefaults(t *testing.T) {
+ cfg := &ServerConfig{
+ Command: "php php-src/tests/client.php pipes",
+ }
+
+ cfg.SetDefaults()
+
+ assert.Equal(t, "pipes", cfg.Relay)
+ assert.Equal(t, time.Minute, cfg.Pool.AllocateTimeout)
+ assert.Equal(t, time.Minute, cfg.Pool.DestroyTimeout)
+}
+
+func Test_Config_Upscale(t *testing.T) {
+ cfg := &ServerConfig{
+ Command: "php php-src/tests/client.php pipes",
+ RelayTimeout: 1,
+ Pool: &Config{
+ AllocateTimeout: 1,
+ DestroyTimeout: 1,
+ },
+ }
+
+ cfg.UpscaleDurations()
+ assert.Equal(t, time.Second, cfg.RelayTimeout)
+ assert.Equal(t, time.Second, cfg.Pool.AllocateTimeout)
+ assert.Equal(t, time.Second, cfg.Pool.DestroyTimeout)
+}
diff --git a/service/container.go b/service/container.go
index 3450a18c..861e1aac 100644
--- a/service/container.go
+++ b/service/container.go
@@ -133,7 +133,7 @@ func (c *container) Init(cfg Config) error {
if ok, err := c.initService(e.svc, cfg.Get(e.name)); err != nil {
// soft error (skipping)
if err == errNoConfig {
- c.log.Warningf("[%s]: no config has been provided", e.name)
+ c.log.Debugf("[%s]: no config has been provided", e.name)
continue
}
@@ -152,7 +152,7 @@ func (c *container) Init(cfg Config) error {
// Serve all configured services. Non blocking.
func (c *container) Serve() error {
var (
- numServing int
+ numServing = 0
done = make(chan interface{}, len(c.services))
)
@@ -213,7 +213,7 @@ func (c *container) Stop() {
func (c *container) initService(s interface{}, segment Config) (bool, error) {
r := reflect.TypeOf(s)
- m, ok := r.MethodByName("Init")
+ m, ok := r.MethodByName(InitMethod)
if !ok {
// no Init method is presented, assuming service does not need initialization.
return true, nil
@@ -251,6 +251,11 @@ func (c *container) resolveValues(s interface{}, m reflect.Method, cfg Config) (
case v.Implements(reflect.TypeOf((*Container)(nil)).Elem()): // container
values = append(values, reflect.ValueOf(c))
+ case v.Implements(reflect.TypeOf((*logrus.StdLogger)(nil)).Elem()),
+ v.Implements(reflect.TypeOf((*logrus.FieldLogger)(nil)).Elem()),
+ v.ConvertibleTo(reflect.ValueOf(c.log).Type()): // logger
+ values = append(values, reflect.ValueOf(c.log))
+
case v.Implements(reflect.TypeOf((*HydrateConfig)(nil)).Elem()): // injectable config
if cfg == nil {
return nil, errNoConfig
diff --git a/service/env/provider.go b/service/env/environment.go
index 2918f18c..8e89d2c8 100644
--- a/service/env/provider.go
+++ b/service/env/environment.go
@@ -2,7 +2,10 @@ package env
// Provider aggregates list of environment variables. This interface can be used in custom implementation to drive
// values from external sources.
-type Provider interface {
+type Environment interface {
// GetEnv must return list of env variables.
GetEnv() (map[string]string, error)
+
+ // SetEnv sets or creates environment value.
+ SetEnv(key, value string)
}
diff --git a/service/env/service.go b/service/env/service.go
index 0822d55a..41e70bee 100644
--- a/service/env/service.go
+++ b/service/env/service.go
@@ -1,7 +1,12 @@
package env
-// ID contains default svc name.
-const ID = "env"
+const (
+ // ID contains default service name.
+ ID = "env"
+
+ // rrKey contains default env key to indicate than php running in RR mode.
+ rrKey = "rr"
+)
// Service provides ability to map _ENV values from config file.
type Service struct {
@@ -12,16 +17,17 @@ type Service struct {
// NewService creates new env service instance for given rr version.
func NewService(defaults map[string]string) *Service {
s := &Service{values: defaults}
- if s.values == nil {
- s.values = make(map[string]string)
- }
-
return s
}
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (s *Service) Init(cfg *Config) (bool, error) {
+ if s.values == nil {
+ s.values = make(map[string]string)
+ s.values[rrKey] = "yes"
+ }
+
for k, v := range cfg.Values {
s.values[k] = v
}
@@ -33,3 +39,8 @@ func (s *Service) Init(cfg *Config) (bool, error) {
func (s *Service) GetEnv() (map[string]string, error) {
return s.values, nil
}
+
+// SetEnv sets or creates environment value.
+func (s *Service) SetEnv(key, value string) {
+ s.values[key] = value
+}
diff --git a/service/env/service_test.go b/service/env/service_test.go
index 69a2a375..28e0d15b 100644
--- a/service/env/service_test.go
+++ b/service/env/service_test.go
@@ -10,6 +10,16 @@ func Test_NewService(t *testing.T) {
assert.Len(t, s.values, 1)
}
+func Test_Init(t *testing.T) {
+ s := &Service{}
+ s.Init(&Config{})
+ assert.Len(t, s.values, 1)
+
+ values, err := s.GetEnv()
+ assert.NoError(t, err)
+ assert.Equal(t, "yes", values["rr"])
+}
+
func Test_Extend(t *testing.T) {
s := NewService(map[string]string{"rr": "version"})
@@ -22,3 +32,20 @@ func Test_Extend(t *testing.T) {
assert.Equal(t, "version", values["rr"])
assert.Equal(t, "value", values["key"])
}
+
+func Test_Set(t *testing.T) {
+ s := NewService(map[string]string{"rr": "version"})
+
+ s.Init(&Config{Values: map[string]string{"key": "value"}})
+ assert.Len(t, s.values, 2)
+
+ s.SetEnv("key", "value-new")
+ s.SetEnv("other", "new")
+
+ values, err := s.GetEnv()
+ assert.NoError(t, err)
+ assert.Len(t, values, 3)
+ assert.Equal(t, "version", values["rr"])
+ assert.Equal(t, "value-new", values["key"])
+ assert.Equal(t, "new", values["other"])
+}
diff --git a/service/http/config.go b/service/http/config.go
index 20a247fb..5be42ae6 100644
--- a/service/http/config.go
+++ b/service/http/config.go
@@ -5,12 +5,11 @@ import (
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
"strings"
- "time"
)
// Config configures RoadRunner HTTP server.
type Config struct {
- // Enable enables http svc.
+ // Enable enables http service.
Enable bool
// Address and port to handle as http server.
@@ -32,25 +31,16 @@ func (c *Config) Hydrate(cfg service.Config) error {
return err
}
- if err := c.Valid(); err != nil {
- return err
+ if !c.Enable {
+ return nil
}
- if c.Workers.Relay == "" {
- c.Workers.Relay = "pipes"
- }
-
- if c.Workers.RelayTimeout < time.Microsecond {
- c.Workers.RelayTimeout = time.Second * time.Duration(c.Workers.RelayTimeout.Nanoseconds())
- }
-
- if c.Workers.Pool.AllocateTimeout < time.Microsecond {
- c.Workers.Pool.AllocateTimeout = time.Second * time.Duration(c.Workers.Pool.AllocateTimeout.Nanoseconds())
+ if err := c.Valid(); err != nil {
+ return err
}
- if c.Workers.Pool.DestroyTimeout < time.Microsecond {
- c.Workers.Pool.DestroyTimeout = time.Second * time.Duration(c.Workers.Pool.DestroyTimeout.Nanoseconds())
- }
+ c.Workers.SetDefaults()
+ c.Workers.UpscaleDurations()
return nil
}
diff --git a/service/http/handler_test.go b/service/http/handler_test.go
index 59a4c7c0..b82fc938 100644
--- a/service/http/handler_test.go
+++ b/service/http/handler_test.go
@@ -14,6 +14,7 @@ import (
"strings"
"testing"
"time"
+ "net/http/httptest"
)
// get request and return body
@@ -63,6 +64,63 @@ func TestServer_Echo(t *testing.T) {
assert.Equal(t, "WORLD", body)
}
+func Test_HandlerErrors(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ wr := httptest.NewRecorder()
+ rq := httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("data")))
+
+ st.ServeHTTP(wr, rq)
+ assert.Equal(t, 500, wr.Code)
+}
+
+func Test_Handler_JSON_error(t *testing.T) {
+ st := &Handler{
+ cfg: &Config{
+ MaxRequest: 1024,
+ Uploads: &UploadsConfig{
+ Dir: os.TempDir(),
+ Forbid: []string{},
+ },
+ },
+ rr: roadrunner.NewServer(&roadrunner.ServerConfig{
+ Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Relay: "pipes",
+ Pool: &roadrunner.Config{
+ NumWorkers: 1,
+ AllocateTimeout: 10000000,
+ DestroyTimeout: 10000000,
+ },
+ }),
+ }
+
+ wr := httptest.NewRecorder()
+ rq := httptest.NewRequest("POST", "/", bytes.NewBuffer([]byte("{sd")))
+ rq.Header.Add("Content-Type","application/json")
+ rq.Header.Add("Content-Size","3")
+
+ st.ServeHTTP(wr, rq)
+ assert.Equal(t, 500, wr.Code)
+}
+
+
func TestServer_Headers(t *testing.T) {
st := &Handler{
cfg: &Config{
diff --git a/service/http/rpc.go b/service/http/rpc.go
index aebc5903..9dfe718e 100644
--- a/service/http/rpc.go
+++ b/service/http/rpc.go
@@ -29,7 +29,7 @@ type Worker struct {
// Reset resets underlying RR worker pool and restarts all of it's workers.
func (rpc *rpcServer) Reset(reset bool, r *string) error {
- if rpc.svc.srv == nil {
+ if rpc.svc == nil || rpc.svc.srv == nil {
return errors.New("http server is not running")
}
@@ -39,7 +39,7 @@ func (rpc *rpcServer) Reset(reset bool, r *string) error {
// Workers returns list of active workers and their stats.
func (rpc *rpcServer) Workers(list bool, r *WorkerList) error {
- if rpc.svc.srv == nil {
+ if rpc.svc == nil || rpc.svc.srv == nil {
return errors.New("http server is not running")
}
diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go
index c392b060..32bb776c 100644
--- a/service/http/rpc_test.go
+++ b/service/http/rpc_test.go
@@ -177,3 +177,10 @@ func Test_Workers(t *testing.T) {
assert.Equal(t, *ss.rr.Workers()[0].Pid, r.Workers[0].Pid)
}
+
+func Test_Errors(t *testing.T) {
+ r := &rpcServer{nil}
+
+ assert.Error(t, r.Reset(true, nil))
+ assert.Error(t, r.Workers(true, nil))
+}
diff --git a/service/http/service.go b/service/http/service.go
index 9f62f5af..f988a843 100644
--- a/service/http/service.go
+++ b/service/http/service.go
@@ -20,7 +20,7 @@ type middleware func(f http.HandlerFunc) http.HandlerFunc
// Service manages rr, http servers.
type Service struct {
cfg *Config
- env env.Provider
+ env env.Environment
lsns []func(event int, ctx interface{})
mdws []middleware
mu sync.Mutex
@@ -42,7 +42,7 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) {
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
-func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Provider) (bool, error) {
+func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) {
if !cfg.Enable {
return false, nil
}
diff --git a/service/http/service_test.go b/service/http/service_test.go
index 4e572335..8dab7cd4 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -6,6 +6,7 @@ import (
"github.com/sirupsen/logrus/hooks/test"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
+ "github.com/spiral/roadrunner/service/env"
"github.com/spiral/roadrunner/service/rpc"
"github.com/stretchr/testify/assert"
"io/ioutil"
@@ -13,7 +14,6 @@ import (
"os"
"testing"
"time"
- "github.com/spiral/roadrunner/service/env"
)
type testCfg struct {
@@ -49,7 +49,7 @@ func Test_Service_NoConfig(t *testing.T) {
c := service.NewContainer(logger)
c.Register(ID, &Service{})
- assert.Error(t, c.Init(&testCfg{httpCfg: `{}`}))
+ assert.Error(t, c.Init(&testCfg{httpCfg: `{"Enable":true}`}))
s, st := c.Get(ID)
assert.NotNil(t, s)
diff --git a/service/rpc/service.go b/service/rpc/service.go
index 6e231048..3ea6c5fc 100644
--- a/service/rpc/service.go
+++ b/service/rpc/service.go
@@ -3,12 +3,19 @@ package rpc
import (
"errors"
"github.com/spiral/goridge"
+ "github.com/spiral/roadrunner/service/env"
"net/rpc"
"sync"
)
-// ID contains default service name.
-const ID = "rpc"
+const (
+ // ID contains default service name.
+ ID = "rpc"
+
+ // rrKey defines environment key to be used to store information about
+ // rpc server connection.
+ envKey = "rr_rpc"
+)
// Service is RPC service.
type Service struct {
@@ -20,7 +27,7 @@ type Service struct {
}
// Init rpc service. Must return true if service is enabled.
-func (s *Service) Init(cfg *Config) (bool, error) {
+func (s *Service) Init(cfg *Config, env env.Environment) (bool, error) {
if !cfg.Enable {
return false, nil
}
@@ -28,6 +35,10 @@ func (s *Service) Init(cfg *Config) (bool, error) {
s.cfg = cfg
s.rpc = rpc.NewServer()
+ if env != nil {
+ env.SetEnv(envKey, cfg.Listen)
+ }
+
return true, nil
}
diff --git a/service/rpc/service_test.go b/service/rpc/service_test.go
index fc88d38d..59e0e05d 100644
--- a/service/rpc/service_test.go
+++ b/service/rpc/service_test.go
@@ -12,7 +12,7 @@ func (ts *testService) Echo(msg string, r *string) error { *r = msg; return nil
func Test_Disabled(t *testing.T) {
s := &Service{}
- ok, err := s.Init(&Config{Enable: false})
+ ok, err := s.Init(&Config{Enable: false}, nil)
assert.NoError(t, err)
assert.False(t, ok)
@@ -30,7 +30,7 @@ func Test_RegisterNotConfigured(t *testing.T) {
func Test_Enabled(t *testing.T) {
s := &Service{}
- ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9008"})
+ ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9008"}, nil)
assert.NoError(t, err)
assert.True(t, ok)
@@ -38,7 +38,7 @@ func Test_Enabled(t *testing.T) {
func Test_StopNonServing(t *testing.T) {
s := &Service{}
- ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9008"})
+ ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9008"}, nil)
assert.NoError(t, err)
assert.True(t, ok)
@@ -47,7 +47,7 @@ func Test_StopNonServing(t *testing.T) {
func Test_Serve_Errors(t *testing.T) {
s := &Service{}
- ok, err := s.Init(&Config{Enable: true, Listen: "mailformed"})
+ ok, err := s.Init(&Config{Enable: true, Listen: "mailformed"}, nil)
assert.NoError(t, err)
assert.True(t, ok)
@@ -60,7 +60,7 @@ func Test_Serve_Errors(t *testing.T) {
func Test_Serve_Client(t *testing.T) {
s := &Service{}
- ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9018"})
+ ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9018"}, nil)
assert.NoError(t, err)
assert.True(t, ok)
diff --git a/service/static/config.go b/service/static/config.go
index a6931907..be0ac3ed 100644
--- a/service/static/config.go
+++ b/service/static/config.go
@@ -1,7 +1,7 @@
package static
import (
- "github.com/pkg/errors"
+ "fmt"
"github.com/spiral/roadrunner/service"
"os"
"path"
@@ -35,18 +35,18 @@ func (c *Config) Valid() error {
if !c.Enable {
return nil
}
-
+
st, err := os.Stat(c.Dir)
if err != nil {
if os.IsNotExist(err) {
- return errors.New("root directory does not exists")
+ return fmt.Errorf("root directory '%s' does not exists", c.Dir)
}
return err
}
if !st.IsDir() {
- return errors.New("invalid root directory")
+ return fmt.Errorf("invalid root directory '%s'", c.Dir)
}
return nil