coretask

package
v0.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultTagPriority = 100

DefaultTagPriority is the default priority assigned to a provided tag when not explicitly specified.

View Source
const (
	KHISystemPrefix = "khi.google.com/"
)
View Source
const LabelKeyProvidedTagPrefix = KHISystemPrefix + "provided-tag/"

LabelKeyProvidedTagPrefix is the prefix used for tag labels on tasks.

View Source
const LabelKeyProvidedTagPriorityPrefix = KHISystemPrefix + "provided-tag-priority/"

LabelKeyProvidedTagPriorityPrefix is the prefix used for tag priority labels on tasks.

View Source
const LabelKeyProvidedTagTypePrefix = KHISystemPrefix + "provided-tag-type/"

LabelKeyProvidedTagTypePrefix is the prefix used for tag output type labels on tasks.

Variables

View Source
var (
	// FromActiveFeatures configures the dependency scope to ScopeActiveFeatures.
	FromActiveFeatures = taskid.ScopeActiveFeatures
	// FromActiveGraph configures the dependency scope to ScopeActiveGraph.
	FromActiveGraph = taskid.ScopeActiveGraph
	// FromAll configures the dependency scope to ScopeAll.
	FromAll = taskid.ScopeAll
)
View Source
var LabelKeyFeatureGateTaskRef = NewTaskLabelKey[taskid.UntypedTaskReference](KHISystemPrefix + "feature-gate-task-ref")

LabelKeyFeatureGateTaskRef is a task label key specifying the task reference whose presence in the active graph gates inclusion of this task when resolved via ScopeActiveFeatures.

View Source
var LabelKeyRequiredTask = NewTaskLabelKey[bool](KHISystemPrefix + "required-task")

LabelKeyRequiredTask is the task label to tell task resolver to always include the task in the task graph when the task is available.

View Source
var LabelKeyTaskDescription = NewTaskLabelKey[string](KHISystemPrefix + "task-description")

LabelKeyTaskDescription is the task label to record a human-readable description of the task.

View Source
var LabelKeyTaskResultRetention = NewTaskLabelKey[bool](KHISystemPrefix + "task-result-retention")

LabelKeyTaskResultRetention indicates whether the task result should be retained in the runner after all dependent tasks finish.

View Source
var LabelKeyTaskResultType = NewTaskLabelKey[string](KHISystemPrefix + "task-result-type")

LabelKeyTaskResultType is the task label to record the string representation of the task output type.

View Source
var LabelKeyTaskSelectionPriority = NewTaskLabelKey[int](KHISystemPrefix + "task-selection-priority")
View Source
var LabelKeyTaskTitle = NewTaskLabelKey[string](KHISystemPrefix + "task-title")

LabelKeyTaskTitle is the task label to record a human-readable title of the task.

Functions

func After added in v0.60.1

func After(b *Binder, dep Dependency)

After declares a dependency that the task only waits for. It intentionally returns no handle, so the task cannot read the value and the declaration cannot become an unused input.

func GetTaskResultFromLocalRunner

func GetTaskResultFromLocalRunner[TaskResult any](runner *LocalRunner, taskRef taskid.TaskReference[TaskResult]) (TaskResult, bool)

GetTaskResultFromLocalRunner is a helper function to safely extract a specific task's result from a LocalRunner's result map. It provides a type-safe way to access results using a TaskReference.

func HasDependency

func HasDependency(taskSet *TaskSet, dependencyFrom UntypedTask, dependencyTo UntypedTask) (bool, error)

HasDependency check if 2 tasks have dependency between them when the task graph was resolved with given task set.

func NewEqualFilter

func NewEqualFilter[T comparable](labelKey TaskLabelKey[T], value T, includeUndefined bool) filter.TypedMapFilter[T]

NewEqualFilter creates a new filter that matches exact label values

func NewLabelSet

func NewLabelSet(labelOpts ...LabelOpt) *typedmap.ReadonlyTypedMap

Construct the LabelSet with required fields.

func NewRequiredTaskLabel

func NewRequiredTaskLabel() *requiredTaskLabelImpl

NewRequiredTaskLabel returns a LabelOpt to mark the task is always included in the result task graph.

func WrapErrorWithTaskInformation

func WrapErrorWithTaskInformation(ctx context.Context, err error) error

WrapErrorWithTaskInformation annotates given error with the current task information.

Types

type Binder added in v0.60.1

type Binder struct {
	// contains filtered or unexported fields
}

Binder collects the inputs of a task defined with Define. Inputs can only be declared while the bind function passed to Define is running.

