2020-03-25 22:39:32 +00:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type asyncState struct {
|
2021-06-23 04:34:43 +00:00
|
|
|
f func(context.Context, State, *Constraints) (State, error)
|
2020-03-25 22:39:32 +00:00
|
|
|
prev State
|
|
|
|
target State
|
|
|
|
set bool
|
|
|
|
err error
|
|
|
|
g flightcontrol.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *asyncState) Output() Output {
|
|
|
|
return as
|
|
|
|
}
|
|
|
|
|
2021-06-23 01:47:32 +00:00
|
|
|
func (as *asyncState) Vertex(ctx context.Context, c *Constraints) Vertex {
|
2021-06-23 04:34:43 +00:00
|
|
|
err := as.Do(ctx, c)
|
2020-03-25 22:39:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return &errVertex{err}
|
|
|
|
}
|
|
|
|
if as.set {
|
|
|
|
out := as.target.Output()
|
|
|
|
if out == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-23 01:47:32 +00:00
|
|
|
return out.Vertex(ctx, c)
|
2020-03-25 22:39:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *asyncState) ToInput(ctx context.Context, c *Constraints) (*pb.Input, error) {
|
2021-06-23 04:34:43 +00:00
|
|
|
err := as.Do(ctx, c)
|
2020-03-25 22:39:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if as.set {
|
|
|
|
out := as.target.Output()
|
|
|
|
if out == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return out.ToInput(ctx, c)
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-23 04:34:43 +00:00
|
|
|
func (as *asyncState) Do(ctx context.Context, c *Constraints) error {
|
2020-03-25 22:39:32 +00:00
|
|
|
_, err := as.g.Do(ctx, "", func(ctx context.Context) (interface{}, error) {
|
|
|
|
if as.set {
|
|
|
|
return as.target, as.err
|
|
|
|
}
|
2021-06-23 04:34:43 +00:00
|
|
|
res, err := as.f(ctx, as.prev, c)
|
2020-03-25 22:39:32 +00:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-04-19 05:17:47 +00:00
|
|
|
if errors.Is(err, ctx.Err()) {
|
2020-03-25 22:39:32 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
as.target = res
|
|
|
|
as.err = err
|
|
|
|
as.set = true
|
|
|
|
return res, err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return as.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type errVertex struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-06-23 01:47:32 +00:00
|
|
|
func (v *errVertex) Validate(context.Context, *Constraints) error {
|
2020-03-25 22:39:32 +00:00
|
|
|
return v.err
|
|
|
|
}
|
2020-05-20 23:44:57 +00:00
|
|
|
func (v *errVertex) Marshal(context.Context, *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
|
2020-05-01 18:39:45 +00:00
|
|
|
return "", nil, nil, nil, v.err
|
2020-03-25 22:39:32 +00:00
|
|
|
}
|
|
|
|
func (v *errVertex) Output() Output {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (v *errVertex) Inputs() []Output {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Vertex = &errVertex{}
|