forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
73 lines (64 loc) · 1.37 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
package main
import (
"errors"
"net"
"net/http"
"github.com/flynn/flynn/host/types"
"github.com/flynn/flynn/pkg/rpcplus"
rpc "github.com/flynn/flynn/pkg/rpcplus/comborpc"
)
func serveHTTP(host *Host, attach *attachHandler, sh *shutdownHandler) error {
if err := rpc.Register(host); err != nil {
return err
}
rpc.HandleHTTP()
http.Handle("/attach", attach)
l, err := net.Listen("tcp", ":1113")
if err != nil {
return err
}
sh.BeforeExit(func() { l.Close() })
go http.Serve(l, nil)
return nil
}
type Host struct {
state *State
backend Backend
}
func (h *Host) ListJobs(arg struct{}, res *map[string]host.ActiveJob) error {
*res = h.state.Get()
return nil
}
func (h *Host) GetJob(id string, res *host.ActiveJob) error {
job := h.state.GetJob(id)
if job != nil {
*res = *job
}
return nil
}
func (h *Host) StopJob(id string, res *struct{}) error {
job := h.state.GetJob(id)
if job == nil {
return errors.New("host: unknown job")
}
if job.Status != host.StatusRunning {
return errors.New("host: job is not running")
}
return h.backend.Stop(id)
}
func (h *Host) StreamEvents(id string, stream rpcplus.Stream) error {
ch := h.state.AddListener(id)
defer h.state.RemoveListener(id, ch)
for {
select {
case event := <-ch:
select {
case stream.Send <- event:
case <-stream.Error:
return nil
}
case <-stream.Error:
return nil
}
}
}