type Dependency added in v0.59.0

type Dependency = taskid.DependencyDescriptor

Dependency represents any task dependency descriptor, such as a point-to-point reference or a fan-in tag reference.

type Input added in v0.60.1

type Input[T any] struct {
	// contains filtered or unexported fields
}

Input is a handle to a required input declared with Use.

func Use added in v0.60.1

func Use[T any](b *Binder, ref taskid.TaskReference[T]) Input[T]

Use declares a required input and returns the handle to read its value. The reference must use ScopeAll, the default scope, so that the producer is always pulled into the graph. Use UseOptional for references with a narrower scope. Define prefixes the panic for a wrong scope with the task ID.

func (Input[T]) Get added in v0.60.1

func (in Input[T]) Get(ctx context.Context) T

Get returns the result of the producer task.

type InputKind added in v0.60.1

type InputKind int

InputKind classifies how a task depends on an input after all declarations of the input are merged.

const (
	// InputKindRequired is an input whose value the task reads and whose producer must be in the graph.
	// Use declares it. An input declared with UseOptional also becomes required when an ordering dependency
	// on the same reference uses ScopeAll.
	InputKindRequired InputKind = iota
	// InputKindOptional is an input whose value the task reads and whose producer may be absent from the graph.
	// UseOptional declares it.
	InputKindOptional
	// InputKindTag is a fan-in input declared with UseTag over all producers of a tag.
	InputKindTag
	// InputKindOrdering is a dependency declared only with After. The task waits for it but never reads its value.
	InputKindOrdering
)

func (InputKind) String added in v0.60.1

func (k InputKind) String() string

String returns a human readable name of the input kind. It panics for values other than the declared InputKind constants because they indicate a programming error.

type InputSpec added in v0.60.1

type InputSpec struct {
	// Dependency is the dependency descriptor registered in the task graph for this input.
	Dependency Dependency
	// Kind is the merged kind of every declaration of the input.
	Kind InputKind
}

InputSpec describes one input declared on a Binder.

type Interceptor added in v0.50.0

type Interceptor func(ctx context.Context, task UntypedTask, next func(context.Context) (any, error)) (any, error)

Interceptor is a function that can intercept the execution of a task. It allows injecting custom logic before and after the task execution.

type LabelOpt

type LabelOpt interface {
	Write(labels *typedmap.TypedMap)
}

LabelOpt implementations wraps setting values to the task albels.

func NewTaskResultRetentionLabel added in v0.58.0

func NewTaskResultRetentionLabel(retain bool) LabelOpt

NewTaskResultRetentionLabel returns a LabelOpt to specify whether the task result should be retained after all dependent tasks finish.

func ProvidesTag added in v0.59.0

func ProvidesTag[TaskResult any](tag Tag[TaskResult], opts ...ProvidesTagOption) LabelOpt

ProvidesTag returns a LabelOpt declaring that the task provides the given typed tag. Optional ProvidesTagOptions (such as WithTagPriority) can be specified to adjust tag attributes.

func WithDescription added in v0.61.0

func WithDescription(description string) LabelOpt

WithDescription returns a LabelOpt to attach a human-readable description to the task.

func WithFeatureGate added in v0.60.1

func WithFeatureGate(featureTaskRef taskid.UntypedTaskReference) LabelOpt

WithFeatureGate sets the feature gate task reference for a task. When a task with a feature gate is referenced via ScopeActiveFeatures, it is pulled into the graph only if it is already in the graph or its feature gate task is present in the active graph. Tasks without WithFeatureGate are pulled in unconditionally when referenced via ScopeActiveFeatures.

func WithLabelValue

func WithLabelValue[T any](labelKey TaskLabelKey[T], value T) LabelOpt

WithLabelValue creates a LabelOpt to store a single value associated to a label key.

func WithSelectionPriority

func WithSelectionPriority(priority int) LabelOpt

func WithTitle added in v0.61.0

func WithTitle(title string) LabelOpt

WithTitle returns a LabelOpt to attach a human-readable title to the task.

type LabelPredicate

type LabelPredicate[T any] = func(v T) bool

type LocalRunner

type LocalRunner struct {
	// contains filtered or unexported fields
}

LocalRunner executes a task graph defined by a TaskSet on the local machine. It manages task dependencies, concurrent execution, and result aggregation.

func NewLocalRunner

func NewLocalRunner(taskSet *TaskSet) (*LocalRunner, error)

NewLocalRunner creates and initializes a new LocalRunner for a given TaskSet. The TaskSet must be runnable (i.e., topologically sorted with all dependencies met). It returns an error if the provided TaskSet is not runnable.

