Documentation
¶
Index ¶
- type Manager
- func (m *Manager) Add(ctx context.Context, name string, task schema.Task) (_ uuid.UUID, err error)
- func (m *Manager) Cancel(ctx context.Context, id uuid.UUID) (err error)
- func (m *Manager) GetTask(ctx context.Context, id uuid.UUID) (_ *schema.Status, err error)
- func (m *Manager) ListTasks(ctx context.Context, req schema.TaskListRequest) (_ *schema.TaskList, err error)
- func (m *Manager) Ready() <-chan struct{}
- func (m *Manager) Remove(ctx context.Context, id uuid.UUID) (err error)
- func (m *Manager) Run(ctx context.Context, log *slog.Logger) error
- func (m *Manager) Start(ctx context.Context, id uuid.UUID) (err error)
- func (m *Manager) Subscribe(ctx context.Context, fn func(*schema.Event)) error
- func (m *Manager) Wait(ctx context.Context, id uuid.UUID) (_ *schema.Status, err error)
- type Opt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
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 (*Manager) Add ¶
Add validates task and, if well-formed, registers it under name and returns its UUID. The task isn't run until Start is called with that UUID.
func (*Manager) Cancel ¶
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) 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 ¶
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 ¶
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 ¶
Start runs the task registered under id in a new goroutine and returns immediately - use Cancel to stop the task, and Wait to block until it finishes. ctx scopes only this call itself (e.g. its otel span); the task's own execution is scoped to Run's ctx instead, so it isn't cut short by the caller's ctx ending (e.g. an HTTP request's context, once that request's handler returns) - Run's own shutdown sequence is what cancels every still-running task, not this one. The task's execution span is still parented under this call's own "Start" span (and transitively whatever ctx carried in, if anything), so it shows up as a normal child in the same trace rather than a separately linked one that's easy to lose track of in a UI - only cancellation is decoupled from ctx, not the trace itself.
func (*Manager) Subscribe ¶
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 ¶
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).