summaryrefslogtreecommitdiff
path: root/internal/cli/workers/command.go
blob: 3c34740efdabae6927779327cb23f67b880a2556 (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
package workers

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

	"github.com/roadrunner-server/api/v4/plugins/v4/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 fmt.Errorf("failed to get list of plugins: %w", err)
				}
			}

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

			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()

					showWorkers(plugins, client)
				}
			}
		},
	}

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

	return cmd
}

func showWorkers(plugins []string, client *rpc.Client) {
	const (
		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 {
			// this is a special case, when we can't get workers list, we need to render an error message
			WorkerTable(os.Stdout, list.Workers, fmt.Errorf("failed to receive information about %s plugin: %w", plugin, err)).Render()
			continue
		}

		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, nil).Render()
	}

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

		if err := client.Call(informerJobs, plugin, &jst); err != nil {
			JobsTable(os.Stdout, jst, fmt.Errorf("failed to receive information about %s plugin: %w", plugin, err)).Render()
			continue
		}

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

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