summaryrefslogtreecommitdiff
path: root/internal/cli/workers/command.go
blob: 8ea696008693d0bded0b0fbb49cdcf2ff8c77137 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package workers

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

	"github.com/roadrunner-server/api/v4/plugins/v1/jobs"
	internalRpc "github.com/roadrunner-server/roadrunner/v2024/internal/rpc"

	tm "github.com/buger/goterm"
	"github.com/fatih/color"
	"github.com/roadrunner-server/errors"
	"github.com/roadrunner-server/informer/v5"
	"github.com/spf13/cobra"
)

// NewCommand creates `workers` command.
func NewCommand(cfgFile *string, override *[]string) *cobra.Command { //nolint:funlen
	// interactive workers updates
	var interactive bool

	cmd := &cobra.Command{
		Use:   "workers",
		Short: "Show information about active RoadRunner workers",
		RunE: func(_ *cobra.Command, args []string) error {
			const (
				op           = errors.Op("handle_workers_command")
				informerList = "informer.List"
			)

			if cfgFile == nil {
				return errors.E(op, errors.Str("no configuration file provided"))
			}

			client, err := internalRpc.NewClient(*cfgFile, *override)
			if err != nil {
				return err
			}

			defer func() { _ = client.Close() }()

			plugins := args        // by default, we expect a plugin list from user
			if len(plugins) == 0 { // but if nothing was passed - request all informers list
				if err = client.Call(informerList, true, &plugins); err != nil {
					return err
				}
			}

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

			oss := make(chan os.Signal, 1)
			signal.Notify(oss, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

			tm.Clear()

			tt := time.NewTicker(time.Second)
			defer tt.Stop()

			for {
				select {
				case <-oss:
					return nil

				case <-tt.C:
					tm.MoveCursor(1, 1)
					tm.Flush()

					if err = showWorkers(plugins, client); err != nil {
						return errors.E(op, err)
					}
				}
			}
		},
	}

	cmd.Flags().BoolVarP(
		&interactive,
		"interactive",
		"i",
		false,
		"render interactive workers table",
	)

	return cmd
}

func showWorkers(plugins []string, client *rpc.Client) error {
	const (
		op              = errors.Op("show_workers")
		informerWorkers = "informer.Workers"
		informerJobs    = "informer.Jobs"
		// this is only one exception to Render the workers, service plugin has the same workers as other plugins,
		// but they are RAW processes and needs to be handled in a different way. We don't need a special RPC call, but
		// need a special render method.
		servicePluginName = "service"
	)

	for _, plugin := range plugins {
		list := &informer.WorkerList{}

		if err := client.Call(informerWorkers, plugin, &list); err != nil {
			return errors.E(op, err)
		}

		if len(list.Workers) == 0 {
			continue
		}

		if plugin == servicePluginName {
			fmt.Printf("Workers of [%s]:\n", color.HiYellowString(plugin))
			ServiceWorkerTable(os.Stdout, list.Workers).Render()

			continue
		}

		fmt.Printf("Workers of [%s]:\n", color.HiYellowString(plugin))

		WorkerTable(os.Stdout, list.Workers).Render()
	}

	for _, plugin := range plugins {
		var jst []*jobs.State

		if err := client.Call(informerJobs, plugin, &jst); err != nil {
			return errors.E(op, err)
		}

		// eq to nil
		if len(jst) == 0 {
			continue
		}

		fmt.Printf("Jobs of [%s]:\n", color.HiYellowString(plugin))
		JobsTable(os.Stdout, jst).Render()
	}

	return nil
}