summaryrefslogtreecommitdiff
path: root/cmd/cli/workers.go
blob: e9c8ab2cf49687d346ddf606b711c27ce8a03cc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package cli

import (
	"fmt"
	"net/rpc"
	"os"
	"os/signal"
	"syscall"
	"time"

	tm "github.com/buger/goterm"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/tools"
	"go.uber.org/zap"

	"github.com/fatih/color"
	"github.com/spf13/cobra"
	"github.com/spiral/roadrunner-plugins/informer"
)

var (
	interactive bool
	stopSignal  = make(chan os.Signal, 1)
)

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)

	signal.Notify(stopSignal, syscall.SIGTERM)
	signal.Notify(stopSignal, syscall.SIGINT)
}

func workersHandler(cmd *cobra.Command, args []string) error {
	const op = errors.Op("workers handler")
	client, err := RPCClient()
	if err != nil {
		return err
	}
	defer func() {
		err := client.Close()
		if err != nil {
			Logger.Error("error when closing RPCClient", zap.Error(err))
		}
	}()

	var plugins []string
	if len(args) != 0 {
		plugins = args
	} else {
		err = client.Call("informer.List", true, &plugins)
		if err != nil {
			return err
		}
	}

	if !interactive {
		return showWorkers(plugins, client)
	}

	tm.Clear()
	for {
		select {
		case <-stopSignal:
			return nil
		case <-time.NewTicker(time.Second).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 {
	for _, plugin := range plugins {
		list := &informer.WorkerList{}
		err := client.Call("informer.Workers", plugin, &list)
		if err != nil {
			return err
		}

		// it's a golang :)
		ps := make([]tools.ProcessState, len(list.Workers))
		for i := 0; i < len(list.Workers); i++ {
			ps[i].Created = list.Workers[i].Created
			ps[i].NumJobs = list.Workers[i].NumJobs
			ps[i].MemoryUsage = list.Workers[i].MemoryUsage
			ps[i].Pid = list.Workers[i].Pid
			ps[i].Status = list.Workers[i].Status
		}

		fmt.Printf("Workers of [%s]:\n", color.HiYellowString(plugin))
		tools.WorkerTable(os.Stdout, ps).Render()
	}
	return nil
}