manager

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Manager is an in-memory registry of tasks: Add one to get back a UUID, then Start, Cancel or Remove it by that UUID.

Add, Start, Cancel, Wait and Remove all require Run to be actively running - the entry bookkeeping they touch is only meaningful while Run's shutdown-cancellation loop is watching it - and return an error otherwise.

func New

func New(ctx context.Context, opts ...Opt) (_ *Manager, err error)

New creates a new task manager

func (*Manager) Add

func (m *Manager) Add(ctx context.Context, name string, task schema.Task) (_ uuid.UUID, err error)

Add registers task under name and returns its UUID. The task isn't run until Start is called with that UUID.

func (*Manager) Cancel

func (m *Manager) Cancel(ctx context.Context, id uuid.UUID) (err error)

Cancel stops the running task registered under id. It's a no-op if the task has already finished or was never started.

func (*Manager) GetTask

func (m *Manager) GetTask(ctx context.Context, id uuid.UUID) (_ *schema.Status, err error)

GetTask returns a snapshot of the task registered under id.

func (*Manager) ListTasks

func (m *Manager) ListTasks(ctx context.Context, req schema.TaskListRequest) (_ *schema.TaskList, err error)

ListTasks returns a snapshot of the tasks matching req, in the order they were added.

func (*Manager) Ready

func (m *Manager) Ready() <-chan struct{}

Ready returns a channel that's closed once Run has started - Add, Start, Cancel, Wait and Remove aren't usable until then.

func (*Manager) Remove

func (m *Manager) Remove(ctx context.Context, id uuid.UUID) (err error)

Remove unregisters the task registered under id, so it's no longer tracked by the manager. It fails if the task is still running - Cancel it and Wait for it to finish before removing it.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context, log *slog.Logger) error

Run the task manager until ctx is cancelled, at which point it cancels every task still running and waits for each to finish (i.e. for its Run goroutine to return) or for ctx to be done a second time (e.g. a shutdown deadline), whichever comes first. Run refuses to run a second time - once it returns (or while it's still running), calling it again just returns an error rather than panicking or restarting anything.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, id uuid.UUID) (err error)

Start runs the task registered under id in a new goroutine, using ctx as the parent for cancellation and tracing, and returns immediately - use Cancel to stop the task, and Wait to block until it finishes.

func (*Manager) Subscribe

func (m *Manager) Subscribe(ctx context.Context, fn func(*schema.Event)) error

Subscribe registers fn to be called for every event a task emits - Add, Start, a task reporting progress or a result, Cancel, a task's Run goroutine returning, and Remove (see schema.Event). It blocks until ctx is done or the Manager itself stops (its own Run returns), whichever comes first, at which point fn is unregistered and Subscribe returns.

fn is called synchronously from whichever goroutine made the change, so it must not block or call back into the Manager other than to Subscribe/ unsubscribe.

func (*Manager) Wait

func (m *Manager) Wait(ctx context.Context, id uuid.UUID) (_ *schema.Status, err error)

Wait blocks until the task registered under id finishes, or until ctx is done, whichever comes first, then returns its final status. It returns an error if the task hasn't been started (there's nothing to wait for), if ctx ends the wait first (no status is returned in that case, since the task may still be running), or if the task itself returned an error (its status is still returned alongside that error).

type Opt

type Opt func(*opt) error

Opt is a functional option for task manager configuration.

func WithTracer

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer used for tracing operations.

Jump to

Keyboard shortcuts

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