func (*LocalRunner) AddInterceptor added in v0.50.0

func (r *LocalRunner) AddInterceptor(interceptor Interceptor)

AddInterceptor adds an interceptor to the runner. Interceptors are executed in the order they are added.

func (*LocalRunner) Result

func (r *LocalRunner) Result() (*typedmap.ReadonlyTypedMap, error)

Result returns the final results of the task graph execution. It returns a map of task results if the execution was successful, or an error if any task failed or the runner has not yet completed. This method should only be called after the channel from Wait() has been closed.

func (*LocalRunner) Run

func (r *LocalRunner) Run(ctx context.Context) error

Run starts the execution of the task graph in a non-blocking manner. It launches a goroutine to manage the entire execution process. It returns an error if the runner has already been started.

func (*LocalRunner) TaskRunStatuses added in v0.59.0

func (r *LocalRunner) TaskRunStatuses() map[string]TaskRunStatus

TaskRunStatuses returns a snapshot of the execution state of every task in the runner's task set, keyed by the task implementation ID. The returned map and its values are detached from the runner, so they are safe to read while the task graph keeps running.

func (*LocalRunner) Tasks added in v0.50.0

func (r *LocalRunner) Tasks() []UntypedTask

func (*LocalRunner) Wait

func (r *LocalRunner) Wait() <-chan interface{}

Wait returns a channel that is closed when the runner finishes executing all tasks in the graph. This is the primary mechanism for waiting for the completion of the entire task set.

type OptionalInput added in v0.60.1

type OptionalInput[T any] struct {
	// contains filtered or unexported fields
}

OptionalInput is a handle to an optional input declared with UseOptional.

func UseOptional added in v0.60.1

func UseOptional[T any](b *Binder, ref taskid.TaskReference[T]) OptionalInput[T]

UseOptional declares an input whose producer may be absent from the graph and returns the handle to read it. The reference must use a scope narrower than ScopeAll, such as ScopeActiveGraph or ScopeActiveFeatures. Define prefixes the panic for a wrong scope with the task ID.

func (OptionalInput[T]) Get added in v0.60.1

func (in OptionalInput[T]) Get(ctx context.Context) (T, bool)

Get returns the result of the producer task and true, or the zero value and false when the producer is not in the graph.

type ProvidesTagOption added in v0.59.0

type ProvidesTagOption interface {
	// contains filtered or unexported methods
}

ProvidesTagOption is an option to configure tag provision attributes on a task.

func WithTagPriority added in v0.59.0

func WithTagPriority(priority int) ProvidesTagOption

WithTagPriority returns a ProvidesTagOption specifying the priority weight of the provided tag during graph resolution and cycle pruning. Lower numerical values indicate higher precedence (e.g. 10 is higher priority than 100). The default priority when unspecified is DefaultTagPriority (100).

type Tag added in v0.59.0

type Tag[TaskResult any] struct {
	// contains filtered or unexported fields
}

Tag represents a strongly-typed tag identifier that groups task outputs of type TaskResult.

func NewTag added in v0.59.0

func NewTag[TaskResult any](id string) Tag[TaskResult]

NewTag creates a new typed tag with the given identifier.

func (Tag[TaskResult]) ID added in v0.59.0

func (t Tag[TaskResult]) ID() string

ID returns the string identifier of the tag.

func (Tag[TaskResult]) Ref added in v0.59.0

func (t Tag[TaskResult]) Ref(opts ...taskid.FanInOption) TagReference[TaskResult]

Ref creates a typed dependency reference to tasks providing this tag with optional configurations.

func (Tag[TaskResult]) String added in v0.59.0

func (t Tag[TaskResult]) String() string

String returns the string representation of the tag.

type TagInput added in v0.60.1

type TagInput[T any] struct {
	// contains filtered or unexported fields
}

TagInput is a handle to a fan-in input declared with UseTag.

func UseTag added in v0.60.1

func UseTag[T any](b *Binder, ref TagReference[T]) TagInput[T]

UseTag declares a fan-in input over all producers of a tag and returns the handle to read their results. The tag reference must not use ScopeAll because fan-in resolution only supports ScopeActiveFeatures and ScopeActiveGraph.

func (TagInput[T]) Get added in v0.60.1

func (in TagInput[T]) Get(ctx context.Context) []T

Get returns the results of all producer tasks bound to the tag in deterministic order.

type TagReference added in v0.59.0

