This repository has been archived by the owner on Jan 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrpc.go
151 lines (120 loc) · 3.24 KB
/
rpc.go
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
145
146
147
148
149
150
151
package jobs
import (
"fmt"
"github.com/spiral/roadrunner/util"
)
type rpcServer struct{ svc *Service }
// WorkerList contains list of workers.
type WorkerList struct {
// Workers is list of workers.
Workers []*util.State `json:"workers"`
}
// PipelineList contains list of pipeline stats.
type PipelineList struct {
// Pipelines is list of pipeline stats.
Pipelines []*Stat `json:"pipelines"`
}
// Push job to the testQueue.
func (rpc *rpcServer) Push(j *Job, id *string) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
*id, err = rpc.svc.Push(j)
return
}
// Push job to the testQueue.
func (rpc *rpcServer) PushAsync(j *Job, ok *bool) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
*ok = true
go rpc.svc.Push(j)
return
}
// Reset resets underlying RR worker pool and restarts all of it's workers.
func (rpc *rpcServer) Reset(reset bool, w *string) error {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
*w = "OK"
return rpc.svc.rr.Reset()
}
// Destroy job pipelines for a given pipeline.
func (rpc *rpcServer) Stop(pipeline string, w *string) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
pipe := rpc.svc.cfg.pipelines.Get(pipeline)
if pipe == nil {
return fmt.Errorf("undefined pipeline `%s`", pipeline)
}
if err := rpc.svc.Consume(pipe, nil, nil); err != nil {
return err
}
*w = "OK"
return nil
}
// Resume job pipelines for a given pipeline.
func (rpc *rpcServer) Resume(pipeline string, w *string) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
pipe := rpc.svc.cfg.pipelines.Get(pipeline)
if pipe == nil {
return fmt.Errorf("undefined pipeline `%s`", pipeline)
}
if err := rpc.svc.Consume(pipe, rpc.svc.execPool, rpc.svc.error); err != nil {
return err
}
*w = "OK"
return nil
}
// Destroy job pipelines for a given pipeline.
func (rpc *rpcServer) StopAll(stop bool, w *string) (err error) {
if rpc.svc == nil || rpc.svc.rr == nil {
return fmt.Errorf("jobs server is not running")
}
for _, pipe := range rpc.svc.cfg.pipelines {
if err := rpc.svc.Consume(pipe, nil, nil); err != nil {
return err
}
}
*w = "OK"
return nil
}
// Resume job pipelines for a given pipeline.
func (rpc *rpcServer) ResumeAll(resume bool, w *string) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
for _, pipe := range rpc.svc.cfg.pipelines {
if err := rpc.svc.Consume(pipe, rpc.svc.execPool, rpc.svc.error); err != nil {
return err
}
}
*w = "OK"
return nil
}
// Workers returns list of pipelines workers and their stats.
func (rpc *rpcServer) Workers(list bool, w *WorkerList) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
w.Workers, err = util.ServerState(rpc.svc.rr)
return err
}
// Stat returns list of pipelines workers and their stats.
func (rpc *rpcServer) Stat(list bool, l *PipelineList) (err error) {
if rpc.svc == nil {
return fmt.Errorf("jobs server is not running")
}
*l = PipelineList{}
for _, p := range rpc.svc.cfg.pipelines {
stat, err := rpc.svc.Stat(p)
if err != nil {
return err
}
l.Pipelines = append(l.Pipelines, stat)
}
return err
}