-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
163 lines (143 loc) · 3.63 KB
/
main.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
// Package main defines a command line for running a command against the workers
package main
import (
"flag"
"fmt"
"io"
"math"
"net"
"os"
"strconv"
"time"
"github.com/dominichamon/sprinkle/internal"
"github.com/golang/glog"
"golang.org/x/net/context"
pb "github.com/dominichamon/sprinkle/api/sprinkle"
)
var (
cmd = flag.String("cmd", "", "The command to run")
ram = flag.Uint64("ram", 0, "The amount of RAM to reserve for the command")
wait = flag.Bool("wait", true, "Whether to wait for the command to complete")
addr = flag.String("addr", "239.192.0.1:9999", "The multicast address to use for discovery")
port = flag.Int("port", 9998, "The port to listen on for discovery")
retries = flag.Int("retries", 3, "Number of times to retry running the command")
retryWait = flag.Duration("retry_wait", 10*time.Second, "time between retries")
)
func bestWorker(ctx context.Context, ram uint64, addrs <-chan string) *internal.Worker {
var worker *internal.Worker
bestFreeRam := uint64(math.Inf(1))
for addr := range addrs {
glog.Infof("discovered worker at %s", addr)
host, port, err := net.SplitHostPort(addr)
if err != nil {
glog.Error(err)
continue
}
p, err := strconv.ParseInt(port, 10, 32)
if err != nil {
glog.Error(err)
continue
}
s, err := internal.NewWorker(host, int(p))
if err != nil {
glog.Error(err)
continue
}
stat, err := s.Client.Status(ctx, &pb.StatusRequest{})
if err != nil {
glog.Errorf("failed to get status for %+v: %s", s, err)
}
glog.Infof("Status of %s [%s]: %+v", s.Id, addr, stat)
if stat.FreeRam > ram {
if worker == nil || stat.FreeRam < bestFreeRam {
// Close out any worker we previously found
if worker != nil {
if err := worker.Close(); err != nil {
glog.Warningf("failed to close worker: %s", err)
}
}
worker = s
bestFreeRam = stat.FreeRam
}
}
}
return worker
}
func main() {
flag.Parse()
ctx := context.Background()
// Discover best worker.
addrs := make(chan string)
if err := internal.Ping(*addr, *port, addrs); err != nil {
glog.Exit("failed to find workers: +v", err)
}
var worker *internal.Worker
var resp *pb.RunResponse
var errs []error
for i := 0; i < *retries; i++ {
// Close worker from previous attempt
if worker != nil {
if err := worker.Close(); err != nil {
glog.Warningf("failed to close worker: %s", err)
}
}
worker = bestWorker(ctx, *ram, addrs)
if worker == nil {
errs = append(errs, fmt.Errorf("failed to identify best worker"))
time.Sleep(*retryWait)
continue
}
glog.Infof("best worker found: %q", worker.Id)
// Run command.
var err error
resp, err = worker.Client.Run(ctx, &pb.RunRequest{
Cmd: *cmd,
Ram: *ram,
})
if err != nil {
errs = append(errs, fmt.Errorf("failed to run command: %s", err))
time.Sleep(*retryWait)
continue
}
break
}
if len(errs) != 0 {
for e := range errs {
glog.Errorln(e)
}
glog.Exit()
}
defer func() {
if err := worker.Close(); err != nil {
glog.Exit(err)
}
}()
job := resp.JobId
glog.Infof("running job %d on worker %q", job, worker.Id)
if *wait {
// no need to check on the job as the logs stream until the job
// is complete.
stream, err := worker.Client.Logs(ctx, &pb.LogsRequest{JobId: job})
if err != nil {
glog.Exit(err)
}
for {
chunk, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
glog.Exit(err)
}
if len(chunk.Chunk) == 0 {
continue
}
switch chunk.Type {
case pb.LogType_STDOUT:
fmt.Fprint(os.Stdout, chunk.Chunk)
case pb.LogType_STDERR:
fmt.Fprint(os.Stderr, chunk.Chunk)
}
}
}
}