forked from mattn/goreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
209 lines (197 loc) · 3.88 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"errors"
"fmt"
"net"
"net/rpc"
"sync"
"time"
)
// Goreman is RPC server
type Goreman struct {
rpcChan chan<- *rpcMessage
}
type rpcMessage struct {
Msg string
Args []string
// sending error (if any) when the task completes
ErrCh chan error
}
// Start do start
func (r *Goreman) Start(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
for _, arg := range args {
if err = startProc(arg, nil, nil); err != nil {
break
}
}
return err
}
// Stop do stop
func (r *Goreman) Stop(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
errChan := make(chan error, 1)
r.rpcChan <- &rpcMessage{
Msg: "stop",
Args: args,
ErrCh: errChan,
}
err = <-errChan
return
}
// StopAll do stop all
func (r *Goreman) StopAll(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
for proc := range procs {
if err = stopProc(proc, nil); err != nil {
break
}
}
return err
}
// Restart do restart
func (r *Goreman) Restart(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
for _, arg := range args {
if err = restartProc(arg); err != nil {
break
}
}
return err
}
// RestartAll do restart all
func (r *Goreman) RestartAll(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
for proc := range procs {
if err = restartProc(proc); err != nil {
break
}
}
return err
}
// List do list
func (r *Goreman) List(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
*ret = ""
for proc := range procs {
*ret += proc + "\n"
}
return err
}
// Status do status
func (r *Goreman) Status(args []string, ret *string) (err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
*ret = ""
for proc := range procs {
if procs[proc].cmd != nil {
*ret += "*" + proc + "\n"
} else {
*ret += " " + proc + "\n"
}
}
return err
}
// command: run.
func run(cmd string, args []string, serverPort uint) error {
client, err := rpc.Dial("tcp", defaultServer(serverPort))
if err != nil {
return err
}
defer client.Close()
var ret string
switch cmd {
case "start":
return client.Call("Goreman.Start", args, &ret)
case "stop":
return client.Call("Goreman.Stop", args, &ret)
case "stop-all":
return client.Call("Goreman.StopAll", args, &ret)
case "restart":
return client.Call("Goreman.Restart", args, &ret)
case "restart-all":
return client.Call("Goreman.RestartAll", args, &ret)
case "list":
err := client.Call("Goreman.List", args, &ret)
fmt.Print(ret)
return err
case "status":
err := client.Call("Goreman.Status", args, &ret)
fmt.Print(ret)
return err
}
return errors.New("unknown command")
}
// start rpc server.
func startServer(ctx context.Context, rpcChan chan<- *rpcMessage, listenPort uint) error {
gm := &Goreman{
rpcChan: rpcChan,
}
rpc.Register(gm)
server, err := net.Listen("tcp", fmt.Sprintf("%s:%d", defaultAddr(), listenPort))
if err != nil {
return err
}
var wg sync.WaitGroup
var acceptingConns = true
for acceptingConns {
conns := make(chan net.Conn, 1)
go func() {
conn, err := server.Accept()
if err != nil {
return
}
conns <- conn
}()
select {
case <-ctx.Done():
acceptingConns = false
break
case client := <-conns: // server is not canceled.
wg.Add(1)
go func() {
defer wg.Done()
rpc.ServeConn(client)
}()
}
}
done := make(chan struct{}, 1)
go func() {
wg.Wait()
done <- struct{}{}
}()
select {
case <-done:
return nil
case <-time.After(10 * time.Second):
return errors.New("RPC server did not shut down in 10 seconds, quitting")
}
}