summaryrefslogtreecommitdiff
path: root/plugins/checker
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-02-23 19:00:44 +0300
committerValery Piashchynski <[email protected]>2021-02-23 19:00:44 +0300
commitfdbf6a61600745b5cb1022117dcd9b06d392dc84 (patch)
treec6b7711f4dfa9873415ff0525887917cc521fd7e /plugins/checker
parent7dc61c309a34d21630bc1ad53c53211b3dc985de (diff)
- Remove unneeded mutex from the `http.Workers` method.
- Rename `checker` plugin package to `status`, remove `/v1` endpoint prefix (#557) - Add static, headers, status, gzip plugins to the `main.go`. Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'plugins/checker')
-rw-r--r--plugins/checker/config.go5
-rw-r--r--plugins/checker/interface.go11
-rw-r--r--plugins/checker/plugin.go150
-rw-r--r--plugins/checker/rpc.go27
4 files changed, 0 insertions, 193 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/interface.go b/plugins/checker/interface.go
deleted file mode 100644
index dd9dcada..00000000
--- a/plugins/checker/interface.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package checker
-
-// Status consists of status code from the service
-type Status struct {
- Code int
-}
-
-// Checker interface used to get latest status from plugin
-type Checker interface {
- Status() Status
-}
diff --git a/plugins/checker/plugin.go b/plugins/checker/plugin.go
deleted file mode 100644
index 59a37613..00000000
--- a/plugins/checker/plugin.go
+++ /dev/null
@@ -1,150 +0,0 @@
-package checker
-
-import (
- "fmt"
- "net/http"
- "time"
-
- "github.com/gofiber/fiber/v2"
- fiberLogger "github.com/gofiber/fiber/v2/middleware/logger"
- endure "github.com/spiral/endure/pkg/container"
- "github.com/spiral/errors"
- "github.com/spiral/roadrunner/v2/plugins/config"
- "github.com/spiral/roadrunner/v2/plugins/logger"
-)
-
-const (
- // PluginName declares public plugin name.
- PluginName = "status"
-)
-
-type Plugin struct {
- registry map[string]Checker
- server *fiber.App
- log logger.Logger
- cfg *Config
-}
-
-func (c *Plugin) Init(log logger.Logger, cfg config.Configurer) error {
- const op = errors.Op("checker_plugin_init")
- if !cfg.Has(PluginName) {
- return errors.E(op, errors.Disabled)
- }
- err := cfg.UnmarshalKey(PluginName, &c.cfg)
- if err != nil {
- return errors.E(op, errors.Disabled, err)
- }
-
- c.registry = make(map[string]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(fiberLogger.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_plugin_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, error) {
- const op = errors.Op("checker_plugin_status")
- svc, ok := c.registry[name]
- if !ok {
- return 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 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("checker_plugin_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 a965dcd4..00000000
--- a/plugins/checker/rpc.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package checker
-
-import (
- "github.com/spiral/errors"
- "github.com/spiral/roadrunner/v2/plugins/logger"
-)
-
-type rpc struct {
- srv *Plugin
- log logger.Logger
-}
-
-// Status return current status of the provided plugin
-func (rpc *rpc) Status(service string, status *Status) error {
- const op = errors.Op("checker_rpc_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
-}