diff options
Diffstat (limited to 'cmd/cli')
-rw-r--r-- | cmd/cli/reset.go | 107 | ||||
-rw-r--r-- | cmd/cli/root.go | 129 | ||||
-rw-r--r-- | cmd/cli/serve.go | 63 | ||||
-rw-r--r-- | cmd/cli/version.go | 9 | ||||
-rw-r--r-- | cmd/cli/workers.go | 109 |
5 files changed, 0 insertions, 417 deletions
diff --git a/cmd/cli/reset.go b/cmd/cli/reset.go deleted file mode 100644 index a5055a53..00000000 --- a/cmd/cli/reset.go +++ /dev/null @@ -1,107 +0,0 @@ -package cli - -import ( - "fmt" - "sync" - - "github.com/fatih/color" - "github.com/mattn/go-runewidth" - "github.com/spf13/cobra" - "github.com/spiral/errors" - "github.com/vbauerster/mpb/v5" - "github.com/vbauerster/mpb/v5/decor" -) - -// List is the resetter.List RPC method -const List string = "resetter.List" - -// Reset is the resetter.Reset RPC method -const Reset string = "resetter.Reset" - -func init() { - root.AddCommand(&cobra.Command{ - Use: "reset", - Short: "Reset workers of all or specific RoadRunner service", - RunE: resetHandler, - }) -} - -func resetHandler(_ *cobra.Command, args []string) error { - const op = errors.Op("reset_handler") - client, err := RPCClient() - if err != nil { - return err - } - defer func() { - _ = client.Close() - }() - - var services []string - if len(args) != 0 { - services = args - } else { - err = client.Call(List, true, &services) - if err != nil { - return errors.E(op, err) - } - } - - var wg sync.WaitGroup - pr := mpb.New(mpb.WithWaitGroup(&wg), mpb.WithWidth(6)) - wg.Add(len(services)) - - for _, service := range services { - var ( - bar *mpb.Bar - name = runewidth.FillRight(fmt.Sprintf("Resetting plugin: [%s]", color.HiYellowString(service)), 27) - result = make(chan interface{}) - ) - - bar = pr.AddSpinner( - 1, - mpb.SpinnerOnMiddle, - mpb.SpinnerStyle([]string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙∙∙"}), - mpb.PrependDecorators(decor.Name(name)), - mpb.AppendDecorators(onComplete(result)), - ) - - // simulating some work - go func(service string, result chan interface{}) { - defer wg.Done() - defer bar.Increment() - - var done bool - err = client.Call(Reset, service, &done) - if err != nil { - result <- errors.E(op, err) - return - } - result <- nil - }(service, result) - } - - pr.Wait() - return nil -} - -func onComplete(result chan interface{}) decor.Decorator { - var ( - msg = "" - fn = func(s decor.Statistics) string { - select { - case r := <-result: - if err, ok := r.(error); ok { - msg = color.HiRedString(err.Error()) - return msg - } - - msg = color.HiGreenString("done") - return msg - default: - return msg - } - } - ) - - return decor.Any(fn) -} diff --git a/cmd/cli/root.go b/cmd/cli/root.go deleted file mode 100644 index 6f73aecf..00000000 --- a/cmd/cli/root.go +++ /dev/null @@ -1,129 +0,0 @@ -package cli - -import ( - "log" - "net/http/pprof" - "net/rpc" - "os" - "path/filepath" - - "github.com/spiral/errors" - goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" - rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" - - "github.com/spiral/roadrunner/v2/plugins/config" - - "net/http" - - "github.com/spf13/cobra" - endure "github.com/spiral/endure/pkg/container" -) - -var ( - // WorkDir is working directory - WorkDir string - // CfgFile is path to the .rr.yaml - CfgFile string - // Debug mode - Debug bool - // Container is the pointer to the Endure container - Container *endure.Endure - cfg *config.Viper - root = &cobra.Command{ - Use: "rr", - SilenceErrors: true, - SilenceUsage: true, - Version: Version, - } -) - -func Execute() { - if err := root.Execute(); err != nil { - // exit with error, fatal invoke os.Exit(1) - log.Fatal(err) - } -} - -func init() { - root.PersistentFlags().StringVarP(&CfgFile, "config", "c", ".rr.yaml", "config file (default is .rr.yaml)") - root.PersistentFlags().StringVarP(&WorkDir, "WorkDir", "w", "", "work directory") - root.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "debug mode") - cobra.OnInitialize(func() { - if CfgFile != "" { - if absPath, err := filepath.Abs(CfgFile); err == nil { - CfgFile = absPath - - // force working absPath related to config file - if err := os.Chdir(filepath.Dir(absPath)); err != nil { - panic(err) - } - } - } - - if WorkDir != "" { - if err := os.Chdir(WorkDir); err != nil { - panic(err) - } - } - - cfg = &config.Viper{} - cfg.Path = CfgFile - cfg.Prefix = "rr" - - // register config - err := Container.Register(cfg) - if err != nil { - panic(err) - } - - // if debug mode is on - run debug server - if Debug { - runDebugServer() - } - }) -} - -// RPCClient is using to make a requests to the ./rr reset, ./rr workers -func RPCClient() (*rpc.Client, error) { - rpcConfig := &rpcPlugin.Config{} - - err := cfg.Init() - if err != nil { - return nil, err - } - - if !cfg.Has(rpcPlugin.PluginName) { - return nil, errors.E("rpc service disabled") - } - - err = cfg.UnmarshalKey(rpcPlugin.PluginName, rpcConfig) - if err != nil { - return nil, err - } - rpcConfig.InitDefaults() - - conn, err := rpcConfig.Dialer() - if err != nil { - return nil, err - } - - return rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)), nil -} - -// debug server -func runDebugServer() { - mux := http.NewServeMux() - mux.HandleFunc("/debug/pprof/", pprof.Index) - mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) - mux.HandleFunc("/debug/pprof/profile", pprof.Profile) - mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) - mux.HandleFunc("/debug/pprof/trace", pprof.Trace) - srv := http.Server{ - Addr: ":6061", - Handler: mux, - } - - if err := srv.ListenAndServe(); err != nil { - log.Fatal(err) - } -} diff --git a/cmd/cli/serve.go b/cmd/cli/serve.go deleted file mode 100644 index 993ec477..00000000 --- a/cmd/cli/serve.go +++ /dev/null @@ -1,63 +0,0 @@ -package cli - -import ( - "log" - "os" - "os/signal" - "syscall" - - "github.com/spf13/cobra" - "github.com/spiral/errors" - "go.uber.org/multierr" -) - -func init() { - root.AddCommand(&cobra.Command{ - Use: "serve", - Short: "Start RoadRunner server", - RunE: handler, - }) -} - -func handler(_ *cobra.Command, _ []string) error { - const op = errors.Op("handle_serve_command") - /* - We need to have path to the config at the RegisterTarget stage - But after cobra.Execute, because cobra fills up cli variables on this stage - */ - - err := Container.Init() - if err != nil { - return errors.E(op, err) - } - - errCh, err := Container.Serve() - if err != nil { - return errors.E(op, err) - } - - // https://golang.org/pkg/os/signal/#Notify - // should be of buffer size at least 1 - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) - - for { - select { - case e := <-errCh: - err = multierr.Append(err, e.Error) - log.Printf("error occurred: %v, service: %s", e.Error.Error(), e.VertexID) - er := Container.Stop() - if er != nil { - err = multierr.Append(err, er) - return errors.E(op, err) - } - return errors.E(op, err) - case <-c: - err = Container.Stop() - if err != nil { - return errors.E(op, err) - } - return nil - } - } -} diff --git a/cmd/cli/version.go b/cmd/cli/version.go deleted file mode 100644 index 89728bd2..00000000 --- a/cmd/cli/version.go +++ /dev/null @@ -1,9 +0,0 @@ -package cli - -var ( - // Version - defines build version. - Version string = "local" - - // BuildTime - defined build time. - BuildTime string = "development" -) diff --git a/cmd/cli/workers.go b/cmd/cli/workers.go deleted file mode 100644 index 09642a58..00000000 --- a/cmd/cli/workers.go +++ /dev/null @@ -1,109 +0,0 @@ -package cli - -import ( - "fmt" - "log" - "net/rpc" - "os" - "os/signal" - "syscall" - "time" - - tm "github.com/buger/goterm" - "github.com/fatih/color" - "github.com/spf13/cobra" - "github.com/spiral/errors" - "github.com/spiral/roadrunner/v2/plugins/informer" - "github.com/spiral/roadrunner/v2/tools" -) - -// use interactive mode -var interactive bool - -const InformerList string = "informer.List" -const InformerWorkers string = "informer.Workers" - -func init() { - workersCommand := &cobra.Command{ - Use: "workers", - Short: "Show information about active roadrunner workers", - RunE: workersHandler, - } - - workersCommand.Flags().BoolVarP( - &interactive, - "interactive", - "i", - false, - "render interactive workers table", - ) - - root.AddCommand(workersCommand) -} - -func workersHandler(_ *cobra.Command, args []string) error { - const op = errors.Op("handle_workers_command") - // get RPC client - client, err := RPCClient() - if err != nil { - return err - } - defer func() { - err := client.Close() - if err != nil { - log.Printf("error when closing RPCClient: error %v", err) - } - }() - - var plugins []string - // assume user wants to show workers from particular plugin - if len(args) != 0 { - plugins = args - } else { - err = client.Call(InformerList, true, &plugins) - if err != nil { - return errors.E(op, err) - } - } - - if !interactive { - return showWorkers(plugins, client) - } - - // https://golang.org/pkg/os/signal/#Notify - // should be of buffer size at least 1 - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) - - tm.Clear() - tt := time.NewTicker(time.Second) - defer tt.Stop() - for { - select { - case <-c: - return nil - case <-tt.C: - tm.MoveCursor(1, 1) - err := showWorkers(plugins, client) - if err != nil { - return errors.E(op, err) - } - tm.Flush() - } - } -} - -func showWorkers(plugins []string, client *rpc.Client) error { - const op = errors.Op("show_workers") - for _, plugin := range plugins { - list := &informer.WorkerList{} - err := client.Call(InformerWorkers, plugin, &list) - if err != nil { - return errors.E(op, err) - } - - fmt.Printf("Workers of [%s]:\n", color.HiYellowString(plugin)) - tools.WorkerTable(os.Stdout, list.Workers).Render() - } - return nil -} |