diff options
author | Wolfy-J <[email protected]> | 2018-06-05 16:23:14 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-05 16:23:14 +0300 |
commit | 76ff8d1c95e087749d559ee5a4f8f0348feafffa (patch) | |
tree | 112630d2d2cfe41d809065034c13b1066b8e05c2 /cmd/rr | |
parent | 3c86132f90ef6473b4073a8b1500d01b6114fc30 (diff) |
Cs and refactoring
Diffstat (limited to 'cmd/rr')
-rw-r--r-- | cmd/rr/.rr.yaml | 3 | ||||
-rw-r--r-- | cmd/rr/cmd/root.go | 28 | ||||
-rw-r--r-- | cmd/rr/http/register.go | 23 | ||||
-rw-r--r-- | cmd/rr/http/reload.go | 28 | ||||
-rw-r--r-- | cmd/rr/http/workers.go | 59 | ||||
-rw-r--r-- | cmd/rr/main.go | 10 | ||||
-rw-r--r-- | cmd/rr/utils/config.go | 11 | ||||
-rw-r--r-- | cmd/rr/utils/verbose.go | 18 |
8 files changed, 88 insertions, 92 deletions
diff --git a/cmd/rr/.rr.yaml b/cmd/rr/.rr.yaml index 2717e187..6e3b689b 100644 --- a/cmd/rr/.rr.yaml +++ b/cmd/rr/.rr.yaml @@ -1,5 +1,8 @@ # rpc bus allows php application and external clients to talk to rr services. rpc: + # enable rpc server + enable: true + # rpc connection DSN. Supported TCP and Unix sockets. listen: tcp://127.0.0.1:6001 diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index d0cac5ef..d54437f0 100644 --- a/cmd/rr/cmd/root.go +++ b/cmd/rr/cmd/root.go @@ -31,24 +31,29 @@ import ( // Service bus for all the commands. var ( - // Shared service bus. - Services = service.NewBus() + cfgFile string + verbose bool + + // Logger - shared logger. + Logger = logrus.New() + + // Services - shared service bus. + Services = service.NewRegistry(Logger) // CLI is application endpoint. CLI = &cobra.Command{ - Use: "rr", - Short: "RoadRunner, PHP application server", + Use: "rr", + SilenceErrors: true, + SilenceUsage: true, + Short: utils.Sprintf("<green>RoadRunner, PHP Application Server.</reset>"), } - - cfgFile string - verbose bool ) // 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 err := CLI.Execute(); err != nil { - logrus.Error(err) + utils.Printf("Error: <red>%s</reset>\n", err) os.Exit(1) } } @@ -59,7 +64,7 @@ func init() { cobra.OnInitialize(func() { if verbose { - logrus.SetLevel(logrus.DebugLevel) + Logger.SetLevel(logrus.DebugLevel) } if cfg := initConfig(cfgFile, []string{"."}, ".rr"); cfg != nil { @@ -81,6 +86,7 @@ func initConfig(cfgFile string, path []string, name string) service.Config { for _, p := range path { cfg.AddConfigPath(p) } + cfg.SetConfigName(name) } @@ -89,9 +95,9 @@ func initConfig(cfgFile string, path []string, name string) service.Config { // If a cfg file is found, read it in. if err := cfg.ReadInConfig(); err != nil { - logrus.Warnf("config: %s", err) + Logger.Warnf("config: %s", err) return nil } - return &utils.ConfigWrapper{cfg} + return &utils.ViperWrapper{Viper: cfg} } diff --git a/cmd/rr/http/register.go b/cmd/rr/http/register.go deleted file mode 100644 index fb828578..00000000 --- a/cmd/rr/http/register.go +++ /dev/null @@ -1,23 +0,0 @@ -package http - -import ( - "github.com/spf13/cobra" - rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/http" -) - -func init() { - rr.Services.Register(&http.Service{}) - - rr.CLI.AddCommand(&cobra.Command{ - Use: "http:reload", - Short: "Reload RoadRunner worker pools for the HTTP service", - Run: reloadHandler, - }) - - rr.CLI.AddCommand(&cobra.Command{ - Use: "http:workers", - Short: "List workers associated with RoadRunner HTTP service", - Run: workersHandler, - }) -} diff --git a/cmd/rr/http/reload.go b/cmd/rr/http/reload.go index 6cdba576..0fd3d7e9 100644 --- a/cmd/rr/http/reload.go +++ b/cmd/rr/http/reload.go @@ -23,22 +23,34 @@ package http import ( "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/sirupsen/logrus" + "github.com/go-errors/errors" + "github.com/spiral/roadrunner/rpc" ) -func reloadHandler(cmd *cobra.Command, args []string) { - client, err := rr.Services.RCPClient() +func init() { + rr.CLI.AddCommand(&cobra.Command{ + Use: "http:reload", + Short: "Reload RoadRunner worker pools for the HTTP service", + RunE: reloadHandler, + }) +} + +func reloadHandler(cmd *cobra.Command, args []string) error { + if !rr.Services.Has("rpc") { + return errors.New("RPC service is not configured") + } + + client, err := rr.Services.Get("rpc").(*rpc.Service).Client() if err != nil { - logrus.Error(err) - return + return err } defer client.Close() var r string if err := client.Call("http.Reset", true, &r); err != nil { - logrus.Error(err) - return + return err } - logrus.Info("restarting http worker pool") + rr.Logger.Info("http.service: restarting worker pool") + return nil } diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go index 13e8d21c..63ef0cce 100644 --- a/cmd/rr/http/workers.go +++ b/cmd/rr/http/workers.go @@ -21,38 +21,47 @@ package http import ( - "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/http" - "os" - "strconv" - "github.com/sirupsen/logrus" + "errors" + "github.com/spiral/roadrunner/rpc" ) -func workersHandler(cmd *cobra.Command, args []string) { - client, err := rr.Services.RCPClient() - if err != nil { - logrus.Error(err) - return - } - defer client.Close() +func init() { + rr.CLI.AddCommand(&cobra.Command{ + Use: "http:workers", + Short: "List workers associated with RoadRunner HTTP service", + RunE: workersHandler, + }) +} - var r http.WorkerList - if err := client.Call("http.Workers", true, &r); err != nil { - panic(err) +func workersHandler(cmd *cobra.Command, args []string) error { + if !rr.Services.Has("rpc") { + return errors.New("RPC service is not configured") } - tw := tablewriter.NewWriter(os.Stdout) - tw.SetHeader([]string{"PID", "Status", "Num Execs"}) - - for _, w := range r.Workers { - tw.Append([]string{ - strconv.Itoa(w.Pid), - w.Status, - strconv.Itoa(int(w.NumExecs)), - }) + client, err := rr.Services.Get("rpc").(*rpc.Service).Client() + if err != nil { + return err } + defer client.Close() - tw.Render() + //var r http.WorkerList + //if err := client.Call("http.Workers", true, &r); err != nil { + // panic(err) + //} + // + //tw := tablewriter.NewWriter(os.Stdout) + //tw.SetHeader([]string{"PID", "Status", "Num Execs"}) + // + //for _, w := range r.Workers { + // tw.Append([]string{ + // strconv.Itoa(w.Pid), + // w.Status, + // strconv.Itoa(int(w.NumExecs)), + // }) + //} + // + //tw.Render() + return nil } diff --git a/cmd/rr/main.go b/cmd/rr/main.go index 9d6f685c..26f70fdd 100644 --- a/cmd/rr/main.go +++ b/cmd/rr/main.go @@ -23,13 +23,17 @@ package main import ( - "github.com/spiral/roadrunner/cmd/rr/cmd" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/rpc" - // service plugins + // cli plugins _ "github.com/spiral/roadrunner/cmd/rr/http" ) func main() { + // provides ability to make local connection to services + rr.Services.Register("rpc", new(rpc.Service)) + // you can register additional commands using cmd.CLI - cmd.Execute() + rr.Execute() } diff --git a/cmd/rr/utils/config.go b/cmd/rr/utils/config.go index e7e22b3a..452dd195 100644 --- a/cmd/rr/utils/config.go +++ b/cmd/rr/utils/config.go @@ -5,19 +5,22 @@ import ( "github.com/spiral/roadrunner/service" ) -type ConfigWrapper struct { +// ViperWrapper provides interface bridge between Viper configs and service.Config. +type ViperWrapper struct { Viper *viper.Viper } -func (w *ConfigWrapper) Get(key string) service.Config { +// Get nested config section (sub-map), returns nil if section not found. +func (w *ViperWrapper) Get(key string) service.Config { sub := w.Viper.Sub(key) if sub == nil { return nil } - return &ConfigWrapper{sub} + return &ViperWrapper{sub} } -func (w *ConfigWrapper) Unmarshal(out interface{}) error { +// Unmarshal unmarshal config data into given struct. +func (w *ViperWrapper) Unmarshal(out interface{}) error { return w.Viper.Unmarshal(out) } diff --git a/cmd/rr/utils/verbose.go b/cmd/rr/utils/verbose.go deleted file mode 100644 index 43770f34..00000000 --- a/cmd/rr/utils/verbose.go +++ /dev/null @@ -1,18 +0,0 @@ -package utils - -//if f.Verbose { -// rr.Observe(func(event int, ctx interface{}) { -// switch event { -// case roadrunner.EventPoolError: -// logrus.Error(ctx) -// case roadrunner.EventWorkerCreate: -// logrus.Infof("%s - created", ctx) -// case roadrunner.EventWorkerError: -// logrus.Errorf("%s: %s", ctx.(roadrunner.WorkerError).Worker, ctx.(roadrunner.WorkerError).Error()) -// case roadrunner.EventWorkerDestruct: -// logrus.Warnf("%s - destructed", ctx) -// case roadrunner.EventWorkerKill: -// logrus.Warnf("%s - killed", ctx) -// } -// }) -//} |