forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_client.go
62 lines (52 loc) · 1.3 KB
/
local_client.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
package main
import (
"github.com/flynn/flynn/host/sampi"
"github.com/flynn/flynn/host/types"
"github.com/flynn/flynn/pkg/cluster"
"github.com/flynn/flynn/pkg/rpcplus"
)
func NewLocalClient(host string, c *sampi.Cluster) cluster.LocalClient {
return &localClient{c: c, host: host}
}
type localClient struct {
c *sampi.Cluster
host string
}
func (c *localClient) ListHosts() (map[string]host.Host, error) {
res := make(map[string]host.Host)
return res, c.c.ListHosts(struct{}{}, &res)
}
func (c *localClient) AddJobs(req *host.AddJobsReq) (*host.AddJobsRes, error) {
res := &host.AddJobsRes{}
return res, c.c.AddJobs(req, res)
}
type localStream struct {
stream rpcplus.Stream
err error
}
func (s localStream) Close() error {
close(s.stream.Error)
return nil
}
func (s localStream) Err() error {
return s.err
}
func (c *localClient) RegisterHost(h *host.Host, jobs chan *host.Job) cluster.Stream {
ch := make(chan interface{})
err := make(chan error)
s := localStream{stream: rpcplus.Stream{Send: ch, Error: err}}
go func() {
s.err = c.c.RegisterHost(&c.host, h, s.stream)
close(ch)
}()
go func() {
for job := range ch {
jobs <- job.(*host.Job).Dup()
}
close(jobs)
}()
return s
}
func (c *localClient) RemoveJobs(jobs []string) error {
return c.c.RemoveJobs(&c.host, jobs, nil)
}