task

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package task implements generic controller tasks running in goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EqualSpec

type EqualSpec[T any, S Spec[T]] interface {
	Spec[T]
	Equal(S) bool
}

EqualSpec is like Spec but it requires an Equal method from the spec.

type EqualityFunc

type EqualityFunc[T any] func(x, y T) bool

EqualityFunc is used to compare two task specs.

type ID

type ID = string

ID is a task ID.

type Runner

type Runner[T any, S Spec[T]] struct {
	// contains filtered or unexported fields
}

Runner manages running tasks.

func NewEqualRunner

func NewEqualRunner[S EqualSpec[T, S], T any]() *Runner[T, S]

NewEqualRunner creates a new task runner from spec with Equal method.

func NewRunner

func NewRunner[T any, S Spec[T]](equalityFunc EqualityFunc[S]) *Runner[T, S]

NewRunner creates a new task runner.

func (*Runner[T, S]) Reconcile

func (runner *Runner[T, S]) Reconcile(ctx context.Context, logger *zap.Logger, shouldRun map[ID]S, in T)

Reconcile running tasks.

func (*Runner[T, S]) StartTask

func (runner *Runner[T, S]) StartTask(ctx context.Context, logger *zap.Logger, id string, spec S, task T)

StartTask starts a new task.

func (*Runner[T, S]) Stop

func (runner *Runner[T, S]) Stop()

Stop all running tasks.

func (*Runner[T, S]) StopTask

func (runner *Runner[T, S]) StopTask(logger *zap.Logger, id string)

StopTask stop the running task.

type Spec

type Spec[T any] interface {
	ID() ID
	RunTask(ctx context.Context, logger *zap.Logger, in T) error
}

Spec configures a task.

type Task

type Task[T any, S Spec[T]] struct {
	// contains filtered or unexported fields
}

Task is a generic controller task that can run in a goroutine with restarts and panic handling.

func New

func New[T any, S Spec[T]](logger *zap.Logger, spec S, in T) *Task[T, S]

New creates a new task.

func (*Task[T, S]) Spec

func (task *Task[T, S]) Spec() S

Spec returns the task spec.

func (*Task[T, S]) Start

func (task *Task[T, S]) Start(ctx context.Context)

Start the task in a separate goroutine.

func (*Task[T, S]) Stop

func (task *Task[T, S]) Stop()

Stop the task waiting for it to finish.

Jump to

Keyboard shortcuts

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