task

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StateUnknown   = corev1.TaskState_Unknown
	StatePending   = corev1.TaskState_Pending
	StateRunning   = corev1.TaskState_Running
	StateCompleted = corev1.TaskState_Completed
	StateFailed    = corev1.TaskState_Failed
	StateCanceled  = corev1.TaskState_Canceled
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ActiveTask

type ActiveTask interface {
	// Returns the task's ID.
	TaskId() string
	// Decodes the task's metadata (json string) into the output variable.
	// The output variable must be a pointer to a struct. If the task has no
	// metadata, the output variable will be left unchanged.
	LoadTaskMetadata(output any)
	// Sets the task's progress.
	SetProgress(progress *corev1.Progress)
	// Gets the task's progress.
	GetProgress() *corev1.Progress
	// Adds a log entry to the task's status.
	AddLogEntry(level slog.Level, msg string)
}

ActiveTask is an interface to a currently running task's state and metadata. All functions in this interface other than TaskId() will block and retry upon encountering an error (e.g. storage unavailable), or until the task controller's context is canceled.

type Controller

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

Controller manages launching tasks and configuring their underlying state machines, and saving their state to persistent storage.

func NewController

func NewController(ctx context.Context, name string, store KVStore, runner TaskRunner) (*Controller, error)

Creates a new task controller, and resumes any tasks that have state saved in the key-value store if they did not already complete. Saved state for completed tasks will be cleaned.

func (*Controller) CancelTask

func (c *Controller) CancelTask(id string)

Attempts a best-effort cancellation of a task. There are no guarantees that the task will actually be canceled. If a task is already completed, this has no effect.

func (*Controller) LaunchTask

func (c *Controller) LaunchTask(id string, opts ...NewTaskOption) error

func (*Controller) ListTasks

func (c *Controller) ListTasks() ([]string, error)

func (*Controller) TaskStatus

func (c *Controller) TaskStatus(id string) (*corev1.TaskStatus, error)

type NewTaskOption

type NewTaskOption func(*NewTaskOptions)

func WithJsonMetadata

func WithJsonMetadata(md string) NewTaskOption

func WithMetadata

func WithMetadata(md any) NewTaskOption

func WithStateCallback

func WithStateCallback(ch chan State) NewTaskOption

Like StatusCallback, but only writes the task's state value instead of the entire status.

func WithStatusCallback

func WithStatusCallback(ch chan *Status) NewTaskOption

When the task enters one of the completed states (Completed, Failed, or Canceled), the task's status will be written to the provided channel. If the channel cannot be written to at the time the task is completed, the status will be dropped, so a buffered channel should be used if necessary.

type NewTaskOptions

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

type State

type State = corev1.TaskState

type Status

type Status = corev1.TaskStatus

type Task

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

func (*Task) AddLogEntry

func (t *Task) AddLogEntry(level slog.Level, msg string)

func (*Task) GetProgress

func (t *Task) GetProgress() *corev1.Progress

func (*Task) LoadTaskMetadata

func (t *Task) LoadTaskMetadata(output any)

func (*Task) SetProgress

func (t *Task) SetProgress(progress *corev1.Progress)

func (*Task) TaskId

func (t *Task) TaskId() string

type TaskRunner

type TaskRunner interface {
	// Handles the Pending state of a task. The task will remain Pending until
	// this function returns.
	// The state of the task is transitioned depending on the return value:
	// - If the function returns nil, the task will be transitioned to Running.
	// - If the function returns an error other than ctx.Err(), the task will be
	//   transitioned to Failed.
	// - If the function returns ctx.Err(), the task will be transitioned to
	//   Canceled.
	OnTaskPending(ctx context.Context, ti ActiveTask) error

	// Handles the Running state of a task. The task will remain Running until
	// this function returns.
	// The state of the task is transitioned depending on the return value:
	// - If the function returns nil, the task will be transitioned to Completed.
	// - If the function returns an error other than ctx.Err(), the task will be
	//   transitioned to Failed.
	// - If the function returns ctx.Err(), the task will be transitioned to
	//   Canceled. Implementations should be careful not to return ctx.Err()
	//   unless the task can actually be resumed. Resumed tasks will have their
	//   status persisted, so any cancelable task implementation should always
	//   check the task's status as soon as this function is called.
	OnTaskRunning(ctx context.Context, ti ActiveTask) error

	// Called when a task is moved to one of several end states - Completed,
	// Failed, or Canceled.
	OnTaskCompleted(ctx context.Context, ti ActiveTask, state State, args ...any)
}

Jump to

Keyboard shortcuts

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