diff options
-rw-r--r-- | .rr.yaml | 2 | ||||
-rw-r--r-- | .travis.yml | 4 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | cmd/rr/cmd/root.go | 6 | ||||
-rw-r--r-- | cmd/rr/limit/debug.go | 71 | ||||
-rw-r--r-- | cmd/rr/main.go | 5 | ||||
-rw-r--r-- | cmd/util/debug.go | 40 | ||||
-rw-r--r-- | server.go | 2 | ||||
-rw-r--r-- | service/http/handler.go | 2 | ||||
-rw-r--r-- | service/http/service.go | 32 | ||||
-rw-r--r-- | service/limit/config.go (renamed from service/watcher/config.go) | 2 | ||||
-rw-r--r-- | service/limit/controller.go (renamed from service/watcher/controller.go) | 2 | ||||
-rw-r--r-- | service/limit/service.go (renamed from service/watcher/service.go) | 16 | ||||
-rw-r--r-- | service/limit/state_filter.go (renamed from service/watcher/state_filter.go) | 2 |
15 files changed, 111 insertions, 79 deletions
@@ -62,7 +62,7 @@ http: destroyTimeout: 60 # monitors rr server(s) -watch: +limit: # check worker state each second interval: 1 diff --git a/.travis.yml b/.travis.yml index 59497766..c3ad45b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ script: - go test ./service/rpc -race -v -coverprofile=rpc.txt -covermode=atomic - go test ./service/http -race -v -coverprofile=http.txt -covermode=atomic - go test ./service/static -race -v -coverprofile=static.txt -covermode=atomic - - go test ./service/watcher -race -v -coverprofile=watcher.txt -covermode=atomic + - go test ./service/limit -race -v -coverprofile=limit.txt -covermode=atomic after_success: - bash <(curl -s https://codecov.io/bash) -f lib.txt @@ -31,7 +31,7 @@ after_success: - bash <(curl -s https://codecov.io/bash) -f rpc.txt - bash <(curl -s https://codecov.io/bash) -f http.txt - bash <(curl -s https://codecov.io/bash) -f static.txt - - bash <(curl -s https://codecov.io/bash) -f watcher.txt + - bash <(curl -s https://codecov.io/bash) -f limit.txt jobs: include: @@ -17,4 +17,4 @@ test: go test -v -race -cover ./service/rpc go test -v -race -cover ./service/http go test -v -race -cover ./service/static - go test -v -race -cover ./service/watcher + go test -v -race -cover ./service/limit @@ -35,7 +35,7 @@ Features: - automatic worker replacement and safe PHP process destruction - worker create/allocate/destroy timeouts - max jobs per worker -- worker lifecycle management +- worker lifecycle management (controller) - maxMemory (graceful stop) - maxTTL (graceful stop) - maxIdleTTL (graceful stop) diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index 1222177a..411391da 100644 --- a/cmd/rr/cmd/root.go +++ b/cmd/rr/cmd/root.go @@ -25,7 +25,7 @@ import ( "github.com/spf13/cobra" "github.com/spiral/roadrunner/cmd/util" "github.com/spiral/roadrunner/service" - "github.com/spiral/roadrunner/service/watcher" + "github.com/spiral/roadrunner/service/limit" "os" ) @@ -110,8 +110,8 @@ func init() { // global watcher config if Verbose { - wcv, _ := Container.Get(watcher.ID) - if wcv, ok := wcv.(*watcher.Service); ok { + wcv, _ := Container.Get(limit.ID) + if wcv, ok := wcv.(*limit.Service); ok { wcv.AddListener(func(event int, ctx interface{}) { util.LogEvent(Logger, event, ctx) }) diff --git a/cmd/rr/limit/debug.go b/cmd/rr/limit/debug.go new file mode 100644 index 00000000..bb25d099 --- /dev/null +++ b/cmd/rr/limit/debug.go @@ -0,0 +1,71 @@ +package limit + +import ( + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spiral/roadrunner" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/cmd/util" + "github.com/spiral/roadrunner/service/limit" +) + +func init() { + cobra.OnInitialize(func() { + if rr.Debug { + svc, _ := rr.Container.Get(limit.ID) + if svc, ok := svc.(*limit.Service); ok { + svc.AddListener((&debugger{logger: rr.Logger}).listener) + } + } + }) +} + +// listener provide debug callback for system events. With colors! +type debugger struct{ logger *logrus.Logger } + +// listener listens to http events and generates nice looking output. +func (s *debugger) listener(event int, ctx interface{}) { + if util.LogEvent(s.logger, event, ctx) { + // handler by default debug package + return + } + + // watchers + switch event { + case limit.EventMaxTTL: + w := ctx.(roadrunner.WorkerError) + s.logger.Debug(util.Sprintf( + "<white+hb>worker.%v</reset> <yellow>%s</reset>", + *w.Worker.Pid, + w.Caused, + )) + return + + case limit.EventMaxIdleTTL: + w := ctx.(roadrunner.WorkerError) + s.logger.Debug(util.Sprintf( + "<white+hb>worker.%v</reset> <yellow>%s</reset>", + *w.Worker.Pid, + w.Caused, + )) + return + + case limit.EventMaxMemory: + w := ctx.(roadrunner.WorkerError) + s.logger.Error(util.Sprintf( + "<white+hb>worker.%v</reset> <red>%s</reset>", + *w.Worker.Pid, + w.Caused, + )) + return + + case limit.EventMaxExecTTL: + w := ctx.(roadrunner.WorkerError) + s.logger.Error(util.Sprintf( + "<white+hb>worker.%v</reset> <red>%s</reset>", + *w.Worker.Pid, + w.Caused, + )) + return + } +} diff --git a/cmd/rr/main.go b/cmd/rr/main.go index dc2fbc20..196bede3 100644 --- a/cmd/rr/main.go +++ b/cmd/rr/main.go @@ -24,16 +24,17 @@ package main import ( rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/service/watcher" // services (plugins) "github.com/spiral/roadrunner/service/env" "github.com/spiral/roadrunner/service/http" + "github.com/spiral/roadrunner/service/limit" "github.com/spiral/roadrunner/service/rpc" "github.com/spiral/roadrunner/service/static" // additional commands and debug handlers _ "github.com/spiral/roadrunner/cmd/rr/http" + _ "github.com/spiral/roadrunner/cmd/rr/limit" ) func main() { @@ -41,7 +42,7 @@ func main() { rr.Container.Register(rpc.ID, &rpc.Service{}) rr.Container.Register(http.ID, &http.Service{}) rr.Container.Register(static.ID, &static.Service{}) - rr.Container.Register(watcher.ID, &watcher.Service{}) + rr.Container.Register(limit.ID, &limit.Service{}) // you can register additional commands using cmd.CLI rr.Execute() diff --git a/cmd/util/debug.go b/cmd/util/debug.go index c120eba2..9b94510d 100644 --- a/cmd/util/debug.go +++ b/cmd/util/debug.go @@ -3,7 +3,6 @@ package util import ( "github.com/sirupsen/logrus" "github.com/spiral/roadrunner" - "github.com/spiral/roadrunner/service/watcher" "strings" ) @@ -58,44 +57,5 @@ func LogEvent(logger *logrus.Logger, event int, ctx interface{}) bool { return true } - // watchers - switch event { - case watcher.EventMaxTTL: - w := ctx.(roadrunner.WorkerError) - logger.Debug(Sprintf( - "<white+hb>worker.%v</reset> <yellow>%s</reset>", - *w.Worker.Pid, - w.Caused, - )) - return true - - case watcher.EventMaxIdleTTL: - w := ctx.(roadrunner.WorkerError) - logger.Debug(Sprintf( - "<white+hb>worker.%v</reset> <yellow>%s</reset>", - *w.Worker.Pid, - w.Caused, - )) - return true - - case watcher.EventMaxMemory: - w := ctx.(roadrunner.WorkerError) - logger.Error(Sprintf( - "<white+hb>worker.%v</reset> <red>%s</reset>", - *w.Worker.Pid, - w.Caused, - )) - return true - - case watcher.EventMaxExecTTL: - w := ctx.(roadrunner.WorkerError) - logger.Error(Sprintf( - "<white+hb>worker.%v</reset> <red>%s</reset>", - *w.Worker.Pid, - w.Caused, - )) - return true - } - return false } @@ -63,7 +63,7 @@ func (s *Server) Listen(l func(event int, ctx interface{})) { s.lsn = l } -// Watch attaches worker controller. +// AddController attaches worker controller. func (s *Server) Watch(c Controller) { s.mu.Lock() defer s.mu.Unlock() diff --git a/service/http/handler.go b/service/http/handler.go index 280d67aa..254f5ca6 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -63,7 +63,7 @@ type Handler struct { lsn func(event int, ctx interface{}) } -// Listen attaches handler event watcher. +// Listen attaches handler event controller. func (h *Handler) Listen(l func(event int, ctx interface{})) { h.mul.Lock() defer h.mul.Unlock() diff --git a/service/http/service.go b/service/http/service.go index b76d8893..1239acca 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -27,21 +27,21 @@ type middleware func(f http.HandlerFunc) http.HandlerFunc // Services manages rr, http servers. type Service struct { - cfg *Config - env env.Environment - lsns []func(event int, ctx interface{}) - mdwr []middleware - mu sync.Mutex - rr *roadrunner.Server - watcher roadrunner.Controller - handler *Handler - http *http.Server - https *http.Server + cfg *Config + env env.Environment + lsns []func(event int, ctx interface{}) + mdwr []middleware + mu sync.Mutex + rr *roadrunner.Server + controller roadrunner.Controller + handler *Handler + http *http.Server + https *http.Server } -// Watch attaches watcher. -func (s *Service) Watch(w roadrunner.Controller) { - s.watcher = w +// AddController attaches controller. Currently only one controller is supported. +func (s *Service) AddController(w roadrunner.Controller) { + s.controller = w } // AddMiddleware adds new net/http mdwr. @@ -49,7 +49,7 @@ func (s *Service) AddMiddleware(m middleware) { s.mdwr = append(s.mdwr, m) } -// AddListener attaches server event watcher. +// AddListener attaches server event controller. func (s *Service) AddListener(l func(event int, ctx interface{})) { s.lsns = append(s.lsns, l) } @@ -84,8 +84,8 @@ func (s *Service) Serve() error { s.rr = roadrunner.NewServer(s.cfg.Workers) s.rr.Listen(s.throw) - if s.watcher != nil { - s.rr.Watch(s.watcher) + if s.controller != nil { + s.rr.Watch(s.controller) } s.handler = &Handler{cfg: s.cfg, rr: s.rr} diff --git a/service/watcher/config.go b/service/limit/config.go index 8151005d..bf842ac2 100644 --- a/service/watcher/config.go +++ b/service/limit/config.go @@ -1,4 +1,4 @@ -package watcher +package limit import ( "github.com/spiral/roadrunner" diff --git a/service/watcher/controller.go b/service/limit/controller.go index 38eddf84..bdbab003 100644 --- a/service/watcher/controller.go +++ b/service/limit/controller.go @@ -1,4 +1,4 @@ -package watcher +package limit import ( "fmt" diff --git a/service/watcher/service.go b/service/limit/service.go index 3db23b68..72673d1f 100644 --- a/service/watcher/service.go +++ b/service/limit/service.go @@ -1,4 +1,4 @@ -package watcher +package limit import ( "github.com/spiral/roadrunner" @@ -6,12 +6,12 @@ import ( ) // ID defines controller service name. -const ID = "control" +const ID = "constrain" -// Watchable defines the ability to attach rr controller. -type Watchable interface { - // Watch attaches controller to the service. - Watch(w roadrunner.Controller) +// Controllable defines the ability to attach rr controller. +type Controllable interface { + // AddController attaches controller to the service. + AddController(c roadrunner.Controller) } // Services to control the state of rr service inside other services. @@ -25,8 +25,8 @@ func (s *Service) Init(cfg *Config, c service.Container) (bool, error) { // mount Services to designated services for id, watcher := range cfg.Controllers(s.throw) { svc, _ := c.Get(id) - if watchable, ok := svc.(Watchable); ok { - watchable.Watch(watcher) + if ctrl, ok := svc.(Controllable); ok { + ctrl.AddController(watcher) } } diff --git a/service/watcher/state_filter.go b/service/limit/state_filter.go index d85f1308..cd2eca94 100644 --- a/service/watcher/state_filter.go +++ b/service/limit/state_filter.go @@ -1,4 +1,4 @@ -package watcher +package limit import ( "github.com/spiral/roadrunner" |