type TagReference[TaskResult any] interface {
	taskid.FanInDescriptor
	// GetZeroValue returns a zero value of the TaskResult type to preserve type safety.
	GetZeroValue() TaskResult
}

TagReference defines a typed reference to an aggregation of tasks providing a specific tag.

func NewTagReference added in v0.59.0

func NewTagReference[TaskResult any](tag string, opts ...taskid.FanInOption) TagReference[TaskResult]

NewTagReference creates a new TagReference for the specified tag and options.

type Task

type Task[TaskResult any] interface {
	UntypedTask
	// ID returns an unique TaskID of taskid.TaskImplementationID[TaskResult]
	// The implementation of this function must return a constant value.
	ID() taskid.TaskImplementationID[TaskResult]

	Run(ctx context.Context) (TaskResult, error)
}

Task is the fundamental interface that all of DAG nodes in KHI task system implements. The implementation of ID and Labels must be deterministic when the application started. The implementation of Sinks and Source must be pure function not depending anything outside of the argument.

func Define added in v0.60.1

func Define[T any](
	id taskid.TaskImplementationID[T],
	bind func(b *Binder) func(ctx context.Context) (T, error),
	labelOpts ...LabelOpt,
) Task[T]

Define constructs a task whose dependencies are declared through a Binder. bind runs once when Define is called. It must declare every input with Use, UseOptional, UseTag, or After, and return the run function that reads the inputs through the returned handles. Keep handles in local variables of bind so that the compiler reports inputs that are declared but never read.

func DefineAliasTask added in v0.60.1

func DefineAliasTask[TaskResult any](taskId taskid.TaskImplementationID[TaskResult], sourceTaskReference taskid.TaskReference[TaskResult], labelOpts ...LabelOpt) Task[TaskResult]

DefineAliasTask defines a task implementation that proxies the result of another task. This is useful for selectively overriding dependencies on a per-task basis.

func DefineConstant added in v0.60.1

func DefineConstant[T any](id taskid.TaskImplementationID[T], value T, labelOpts ...LabelOpt) Task[T]

DefineConstant constructs a task that has no inputs and always returns value. The same value is returned on every run, so callers must not modify reference types such as slices or maps in it.

func DefineTailTask added in v0.60.1

func DefineTailTask(taskID taskid.TaskImplementationID[struct{}], dependencies []Dependency, labelOpts ...LabelOpt) Task[struct{}]

DefineTailTask constructs a no-op barrier task that waits for all dependencies without reading their values. Duplicate dependencies are merged into one input by the Binder.

type TaskError added in v0.60.1

type TaskError struct {
	TaskID taskid.UntypedTaskImplementationID
	Err    error
}

TaskError represents an error that occurred during the execution of a task in a task graph.

func (*TaskError) Error added in v0.60.1

func (e *TaskError) Error() string

Error returns the formatted error message including the failed task ID and the underlying error.

func (*TaskError) Unwrap added in v0.60.1

func (e *TaskError) Unwrap() error

Unwrap returns the underlying error returned by the task.

type TaskLabelKey

type TaskLabelKey[LabelValueType any] = typedmap.TypedKey[LabelValueType]

TaskLabelKey is a key of labels given to task.

func LabelKeyProvidedTag added in v0.59.0

func LabelKeyProvidedTag(tagID string) TaskLabelKey[bool]

LabelKeyProvidedTag returns a TaskLabelKey to record a provided tag on a task.

func LabelKeyProvidedTagPriority added in v0.59.0

func LabelKeyProvidedTagPriority(tagID string) TaskLabelKey[int]

LabelKeyProvidedTagPriority returns a TaskLabelKey to record the priority of a provided tag on a task.

func LabelKeyProvidedTagType added in v0.59.0

func LabelKeyProvidedTagType(tagID string) TaskLabelKey[string]

LabelKeyProvidedTagType returns a TaskLabelKey to record the output type of a provided tag on a task.

func NewTaskLabelKey

func NewTaskLabelKey[T any](key string) TaskLabelKey[T]

NewTaskLabelKey returns the key used in labels with type annotation,

type TaskRunPhase added in v0.59.0

type TaskRunPhase int

TaskRunPhase represents the execution phase of a single task in a task graph.

const (
	// TaskRunPhaseWaiting indicates that the task is waiting for its dependencies to complete.
	TaskRunPhaseWaiting TaskRunPhase = iota
	// TaskRunPhaseRunning indicates that the task is currently running.
	TaskRunPhaseRunning
	// TaskRunPhaseDone indicates that the task finished successfully.
	TaskRunPhaseDone
	// TaskRunPhaseError indicates that the task finished with an error.
	TaskRunPhaseError
)

