summaryrefslogtreecommitdiff
path: root/service/http/rpc.go
blob: aebc590369281e4a6b2424fb08a9547644798b10 (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
package http

import (
	"github.com/pkg/errors"
)

type rpcServer struct{ svc *Service }

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

// Worker provides information about specific worker.
type Worker struct {
	// Pid contains process id.
	Pid int `json:"pid"`

	// Status of the worker.
	Status string `json:"status"`

	// Number of worker executions.
	NumJobs int64 `json:"numExecs"`

	// Created is unix nano timestamp of worker creation time.
	Created int64 `json:"created"`
}

// Reset resets underlying RR worker pool and restarts all of it's workers.
func (rpc *rpcServer) Reset(reset bool, r *string) error {
	if rpc.svc.srv == nil {
		return errors.New("http server is not running")
	}

	*r = "OK"
	return rpc.svc.rr.Reset()
}

// Workers returns list of active workers and their stats.
func (rpc *rpcServer) Workers(list bool, r *WorkerList) error {
	if rpc.svc.srv == nil {
		return errors.New("http server is not running")
	}

	for _, w := range rpc.svc.rr.Workers() {
		state := w.State()
		r.Workers = append(r.Workers, Worker{
			Pid:     *w.Pid,
			Status:  state.String(),
			NumJobs: state.NumExecs(),
			Created: w.Created.UnixNano(),
		})
	}

	return nil
}