-
Notifications
You must be signed in to change notification settings - Fork 230
/
solve.go
68 lines (59 loc) · 1.49 KB
/
solve.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
package client
import (
"context"
"time"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
)
// Solve calls Solve on the controller.
func (c *Client) Solve(ctx context.Context, req *controlapi.SolveRequest, ch chan *controlapi.StatusResponse) error {
defer close(ch)
if c.controller == nil {
// Create the controller.
if err := c.createController(); err != nil {
return err
}
}
statusCtx, cancelStatus := context.WithCancel(context.Background())
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
defer func() { // make sure the Status ends cleanly on build errors
go func() {
<-time.After(3 * time.Second)
cancelStatus()
}()
}()
_, err := c.controller.Solve(ctx, req)
if err != nil {
return errors.Wrap(err, "failed to solve")
}
return nil
})
eg.Go(func() error {
srv := &controlStatusServer{
ctx: statusCtx,
ch: ch,
}
return c.controller.Status(&controlapi.StatusRequest{
Ref: req.Ref,
}, srv)
})
return eg.Wait()
}
type controlStatusServer struct {
ctx context.Context
ch chan *controlapi.StatusResponse
grpc.ServerStream // dummy
}
func (x *controlStatusServer) SendMsg(m interface{}) error {
return x.Send(m.(*controlapi.StatusResponse))
}
func (x *controlStatusServer) Send(m *controlapi.StatusResponse) error {
x.ch <- m
return nil
}
func (x *controlStatusServer) Context() context.Context {
return x.ctx
}