engine

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidTaskID = fmt.Errorf("task ID must be a valid UUID")

ErrInvalidTaskID is returned when a caller-provided task ID is not a valid UUID.

Functions

func ValidateCron

func ValidateCron(expr string) error

ValidateCron checks whether a cron expression is syntactically valid.

Types

type Engine

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

Engine is the task executor. Every submitted task runs in its own goroutine. Scheduled tasks are re-fired by EvalSchedules on their cron.

func NewEngine

func NewEngine(ctx context.Context, handlers map[TaskType]TaskHandler) *Engine

NewEngine creates a new Engine. The engine runs until ctx is cancelled.

func (*Engine) EvalSchedules

func (e *Engine) EvalSchedules()

EvalSchedules checks all scheduled tasks and fires any that are due.

func (*Engine) GetResult

func (e *Engine) GetResult(id string) *TaskResult

GetResult returns a task by ID (active, completed, or scheduled), or nil.

func (*Engine) Healthz

func (e *Engine) Healthz() bool

Healthz returns true after the engine has been marked ready.

func (*Engine) RecentResults

func (e *Engine) RecentResults() []TaskResult

RecentResults returns the most recent task results, combining active tasks, completed results, and scheduled tasks.

func (*Engine) RemoveResult

func (e *Engine) RemoveResult(id string) bool

RemoveResult removes a task by ID. For active tasks this cancels the goroutine's context. For scheduled tasks this stops the schedule. Returns true if found.

func (*Engine) Status

func (e *Engine) Status() StatusResponse

Status returns the engine's current state.

func (*Engine) Submit

func (e *Engine) Submit(task Task) (string, error)

Submit starts a task in its own goroutine and returns its ID. When task.ID is set, it becomes the canonical identifier (enabling deterministic IDs from the controller). When empty, a random UUID is generated. If a task with the same ID already exists (active or completed), the existing ID is returned without re-submitting.

func (*Engine) SubmitScheduled

func (e *Engine) SubmitScheduled(task Task, sched ScheduleConfig) (string, error)

SubmitScheduled creates a scheduled task. The schedule is evaluated by EvalSchedules. Only cron schedules are supported today. When task.ID is set, it becomes the canonical identifier. If a task with the same ID already exists, the existing ID is returned without re-registering.

type ScheduleConfig added in v0.0.17

type ScheduleConfig struct {
	Cron        string `json:"cron,omitempty"`
	BlockHeight *int64 `json:"blockHeight,omitempty"`
}

ScheduleConfig triggers a task on a recurring basis. Cron is supported today; BlockHeight is reserved for future use.

type StatusResponse

type StatusResponse struct {
	Status string `json:"status"`
}

StatusResponse is the shape returned by the status endpoint.

type Task

type Task struct {
	ID     string         `json:"id,omitempty"`
	Type   TaskType       `json:"type"`
	Params map[string]any `json:"params,omitempty"`
}

Task is a unit of work submitted by the controller. When ID is set, the engine uses it as the canonical task identifier (enabling deterministic IDs from the controller). When empty, the engine generates a random UUID.

type TaskHandler

type TaskHandler func(ctx context.Context, params map[string]any) error

TaskHandler executes a specific task type.

type TaskResult

type TaskResult struct {
	ID          string          `json:"id"`
	Type        string          `json:"type"`
	Status      TaskStatus      `json:"status"`
	Params      map[string]any  `json:"params,omitempty"`
	Schedule    *ScheduleConfig `json:"schedule,omitempty"`
	Error       string          `json:"error,omitempty"`
	SubmittedAt time.Time       `json:"submittedAt"`
	CompletedAt *time.Time      `json:"completedAt,omitempty"`
	NextRunAt   *time.Time      `json:"nextRunAt,omitempty"`
}

TaskResult records a task and its outcome. All tasks share this model. When Schedule is non-nil the engine re-executes the task on its cron.

type TaskStatus added in v0.0.15

type TaskStatus string

TaskStatus represents the lifecycle state of a task.

const (
	TaskStatusRunning   TaskStatus = "running"
	TaskStatusCompleted TaskStatus = "completed"
	TaskStatusFailed    TaskStatus = "failed"
)

type TaskType

type TaskType string

TaskType identifies the kind of task to execute.

const (
	TaskSnapshotRestore          TaskType = "snapshot-restore"
	TaskDiscoverPeers            TaskType = "discover-peers"
	TaskConfigPatch              TaskType = "config-patch"
	TaskConfigApply              TaskType = "config-apply"
	TaskConfigValidate           TaskType = "config-validate"
	TaskConfigReload             TaskType = "config-reload"
	TaskMarkReady                TaskType = "mark-ready"
	TaskConfigureGenesis         TaskType = "configure-genesis"
	TaskConfigureStateSync       TaskType = "configure-state-sync"
	TaskSnapshotUpload           TaskType = "snapshot-upload"
	TaskResultExport             TaskType = "result-export"
	TaskAwaitCondition           TaskType = "await-condition"
	TaskGenerateIdentity         TaskType = "generate-identity"
	TaskGenerateGentx            TaskType = "generate-gentx"
	TaskUploadGenesisArtifacts   TaskType = "upload-genesis-artifacts"
	TaskAssembleAndUploadGenesis TaskType = "assemble-and-upload-genesis"
	TaskSetGenesisPeers          TaskType = "set-genesis-peers"
)

Jump to

Keyboard shortcuts

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