diff options
author | Valery Piashchynski <[email protected]> | 2022-07-15 15:43:01 +0200 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-07-15 15:43:01 +0200 |
commit | 3dfd34d5d4b9f47dae203dd27af9fdba2de67960 (patch) | |
tree | 88539938a4a61faf0221379ac34754042adabadb /container | |
parent | 5d598819f74a860b4f265fb6fde033da008d706e (diff) |
- move container from the internal folder
- channel with the proper size
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'container')
-rw-r--r-- | container/config.go | 88 | ||||
-rw-r--r-- | container/config_test.go | 69 | ||||
-rw-r--r-- | container/container.go | 20 | ||||
-rw-r--r-- | container/container_test.go | 25 | ||||
-rw-r--r-- | container/plugins.go | 112 | ||||
-rw-r--r-- | container/plugins_test.go | 18 | ||||
-rw-r--r-- | container/test/endure_ok.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_debug.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_fatal.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_foobar.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_info.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_panic.yaml | 5 | ||||
-rw-r--r-- | container/test/endure_ok_warn.yaml | 5 | ||||
-rw-r--r-- | container/test/without_endure_ok.yaml | 0 |
14 files changed, 367 insertions, 0 deletions
diff --git a/container/config.go b/container/config.go new file mode 100644 index 00000000..fb5b028e --- /dev/null +++ b/container/config.go @@ -0,0 +1,88 @@ +package container + +import ( + "fmt" + "time" + + endure "github.com/roadrunner-server/endure/pkg/container" + "github.com/spf13/viper" +) + +type Config struct { + GracePeriod time.Duration + PrintGraph bool + LogLevel endure.Level +} + +const ( + endureKey = "endure" + defaultGracePeriod = time.Second * 30 +) + +// NewConfig creates endure container configuration. +func NewConfig(cfgFile string) (*Config, error) { + v := viper.New() + v.SetConfigFile(cfgFile) + + err := v.ReadInConfig() + if err != nil { + return nil, err + } + + if !v.IsSet(endureKey) { + return &Config{ // return config with defaults + GracePeriod: defaultGracePeriod, + PrintGraph: false, + LogLevel: endure.ErrorLevel, + }, nil + } + + rrCfgEndure := struct { + GracePeriod time.Duration `mapstructure:"grace_period"` + PrintGraph bool `mapstructure:"print_graph"` + LogLevel string `mapstructure:"log_level"` + }{} + + err = v.UnmarshalKey(endureKey, &rrCfgEndure) + if err != nil { + return nil, err + } + + if rrCfgEndure.GracePeriod == 0 { + rrCfgEndure.GracePeriod = defaultGracePeriod + } + + if rrCfgEndure.LogLevel == "" { + rrCfgEndure.LogLevel = "error" + } + + logLevel, err := parseLogLevel(rrCfgEndure.LogLevel) + if err != nil { + return nil, err + } + + return &Config{ + GracePeriod: rrCfgEndure.GracePeriod, + PrintGraph: rrCfgEndure.PrintGraph, + LogLevel: logLevel, + }, nil +} + +func parseLogLevel(s string) (endure.Level, error) { + switch s { + case "debug": + return endure.DebugLevel, nil + case "info": + return endure.InfoLevel, nil + case "warn", "warning": + return endure.WarnLevel, nil + case "error": + return endure.ErrorLevel, nil + case "panic": + return endure.PanicLevel, nil + case "fatal": + return endure.FatalLevel, nil + } + + return endure.DebugLevel, fmt.Errorf(`unknown log level "%s" (allowed: debug, info, warn, error, panic, fatal)`, s) +} diff --git a/container/config_test.go b/container/config_test.go new file mode 100644 index 00000000..d07a80d4 --- /dev/null +++ b/container/config_test.go @@ -0,0 +1,69 @@ +package container_test + +import ( + "testing" + "time" + + "github.com/roadrunner-server/config/v2" + endure "github.com/roadrunner-server/endure/pkg/container" + "github.com/roadrunner-server/roadrunner/v2/container" + "github.com/stretchr/testify/assert" +) + +func TestNewConfig_SuccessfulReading(t *testing.T) { + c, err := container.NewConfig("test/endure_ok.yaml") + assert.NoError(t, err) + assert.NotNil(t, c) + + assert.Equal(t, time.Second*10, c.GracePeriod) + assert.True(t, c.PrintGraph) + assert.Equal(t, endure.WarnLevel, c.LogLevel) +} + +func TestNewConfig_WithoutEndureKey(t *testing.T) { + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte{}} + assert.NoError(t, cfgPlugin.Init()) + + c, err := container.NewConfig("test/without_endure_ok.yaml") + assert.NoError(t, err) + assert.NotNil(t, c) + + assert.Equal(t, time.Second*30, c.GracePeriod) + assert.False(t, c.PrintGraph) + assert.Equal(t, endure.ErrorLevel, c.LogLevel) +} + +func TestNewConfig_LoggingLevels(t *testing.T) { + for _, tt := range []struct { + path string + giveLevel string + wantLevel endure.Level + wantError bool + }{ + {path: "test/endure_ok_debug.yaml", giveLevel: "debug", wantLevel: endure.DebugLevel}, + {path: "test/endure_ok_info.yaml", giveLevel: "info", wantLevel: endure.InfoLevel}, + {path: "test/endure_ok_warn.yaml", giveLevel: "warn", wantLevel: endure.WarnLevel}, + {path: "test/endure_ok_panic.yaml", giveLevel: "panic", wantLevel: endure.PanicLevel}, + {path: "test/endure_ok_fatal.yaml", giveLevel: "fatal", wantLevel: endure.FatalLevel}, + + {path: "test/endure_ok_foobar.yaml", giveLevel: "foobar", wantError: true}, + } { + tt := tt + t.Run(tt.giveLevel, func(t *testing.T) { + cfgPlugin := &config.Plugin{Type: "yaml", ReadInCfg: []byte("endure:\n log_level: " + tt.giveLevel)} + assert.NoError(t, cfgPlugin.Init()) + + c, err := container.NewConfig(tt.path) + + if tt.wantError { + assert.Nil(t, c) + assert.Error(t, err) + assert.Contains(t, err.Error(), "unknown log level") + } else { + assert.NoError(t, err) + assert.NotNil(t, c) + assert.Equal(t, tt.wantLevel, c.LogLevel) + } + }) + } +} diff --git a/container/container.go b/container/container.go new file mode 100644 index 00000000..610fdbf9 --- /dev/null +++ b/container/container.go @@ -0,0 +1,20 @@ +package container + +import ( + endure "github.com/roadrunner-server/endure/pkg/container" +) + +// NewContainer creates endure container with all required options (based on container Config). Logger is nil by +// default. +func NewContainer(cfg Config) (*endure.Endure, error) { + endureOptions := []endure.Options{ + endure.SetLogLevel(cfg.LogLevel), + endure.GracefulShutdownTimeout(cfg.GracePeriod), + } + + if cfg.PrintGraph { + endureOptions = append(endureOptions, endure.Visualize(endure.StdOut, "")) + } + + return endure.NewContainer(nil, endureOptions...) +} diff --git a/container/container_test.go b/container/container_test.go new file mode 100644 index 00000000..ed080d38 --- /dev/null +++ b/container/container_test.go @@ -0,0 +1,25 @@ +package container_test + +import ( + "testing" + "time" + + endure "github.com/roadrunner-server/endure/pkg/container" + "github.com/roadrunner-server/roadrunner/v2/container" + "github.com/stretchr/testify/assert" +) + +func TestNewContainer(t *testing.T) { // there is no legal way to test container options + c, err := container.NewContainer(container.Config{}) + c2, err2 := container.NewContainer(container.Config{ + GracePeriod: time.Second, + PrintGraph: true, + LogLevel: endure.WarnLevel, + }) + + assert.NoError(t, err) + assert.NotNil(t, c) + + assert.NoError(t, err2) + assert.NotNil(t, c2) +} diff --git a/container/plugins.go b/container/plugins.go new file mode 100644 index 00000000..3ee8cdc7 --- /dev/null +++ b/container/plugins.go @@ -0,0 +1,112 @@ +package container + +import ( + "github.com/roadrunner-server/amqp/v2" + "github.com/roadrunner-server/beanstalk/v2" + "github.com/roadrunner-server/boltdb/v2" + "github.com/roadrunner-server/broadcast/v2" + "github.com/roadrunner-server/cache/v2" + "github.com/roadrunner-server/fileserver/v2" + grpcPlugin "github.com/roadrunner-server/grpc/v2" + "github.com/roadrunner-server/gzip/v2" + "github.com/roadrunner-server/headers/v2" + httpPlugin "github.com/roadrunner-server/http/v2" + "github.com/roadrunner-server/informer/v2" + "github.com/roadrunner-server/jobs/v2" + "github.com/roadrunner-server/logger/v2" + "github.com/roadrunner-server/memory/v2" + "github.com/roadrunner-server/metrics/v2" + "github.com/roadrunner-server/nats/v2" + newrelic "github.com/roadrunner-server/new_relic/v2" + rrOtel "github.com/roadrunner-server/otel/v2" + "github.com/roadrunner-server/prometheus/v2" + proxyIP "github.com/roadrunner-server/proxy_ip_parser/v2" + "github.com/roadrunner-server/redis/v2" + "github.com/roadrunner-server/reload/v2" + "github.com/roadrunner-server/resetter/v2" + rpcPlugin "github.com/roadrunner-server/rpc/v2" + "github.com/roadrunner-server/send/v2" + "github.com/roadrunner-server/server/v2" + "github.com/roadrunner-server/service/v2" + "github.com/roadrunner-server/sqs/v2" + "github.com/roadrunner-server/static/v2" + "github.com/roadrunner-server/status/v2" + "github.com/roadrunner-server/websockets/v2" + + "github.com/roadrunner-server/kv/v2" + "github.com/roadrunner-server/memcached/v2" + "github.com/roadrunner-server/tcp/v2" + rrt "github.com/temporalio/roadrunner-temporal" +) + +// Plugins returns active plugins for the endure container. Feel free to add or remove any plugins. +func Plugins() []interface{} { //nolint:funlen + return []interface{}{ + // bundled + // informer plugin (./rr workers, ./rr workers -i) + &informer.Plugin{}, + // resetter plugin (./rr reset) + &resetter.Plugin{}, + + // logger plugin + &logger.Plugin{}, + // metrics plugin + &metrics.Plugin{}, + // reload plugin + &reload.Plugin{}, + // rpc plugin (workers, reset) + &rpcPlugin.Plugin{}, + // server plugin (NewWorker, NewWorkerPool) + &server.Plugin{}, + // service plugin + &service.Plugin{}, + + // ========= JOBS bundle + &jobs.Plugin{}, + &amqp.Plugin{}, + &sqs.Plugin{}, + &nats.Plugin{}, + &beanstalk.Plugin{}, + // ========= + + // http server plugin with middleware + &httpPlugin.Plugin{}, + &newrelic.Plugin{}, + &static.Plugin{}, + &headers.Plugin{}, + &status.Plugin{}, + &gzip.Plugin{}, + &prometheus.Plugin{}, + &cache.Plugin{}, + &send.Plugin{}, + &proxyIP.Plugin{}, + &fileserver.Plugin{}, + &rrOtel.Plugin{}, + // =================== + + &grpcPlugin.Plugin{}, + // kv + ws + jobs plugin + &memory.Plugin{}, + // KV + Jobs + &boltdb.Plugin{}, + + // broadcast via memory or redis + // used in conjunction with Websockets, memory and redis plugins + &broadcast.Plugin{}, + // ======== websockets broadcast bundle + &websockets.Plugin{}, + &redis.Plugin{}, + // ========= + + // ============== KV + &kv.Plugin{}, + &memcached.Plugin{}, + // ============== + + // raw TCP connections handling + &tcp.Plugin{}, + + // temporal plugins + &rrt.Plugin{}, + } +} diff --git a/container/plugins_test.go b/container/plugins_test.go new file mode 100644 index 00000000..b857f09a --- /dev/null +++ b/container/plugins_test.go @@ -0,0 +1,18 @@ +package container + +import ( + "reflect" + "testing" +) + +func TestPlugins(t *testing.T) { + for _, p := range Plugins() { + if p == nil { + t.Error("plugin cannot be nil") + } + + if pk := reflect.TypeOf(p).Kind(); pk != reflect.Ptr && pk != reflect.Struct { + t.Errorf("plugin %v must be a structure or pointer to the structure", p) + } + } +} diff --git a/container/test/endure_ok.yaml b/container/test/endure_ok.yaml new file mode 100644 index 00000000..03db7c23 --- /dev/null +++ b/container/test/endure_ok.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: warn diff --git a/container/test/endure_ok_debug.yaml b/container/test/endure_ok_debug.yaml new file mode 100644 index 00000000..5953c4de --- /dev/null +++ b/container/test/endure_ok_debug.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: debug diff --git a/container/test/endure_ok_fatal.yaml b/container/test/endure_ok_fatal.yaml new file mode 100644 index 00000000..69e16467 --- /dev/null +++ b/container/test/endure_ok_fatal.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: fatal diff --git a/container/test/endure_ok_foobar.yaml b/container/test/endure_ok_foobar.yaml new file mode 100644 index 00000000..90728d2c --- /dev/null +++ b/container/test/endure_ok_foobar.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: foobar diff --git a/container/test/endure_ok_info.yaml b/container/test/endure_ok_info.yaml new file mode 100644 index 00000000..7ad62861 --- /dev/null +++ b/container/test/endure_ok_info.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: info diff --git a/container/test/endure_ok_panic.yaml b/container/test/endure_ok_panic.yaml new file mode 100644 index 00000000..5ac4dc9e --- /dev/null +++ b/container/test/endure_ok_panic.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: panic diff --git a/container/test/endure_ok_warn.yaml b/container/test/endure_ok_warn.yaml new file mode 100644 index 00000000..03db7c23 --- /dev/null +++ b/container/test/endure_ok_warn.yaml @@ -0,0 +1,5 @@ +endure: + grace_period: 10s + print_graph: true + retry_on_fail: true + log_level: warn diff --git a/container/test/without_endure_ok.yaml b/container/test/without_endure_ok.yaml new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/container/test/without_endure_ok.yaml |