Fix partial CUE tree run for dagger do
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
135
plan/plan.go
135
plan/plan.go
@@ -3,8 +3,6 @@ package plan
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
cueflow "cuelang.org/go/tools/flow"
|
||||
@@ -17,8 +15,9 @@ import (
|
||||
"go.opentelemetry.io/otel"
|
||||
)
|
||||
|
||||
const (
|
||||
ActionsPath = "actions"
|
||||
var (
|
||||
ActionSelector = cue.Str("actions")
|
||||
OutputSelector = cue.Str("outputs")
|
||||
)
|
||||
|
||||
type Plan struct {
|
||||
@@ -159,49 +158,19 @@ func (p *Plan) prepare(ctx context.Context) error {
|
||||
return flow.Run(ctx)
|
||||
}
|
||||
|
||||
// Up executes the plan
|
||||
func (p *Plan) Up(ctx context.Context, s solver.Solver) (*compiler.Value, error) {
|
||||
// Do executes an action in the plan
|
||||
func (p *Plan) Do(ctx context.Context, path cue.Path, s solver.Solver) error {
|
||||
ctx, span := otel.Tracer("dagger").Start(ctx, "plan.Up")
|
||||
defer span.End()
|
||||
|
||||
computed := compiler.NewValue()
|
||||
|
||||
cfg := &cueflow.Config{
|
||||
FindHiddenTasks: true,
|
||||
}
|
||||
if p.config.Target != "" {
|
||||
cfg.Root = cue.ParsePath(p.config.Target)
|
||||
// The target may reference dependencies outside of the target path.
|
||||
// InferTasks will include them in the workflow.
|
||||
cfg.InferTasks = true
|
||||
}
|
||||
flow := cueflow.New(
|
||||
cfg,
|
||||
p.source.Cue(),
|
||||
newRunner(p.context, s, computed),
|
||||
)
|
||||
if err := flow.Run(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if src, err := computed.Source(); err == nil {
|
||||
log.Ctx(ctx).Debug().Str("computed", string(src)).Msg("computed values")
|
||||
}
|
||||
|
||||
// FIXME: canceling the context makes flow return `nil`
|
||||
// Check explicitly if the context is canceled.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
return computed, nil
|
||||
}
|
||||
r := NewRunner(p.context, path, s)
|
||||
return r.Run(ctx, p.source)
|
||||
}
|
||||
|
||||
func (p *Plan) fillAction() {
|
||||
cfg := &cueflow.Config{
|
||||
FindHiddenTasks: true,
|
||||
Root: cue.ParsePath(ActionsPath),
|
||||
Root: cue.MakePath(ActionSelector),
|
||||
}
|
||||
|
||||
flow := cueflow.New(
|
||||
@@ -210,13 +179,13 @@ func (p *Plan) fillAction() {
|
||||
noOpRunner,
|
||||
)
|
||||
|
||||
actions := p.source.Lookup(ActionsPath)
|
||||
actions := p.source.LookupPath(cue.MakePath(ActionSelector))
|
||||
actionsComment := ""
|
||||
for _, cg := range actions.Doc() {
|
||||
actionsComment += cg.Text()
|
||||
}
|
||||
p.action = &Action{
|
||||
ActionsPath,
|
||||
ActionSelector.String(),
|
||||
false,
|
||||
actions.Path(),
|
||||
actionsComment,
|
||||
@@ -252,87 +221,3 @@ func (p *Plan) fillAction() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func noOpRunner(flowVal cue.Value) (cueflow.Runner, error) {
|
||||
v := compiler.Wrap(flowVal)
|
||||
_, err := task.Lookup(v)
|
||||
if err != nil {
|
||||
// Not a task
|
||||
if err == task.ErrNotTask {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Wrapper around `task.Run` that handles logging, tracing, etc.
|
||||
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
||||
return nil
|
||||
}), nil
|
||||
}
|
||||
|
||||
func newRunner(pctx *plancontext.Context, s solver.Solver, computed *compiler.Value) cueflow.TaskFunc {
|
||||
return func(flowVal cue.Value) (cueflow.Runner, error) {
|
||||
v := compiler.Wrap(flowVal)
|
||||
r, err := task.Lookup(v)
|
||||
if err != nil {
|
||||
// Not a task
|
||||
if err == task.ErrNotTask {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Wrapper around `task.Run` that handles logging, tracing, etc.
|
||||
return cueflow.RunnerFunc(func(t *cueflow.Task) error {
|
||||
ctx := t.Context()
|
||||
lg := log.Ctx(ctx).With().Str("task", t.Path().String()).Logger()
|
||||
ctx = lg.WithContext(ctx)
|
||||
ctx, span := otel.Tracer("dagger").Start(ctx, fmt.Sprintf("up: %s", t.Path().String()))
|
||||
defer span.End()
|
||||
|
||||
lg.Info().Str("state", string(task.StateComputing)).Msg(string(task.StateComputing))
|
||||
|
||||
// Debug: dump dependencies
|
||||
for _, dep := range t.Dependencies() {
|
||||
lg.Debug().Str("dependency", dep.Path().String()).Msg("dependency detected")
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
result, err := r.Run(ctx, pctx, s, compiler.Wrap(t.Value()))
|
||||
if err != nil {
|
||||
// FIXME: this should use errdefs.IsCanceled(err)
|
||||
if strings.Contains(err.Error(), "context canceled") {
|
||||
lg.Error().Dur("duration", time.Since(start)).Str("state", string(task.StateCanceled)).Msg(string(task.StateCanceled))
|
||||
} else {
|
||||
lg.Error().Dur("duration", time.Since(start)).Err(err).Str("state", string(task.StateFailed)).Msg(string(task.StateFailed))
|
||||
}
|
||||
return fmt.Errorf("%s: %w", t.Path().String(), err)
|
||||
}
|
||||
|
||||
lg.Info().Dur("duration", time.Since(start)).Str("state", string(task.StateCompleted)).Msg(string(task.StateCompleted))
|
||||
|
||||
// If the result is not concrete (e.g. empty value), there's nothing to merge.
|
||||
if !result.IsConcrete() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if src, err := result.Source(); err == nil {
|
||||
lg.Debug().Str("result", string(src)).Msg("merging task result")
|
||||
}
|
||||
|
||||
// Mirror task result in both `flow.Task` and `computed`
|
||||
if err := t.Fill(result.Cue()); err != nil {
|
||||
lg.Error().Err(err).Msg("failed to fill task")
|
||||
return err
|
||||
}
|
||||
|
||||
// Merge task value into computed
|
||||
if err := computed.FillPath(t.Path(), result); err != nil {
|
||||
lg.Error().Err(err).Msg("failed to fill plan")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}), nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user