task

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGroupAlreadyStarted indicates the group has already entered its run lifecycle.
	ErrGroupAlreadyStarted = errors.New("task group already started")
	// ErrGroupAlreadyStopped indicates the group has already completed its lifecycle.
	ErrGroupAlreadyStopped = errors.New("task group already stopped")
	// ErrGroupNotStarted indicates the group has not been started yet.
	ErrGroupNotStarted = errors.New("task group not started")
)
View Source
var (
	ErrTaskAlreadyExists = errors.New("task already exists")
	ErrTaskNotFound      = errors.New("task not found")
)

Functions

This section is empty.

Types

type Group

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

Group manages the lifecycle of multiple tasks as a coordinated unit. It ensures all tasks start together and provides graceful shutdown capabilities. The group implements the Task interface, allowing it to be nested within other groups.

func NewGroup

func NewGroup(tasks ...Task) *Group

NewGroup creates a new task group with the provided tasks. All tasks will be managed together with coordinated startup and shutdown.

func NewGroupWithOptions added in v0.0.3

func NewGroupWithOptions(tasks []Task, options ...GroupOption) *Group

NewGroupWithOptions creates a task group with explicit options.

func (*Group) Identifier

func (g *Group) Identifier() string

Identifier returns the group's identifier for logging and debugging purposes.

func (*Group) IsStarted

func (g *Group) IsStarted() bool

IsStarted reports whether the group has entered its lifecycle. It remains true once Start has been invoked successfully.

func (*Group) IsStopped

func (g *Group) IsStopped() bool

IsStopped reports whether the group has fully completed shutdown.

func (*Group) Start

func (g *Group) Start(ctx context.Context) error

Start begins all tasks in the group concurrently. If any task fails to start, all other tasks will be stopped. Returns an error if the group is already started/stopped or if any task fails.

func (*Group) Stop

func (g *Group) Stop(ctx context.Context) error

Stop gracefully shuts down all tasks in the group. It blocks until shutdown completes or the provided context expires. Returns ErrGroupNotStarted when called before Start.

type GroupOption added in v0.0.3

type GroupOption func(*groupOptions)

GroupOption customizes group runtime behavior.

func WithAutoStopTimeout added in v0.0.3

func WithAutoStopTimeout(timeout time.Duration) GroupOption

WithAutoStopTimeout configures the timeout used by internal auto-stop cleanup. It affects cleanup triggered from Start (task failure, parent cancellation, or manual stop signal). A non-positive duration disables timeout and uses context.Background().

type Manager

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

Manager provides dynamic management of named tasks with concurrent execution. It allows starting, stopping, and monitoring individual tasks by name, offering more flexibility than the Group type for long-running applications.

func NewManager

func NewManager(options ...ManagerOption) *Manager

NewManager creates a new task manager with no initial tasks.

func (*Manager) GetRunningTasks

func (m *Manager) GetRunningTasks() []string

GetRunningTasks returns a slice of names of all currently running tasks.

func (*Manager) GetTaskCount

func (m *Manager) GetTaskCount() int

GetTaskCount returns the number of currently running tasks.

func (*Manager) IsRunning

func (m *Manager) IsRunning(name string) bool

IsRunning checks if a task with the given name is currently running.

func (*Manager) StartTask

func (m *Manager) StartTask(ctx context.Context, name string, task Task) error

StartTask starts a new task with the given name. Returns ErrTaskAlreadyExists if a task with the same name is already running. The task runs in its own goroutine and can be stopped individually using StopTask. The provided ctx becomes the parent context of this task's run context.

func (*Manager) StopAll

func (m *Manager) StopAll(ctx context.Context) error

StopAll stops all currently running tasks concurrently. It waits for all tasks to complete shutdown before returning. If the caller ctx expires first, StopAll returns ctx.Err(), but background stops continue. The task.Stop calls use manager-level stop timeout policy (WithManagerAutoStopTimeout). Returns any errors encountered during shutdown and previously collected task run errors.

func (*Manager) StopTask

func (m *Manager) StopTask(ctx context.Context, name string) error

StopTask stops a running task by name. Returns ErrTaskNotFound if no task with the given name is running. It waits for both Stop and Start goroutines to finish. If the caller ctx expires first, StopTask returns ctx.Err(), but internal stopping continues in background. The task.Stop call itself uses manager-level stop timeout policy (WithManagerAutoStopTimeout).

func (*Manager) Wait

func (m *Manager) Wait() error

Wait blocks until all started task goroutines have exited. It returns joined task run errors and stop errors collected by the manager.

type ManagerOption added in v0.0.3

type ManagerOption func(*managerOptions)

ManagerOption customizes manager runtime behavior.

func WithManagerAutoStopTimeout added in v0.0.3

func WithManagerAutoStopTimeout(timeout time.Duration) ManagerOption

WithManagerAutoStopTimeout configures timeout for internal task stop operations. This timeout is used by manager-triggered stop flows and is independent of caller wait context. A non-positive duration disables timeout and uses context.Background().

type Task

type Task interface {
	// Identifier returns a unique identifier for this task.
	// This is used for logging and debugging purposes.
	Identifier() string

	// Start begins the task's operation with the given context.
	// The task should monitor the context for cancellation and stop gracefully when cancelled.
	// Returns an error if the task fails to start.
	Start(ctx context.Context) error

	// Stop gracefully shuts down the task with the given context.
	// The context may have a deadline for shutdown completion.
	// Returns an error if the task fails to stop cleanly.
	Stop(ctx context.Context) error
}

Task defines the interface for lifecycle-managed components in the application. Tasks can be started and stopped with context support for graceful shutdown. This interface is commonly used for services, servers, workers, and other background operations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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