func (TaskRunPhase) String added in v0.59.0

func (p TaskRunPhase) String() string

String returns the human-readable name of the task run phase.

type TaskRunStatus added in v0.59.0

type TaskRunStatus struct {
	Phase     TaskRunPhase
	StartTime time.Time
	EndTime   time.Time
	Error     error
}

TaskRunStatus is an immutable snapshot of the execution state of a single task. StartTime is zero while the task is waiting, and EndTime is zero until the task finishes. Error holds the error returned by the task when Phase is TaskRunPhaseError.

type TaskRunner

type TaskRunner interface {
	Run(ctx context.Context) error
	Wait() <-chan interface{}
	Result() (*typedmap.ReadonlyTypedMap, error)
	Tasks() []UntypedTask
	TaskRunStatuses() map[string]TaskRunStatus
	AddInterceptor(interceptor Interceptor)
}

TaskRunner receives the runnable TaskSet and run tasks with topological sorted order.

type TaskSet

type TaskSet struct {
	// contains filtered or unexported fields
}

TaskSet is a collection of tasks and resolved dependency edges. It implements core_contract.TaskGraphMetadata and provides querying for execution order and edges.

func NewResolvedTaskSet added in v0.59.0

func NewResolvedTaskSet(
	tasks []UntypedTask,
	edges []taskid.TaskEdge,
	boundFanInRefIDsByTaskImplID map[string]map[string][]string,
) *TaskSet

NewResolvedTaskSet creates a new runnable TaskSet with resolved tasks, edges, and metadata.

func NewTaskSet

func NewTaskSet(tasks []UntypedTask) (*TaskSet, error)

NewTaskSet creates a new TaskSet with the given tasks. Returns an error if there are duplicate task IDs.

func ResolveGraph added in v0.59.0

func ResolveGraph(
	initialTasks []UntypedTask,
	availableTasks []UntypedTask,
	disabledTasks []UntypedTask,
) (*TaskSet, error)

ResolveGraph resolves the task graph starting from initialTasks, drawing dependencies from availableTasks, and excluding disabledTasks. It applies a deterministic 5-phase resolution algorithm and returns a runnable TaskSet containing topologically sorted tasks and concrete TaskEdges.

func Subset

func Subset[T any](taskSet *TaskSet, mapFilter filter.TypedMapFilter[T]) *TaskSet

Subset returns a new TaskSet filtered using the provided type-safe filter

func (*TaskSet) Add

func (s *TaskSet) Add(newTask UntypedTask) error

Add a task definition to current TaskSet. Returns an error when duplicated task Id is assigned on the task.

func (*TaskSet) BoundReferenceIDsForTaskImplWithTag added in v0.59.0

func (s *TaskSet) BoundReferenceIDsForTaskImplWithTag(taskImplementationID string, tag string) []string

BoundReferenceIDsForTaskImplWithTag returns the list of task reference IDs providing the tag bound specifically to the given task implementation ID.

func (*TaskSet) Edges added in v0.59.0

func (s *TaskSet) Edges() []taskid.TaskEdge

Edges returns a copy of all resolved edges in the set.

func (*TaskSet) Get

func (s *TaskSet) Get(id string) (UntypedTask, error)

Get returns a task with the given string task ID notation.

func (*TaskSet) GetAll

func (s *TaskSet) GetAll() []UntypedTask

GetAll returns a copy of all tasks in the set.

func (*TaskSet) IncomingEdges added in v0.59.0

func (s *TaskSet) IncomingEdges(taskImplID string) []taskid.TaskEdge

IncomingEdges returns incoming edges for the given task implementation ID.

func (*TaskSet) IsBound added in v0.59.0

func (s *TaskSet) IsBound(refID string) bool

IsBound returns true if the task reference was bound to the graph.

func (*TaskSet) Remove

func (s *TaskSet) Remove(id string) error

Remove a task definition from current DefinitionSet. Returns error if the definition does not exist

type UntypedTask

type UntypedTask interface {
	UntypedID() taskid.UntypedTaskImplementationID
	// Labels returns KHITaskLabelSet assigned to this task unit.
	// The implementation of this function must return a constant value.
	Labels() *typedmap.ReadonlyTypedMap

	// Inputs returns the inputs declared for this task in declaration order.
	Inputs() []InputSpec

	// ResultType returns the reflection Type of the task output.
	ResultType() reflect.Type

	UntypedRun(ctx context.Context) (any, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL