diff options
author | Valery Piashchynski <[email protected]> | 2020-12-21 19:42:23 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-21 19:42:23 +0300 |
commit | ee8b4075c0f836d698d1ae505c87c17147de447a (patch) | |
tree | 531d980e5bfb94ee39b03952a97e0445f7955409 /plugins/checker | |
parent | 0ad45031047bb479e06ce0a0f496c6db9b2641c9 (diff) |
Move plugins to the roadrunner-plugins repository
Diffstat (limited to 'plugins/checker')
-rw-r--r-- | plugins/checker/config.go | 5 | ||||
-rw-r--r-- | plugins/checker/plugin.go | 152 | ||||
-rw-r--r-- | plugins/checker/rpc.go | 28 | ||||
-rwxr-xr-x | plugins/checker/tests/configs/.rr-checker-init.yaml | 31 | ||||
-rw-r--r-- | plugins/checker/tests/plugin_test.go | 188 |
5 files changed, 0 insertions, 404 deletions
diff --git a/plugins/checker/config.go b/plugins/checker/config.go deleted file mode 100644 index 5f952592..00000000 --- a/plugins/checker/config.go +++ /dev/null @@ -1,5 +0,0 @@ -package checker - -type Config struct { - Address string -} diff --git a/plugins/checker/plugin.go b/plugins/checker/plugin.go deleted file mode 100644 index 7aea33b7..00000000 --- a/plugins/checker/plugin.go +++ /dev/null @@ -1,152 +0,0 @@ -package checker - -import ( - "fmt" - "net/http" - "time" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/logger" - "github.com/spiral/endure" - "github.com/spiral/errors" - "github.com/spiral/roadrunner/v2/interfaces/config" - "github.com/spiral/roadrunner/v2/interfaces/log" - "github.com/spiral/roadrunner/v2/interfaces/status" -) - -const ( - // PluginName declares public plugin name. - PluginName = "status" -) - -type Plugin struct { - registry map[string]status.Checker - server *fiber.App - log log.Logger - cfg *Config -} - -func (c *Plugin) Init(log log.Logger, cfg config.Configurer) error { - const op = errors.Op("status plugin init") - err := cfg.UnmarshalKey(PluginName, &c.cfg) - if err != nil { - return errors.E(op, errors.Disabled, err) - } - - if c.cfg == nil { - return errors.E(errors.Disabled) - } - - c.registry = make(map[string]status.Checker) - c.log = log - return nil -} - -func (c *Plugin) Serve() chan error { - errCh := make(chan error, 1) - c.server = fiber.New(fiber.Config{ - ReadTimeout: time.Second * 5, - WriteTimeout: time.Second * 5, - IdleTimeout: time.Second * 5, - }) - c.server.Group("/v1", c.healthHandler) - c.server.Use(logger.New()) - c.server.Use("/health", c.healthHandler) - - go func() { - err := c.server.Listen(c.cfg.Address) - if err != nil { - errCh <- err - } - }() - - return errCh -} - -func (c *Plugin) Stop() error { - const op = errors.Op("checker stop") - err := c.server.Shutdown() - if err != nil { - return errors.E(op, err) - } - return nil -} - -// Reset named service. -func (c *Plugin) Status(name string) (status.Status, error) { - const op = errors.Op("get status") - svc, ok := c.registry[name] - if !ok { - return status.Status{}, errors.E(op, errors.Errorf("no such service: %s", name)) - } - - return svc.Status(), nil -} - -// CollectTarget collecting services which can provide Status. -func (c *Plugin) CollectTarget(name endure.Named, r status.Checker) error { - c.registry[name.Name()] = r - return nil -} - -// Collects declares services to be collected. -func (c *Plugin) Collects() []interface{} { - return []interface{}{ - c.CollectTarget, - } -} - -// Name of the service. -func (c *Plugin) Name() string { - return PluginName -} - -// RPCService returns associated rpc service. -func (c *Plugin) RPC() interface{} { - return &rpc{srv: c, log: c.log} -} - -type Plugins struct { - Plugins []string `query:"plugin"` -} - -const template string = "Service: %s: Status: %d\n" - -func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { - const op = errors.Op("health_handler") - plugins := &Plugins{} - err := ctx.QueryParser(plugins) - if err != nil { - return errors.E(op, err) - } - - if len(plugins.Plugins) == 0 { - ctx.Status(http.StatusOK) - _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: /v1/health?plugin=plugin1&plugin=plugin2 \n") - return nil - } - - failed := false - // iterate over all provided plugins - for i := 0; i < len(plugins.Plugins); i++ { - // check if the plugin exists - if plugin, ok := c.registry[plugins.Plugins[i]]; ok { - st := plugin.Status() - if st.Code >= 500 { - failed = true - continue - } else if st.Code >= 100 && st.Code <= 400 { - _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) - } - } else { - _, _ = ctx.WriteString(fmt.Sprintf("Service: %s not found", plugins.Plugins[i])) - } - } - if failed { - ctx.Status(http.StatusInternalServerError) - return nil - } - - ctx.Status(http.StatusOK) - return nil -} diff --git a/plugins/checker/rpc.go b/plugins/checker/rpc.go deleted file mode 100644 index d03d1638..00000000 --- a/plugins/checker/rpc.go +++ /dev/null @@ -1,28 +0,0 @@ -package checker - -import ( - "github.com/spiral/errors" - "github.com/spiral/roadrunner/v2/interfaces/log" - "github.com/spiral/roadrunner/v2/interfaces/status" -) - -type rpc struct { - srv *Plugin - log log.Logger -} - -// Status return current status of the provided plugin -func (rpc *rpc) Status(service string, status *status.Status) error { - const op = errors.Op("status") - rpc.log.Debug("started Status method", "service", service) - st, err := rpc.srv.Status(service) - if err != nil { - return errors.E(op, err) - } - - *status = st - - rpc.log.Debug("status code", "code", st.Code) - rpc.log.Debug("successfully finished Status method") - return nil -} diff --git a/plugins/checker/tests/configs/.rr-checker-init.yaml b/plugins/checker/tests/configs/.rr-checker-init.yaml deleted file mode 100755 index 0aba90c5..00000000 --- a/plugins/checker/tests/configs/.rr-checker-init.yaml +++ /dev/null @@ -1,31 +0,0 @@ -rpc: - listen: tcp://127.0.0.1:6005 - disabled: false - -server: - command: "php ../../../tests/http/client.php echo pipes" - user: "" - group: "" - env: - "RR_HTTP": "true" - relay: "pipes" - relayTimeout: "20s" - -status: - address: "127.0.0.1:34333" -logs: - mode: development - level: error -http: - debug: true - address: 127.0.0.1:11933 - maxRequestSize: 1024 - middleware: [ "" ] - uploads: - forbid: [ ".php", ".exe", ".bat" ] - trustedSubnets: [ "10.0.0.0/8", "127.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "::1/128", "fc00::/7", "fe80::/10" ] - pool: - numWorkers: 2 - maxJobs: 0 - allocateTimeout: 60s - destroyTimeout: 60s
\ No newline at end of file diff --git a/plugins/checker/tests/plugin_test.go b/plugins/checker/tests/plugin_test.go deleted file mode 100644 index 38b751ff..00000000 --- a/plugins/checker/tests/plugin_test.go +++ /dev/null @@ -1,188 +0,0 @@ -package tests - -import ( - "io/ioutil" - "net" - "net/http" - "net/rpc" - "os" - "os/signal" - "sync" - "syscall" - "testing" - "time" - - "github.com/spiral/endure" - goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" - "github.com/spiral/roadrunner/v2/interfaces/status" - "github.com/spiral/roadrunner/v2/plugins/checker" - "github.com/spiral/roadrunner/v2/plugins/config" - httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" - "github.com/spiral/roadrunner/v2/plugins/logger" - rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" - "github.com/spiral/roadrunner/v2/plugins/server" - "github.com/stretchr/testify/assert" -) - -func TestStatusHttp(t *testing.T) { - cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) - assert.NoError(t, err) - - cfg := &config.Viper{ - Path: "configs/.rr-checker-init.yaml", - Prefix: "rr", - } - - err = cont.RegisterAll( - cfg, - &logger.ZapLogger{}, - &server.Plugin{}, - &httpPlugin.Plugin{}, - &checker.Plugin{}, - ) - assert.NoError(t, err) - - err = cont.Init() - if err != nil { - t.Fatal(err) - } - - ch, err := cont.Serve() - assert.NoError(t, err) - - sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - - wg := &sync.WaitGroup{} - wg.Add(1) - - tt := time.NewTimer(time.Second * 10) - - go func() { - defer wg.Done() - for { - select { - case e := <-ch: - assert.Fail(t, "error", e.Error.Error()) - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - case <-sig: - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - return - case <-tt.C: - // timeout - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - return - } - } - }() - - time.Sleep(time.Second) - t.Run("CheckerGetStatus", checkHTTPStatus) - wg.Wait() -} - -const resp = `Service: http: Status: 200 -Service: rpc not found` - -func checkHTTPStatus(t *testing.T) { - req, err := http.NewRequest("GET", "http://127.0.0.1:34333/v1/health?plugin=http&plugin=rpc", nil) - assert.NoError(t, err) - - r, err := http.DefaultClient.Do(req) - assert.NoError(t, err) - b, err := ioutil.ReadAll(r.Body) - assert.NoError(t, err) - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, resp, string(b)) - - err = r.Body.Close() - assert.NoError(t, err) -} - -func TestStatusRPC(t *testing.T) { - cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) - assert.NoError(t, err) - - cfg := &config.Viper{ - Path: "configs/.rr-checker-init.yaml", - Prefix: "rr", - } - - err = cont.RegisterAll( - cfg, - &rpcPlugin.Plugin{}, - &logger.ZapLogger{}, - &server.Plugin{}, - &httpPlugin.Plugin{}, - &checker.Plugin{}, - ) - assert.NoError(t, err) - - err = cont.Init() - if err != nil { - t.Fatal(err) - } - - ch, err := cont.Serve() - assert.NoError(t, err) - - sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) - - wg := &sync.WaitGroup{} - wg.Add(1) - - tt := time.NewTimer(time.Second * 10) - - go func() { - defer wg.Done() - for { - select { - case e := <-ch: - assert.Fail(t, "error", e.Error.Error()) - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - case <-sig: - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - return - case <-tt.C: - // timeout - err = cont.Stop() - if err != nil { - assert.FailNow(t, "error", err.Error()) - } - return - } - } - }() - - time.Sleep(time.Second) - t.Run("CheckerGetStatusRpc", checkRPCStatus) - wg.Wait() -} - -func checkRPCStatus(t *testing.T) { - conn, err := net.Dial("tcp", "127.0.0.1:6005") - assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) - - st := &status.Status{} - - err = client.Call("status.Status", "http", &st) - assert.NoError(t, err) - assert.Equal(t, st.Code, 200) -} |