2021-11-25 01:22:33 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"cuelang.org/go/cue"
|
|
|
|
"go.dagger.io/dagger/compiler"
|
2022-01-11 21:40:02 +01:00
|
|
|
"go.dagger.io/dagger/pkg"
|
2021-11-25 01:22:33 +01:00
|
|
|
"go.dagger.io/dagger/plancontext"
|
|
|
|
"go.dagger.io/dagger/solver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotTask = errors.New("not a task")
|
|
|
|
tasks sync.Map
|
2021-12-20 19:00:43 +01:00
|
|
|
typePath = cue.MakePath(
|
|
|
|
cue.Str("$dagger"),
|
|
|
|
cue.Str("task"),
|
2022-02-16 19:37:16 +01:00
|
|
|
cue.Hid("_name", pkg.DaggerPackage))
|
2022-03-28 14:02:39 +02:00
|
|
|
corePath = cue.MakePath(
|
|
|
|
cue.Str("$dagger"),
|
|
|
|
cue.Str("task"),
|
|
|
|
cue.Hid("_name", pkg.DaggerCorePackage))
|
|
|
|
paths = []cue.Path{corePath, typePath}
|
2021-11-25 01:22:33 +01:00
|
|
|
)
|
|
|
|
|
2022-02-19 00:00:38 +01:00
|
|
|
// State is the state of the task.
|
2022-04-19 02:50:34 +02:00
|
|
|
type State int8
|
|
|
|
|
|
|
|
func (s State) String() string {
|
|
|
|
return [...]string{"computing", "cancelled", "failed", "completed"}[s]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseState(s string) (State, error) {
|
|
|
|
switch s {
|
|
|
|
case "computing":
|
|
|
|
return StateComputing, nil
|
|
|
|
case "cancelled":
|
|
|
|
return StateCanceled, nil
|
|
|
|
case "failed":
|
|
|
|
return StateFailed, nil
|
|
|
|
case "completed":
|
|
|
|
return StateCompleted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1, fmt.Errorf("invalid state [%s]", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) CanTransition(t State) bool {
|
|
|
|
return s == StateComputing && s <= t
|
|
|
|
}
|
2022-02-19 00:00:38 +01:00
|
|
|
|
|
|
|
const (
|
2022-04-19 02:50:34 +02:00
|
|
|
StateComputing State = iota
|
|
|
|
StateCanceled
|
|
|
|
StateFailed
|
|
|
|
StateCompleted
|
2022-02-19 00:00:38 +01:00
|
|
|
)
|
|
|
|
|
2021-11-25 01:22:33 +01:00
|
|
|
type NewFunc func() Task
|
|
|
|
|
|
|
|
type Task interface {
|
2022-03-23 23:02:17 +01:00
|
|
|
Run(ctx context.Context, pctx *plancontext.Context, s *solver.Solver, v *compiler.Value) (*compiler.Value, error)
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 20:23:52 +01:00
|
|
|
type PreRunner interface {
|
|
|
|
Task
|
|
|
|
|
|
|
|
PreRun(ctx context.Context, pctx *plancontext.Context, v *compiler.Value) error
|
|
|
|
}
|
|
|
|
|
2021-11-25 01:22:33 +01:00
|
|
|
// Register a task type and initializer
|
|
|
|
func Register(typ string, f NewFunc) {
|
|
|
|
tasks.Store(typ, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Task of the given type.
|
|
|
|
func New(typ string) Task {
|
|
|
|
v, ok := tasks.Load(typ)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fn := v.(NewFunc)
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Lookup(v *compiler.Value) (Task, error) {
|
|
|
|
if v.Kind() != cue.StructKind {
|
2022-03-09 13:29:26 +01:00
|
|
|
return nil, ErrNotTask
|
2021-11-25 01:22:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:02:39 +02:00
|
|
|
typeString, err := lookupType(v)
|
2021-11-25 01:22:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := New(typeString)
|
|
|
|
if t == nil {
|
|
|
|
return nil, fmt.Errorf("unknown type %q", typeString)
|
|
|
|
}
|
2022-03-07 14:12:39 +01:00
|
|
|
|
2021-11-25 01:22:33 +01:00
|
|
|
return t, nil
|
|
|
|
}
|
2022-03-28 14:02:39 +02:00
|
|
|
|
|
|
|
func lookupType(v *compiler.Value) (string, error) {
|
|
|
|
for _, path := range paths {
|
|
|
|
typ := v.LookupPath(path)
|
|
|
|
if typ.Exists() {
|
|
|
|
return typ.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ErrNotTask
|
|
|
|
}
|