Solve sub build miss synchronous behavior

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-08-23 15:15:05 +02:00
parent 350e6ac5a2
commit 95468ce2b3
No known key found for this signature in database
GPG Key ID: 3C9847D981AAC1BF
2 changed files with 40 additions and 15 deletions

View File

@ -170,16 +170,25 @@ func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.
go catchOutput(buildCh) go catchOutput(buildCh)
resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) { resp, err := c.c.Build(ctx, opts, "", func(ctx context.Context, gw bkgw.Client) (*bkgw.Result, error) {
eventsWg := sync.WaitGroup{}
closeCh := make(chan *bk.SolveStatus)
// Close events channel // Close events channel
defer close(eventsCh) defer func() {
close(closeCh)
eventsWg.Wait()
close(eventsCh)
}()
s := solver.New(solver.Opts{ s := solver.New(solver.Opts{
Control: c.c, Control: c.c,
Gateway: gw, Gateway: gw,
Events: eventsCh, Events: eventsCh,
Auth: auth, EventsWg: &eventsWg,
Secrets: secrets, CloseEvent: closeCh,
NoCache: c.cfg.NoCache, Auth: auth,
Secrets: secrets,
NoCache: c.cfg.NoCache,
}) })
// Compute output overlay // Compute output overlay

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"sync"
bk "github.com/moby/buildkit/client" bk "github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
@ -23,12 +24,14 @@ type Solver struct {
} }
type Opts struct { type Opts struct {
Control *bk.Client Control *bk.Client
Gateway bkgw.Client Gateway bkgw.Client
Events chan *bk.SolveStatus Events chan *bk.SolveStatus
Auth *RegistryAuthProvider EventsWg *sync.WaitGroup
Secrets session.Attachable CloseEvent chan *bk.SolveStatus
NoCache bool Auth *RegistryAuthProvider
Secrets session.Attachable
NoCache bool
} }
func New(opts Opts) Solver { func New(opts Opts) Solver {
@ -169,11 +172,24 @@ func (s Solver) Export(ctx context.Context, st llb.State, img *dockerfile2llb.Im
// Forward this build session events to the main events channel, for logging // Forward this build session events to the main events channel, for logging
// purposes. // purposes.
go func() { go func() {
for event := range ch { select {
s.opts.Events <- event case <-s.opts.CloseEvent:
return
default:
for event := range ch {
s.opts.Events <- event
}
} }
}() }()
// Add task to events
s.opts.EventsWg.Add(1)
// Resolve event
defer func() {
s.opts.EventsWg.Done()
}()
return s.opts.Control.Build(ctx, opts, "", func(ctx context.Context, c bkgw.Client) (*bkgw.Result, error) { return s.opts.Control.Build(ctx, opts, "", func(ctx context.Context, c bkgw.Client) (*bkgw.Result, error) {
res, err := c.Solve(ctx, bkgw.SolveRequest{ res, err := c.Solve(ctx, bkgw.SolveRequest{
Definition: def, Definition: def,