engine

package
v0.0.7 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBusy = errors.New("a task is already running")

ErrBusy is returned by Submit when a task is already running.

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. One-shot tasks are serialized through a channel with a TryLock gate. Scheduled tasks run in their own goroutines when their cron fires. Both share the unified TaskResult model.

func NewEngine

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

NewEngine creates a new Engine and starts its worker loop. 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. Scheduled tasks run in their own goroutines, bypassing the one-shot lock.

func (*Engine) GetResult

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

GetResult returns a task by ID (one-shot result 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 (newest first), combining completed one-shot results and active scheduled tasks.

func (*Engine) RemoveResult

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

RemoveResult removes a task by ID. 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 enqueues a one-shot task and returns the assigned UUID.

func (*Engine) SubmitScheduled

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

SubmitScheduled creates a scheduled task. Returns the task ID. Currently only cron schedules are supported; block-height scheduling is reserved for future use. The schedule is evaluated by EvalSchedules.

type Schedule

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

Schedule defines when a task should recur. Exactly one field should be set. 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 {
	Type   TaskType       `json:"type"`
	Params map[string]any `json:"params,omitempty"`
}

Task is a unit of work submitted by the controller.

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"`
	Params      map[string]any `json:"params,omitempty"`
	Schedule    *Schedule      `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. Both one-shot and scheduled tasks share this model. Scheduled tasks persist until deleted; one-shot results are kept in a bounded history.

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"
	TaskMarkReady          TaskType = "mark-ready"
	TaskConfigureGenesis   TaskType = "configure-genesis"
	TaskConfigureStateSync TaskType = "configure-state-sync"
	TaskSnapshotUpload     TaskType = "snapshot-upload"
)

Jump to

Keyboard shortcuts

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