summaryrefslogtreecommitdiff
path: root/plugins/informer/rpc.go
blob: 98b5681c8bdd8d0960c1fed097fe97f4adf293f7 (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
package informer

import (
	"github.com/spiral/roadrunner/v2/interfaces/worker"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"github.com/spiral/roadrunner/v2/tools"
)

type rpc struct {
	srv *Plugin
	log logger.Logger
}

// WorkerList contains list of workers.
type WorkerList struct {
	// Workers is list of workers.
	Workers []tools.ProcessState `json:"workers"`
}

// List all resettable services.
func (rpc *rpc) List(_ bool, list *[]string) error {
	rpc.log.Debug("Started List method")
	*list = make([]string, 0, len(rpc.srv.registry))

	for name := range rpc.srv.registry {
		*list = append(*list, name)
	}
	rpc.log.Debug("list of services", "list", *list)

	rpc.log.Debug("successfully finished List method")
	return nil
}

// Workers state of a given service.
func (rpc *rpc) Workers(service string, list *WorkerList) error {
	rpc.log.Debug("started Workers method", "service", service)
	workers, err := rpc.srv.Workers(service)
	if err != nil {
		return err
	}

	list.Workers = make([]tools.ProcessState, 0)
	for _, w := range workers {
		ps, err := tools.WorkerProcessState(w.(worker.BaseProcess))
		if err != nil {
			continue
		}

		list.Workers = append(list.Workers, ps)
	}
	rpc.log.Debug("list of workers", "workers", list.Workers)
	rpc.log.Debug("successfully finished Workers method")
	return nil
}