Documentation
¶
Index ¶
- Variables
- type Group
- type GroupOption
- type Manager
- func (m *Manager) GetRunningTasks() []string
- func (m *Manager) GetTaskCount() int
- func (m *Manager) IsRunning(name string) bool
- func (m *Manager) StartTask(ctx context.Context, name string, task Task) error
- func (m *Manager) StopAll(ctx context.Context) error
- func (m *Manager) StopTask(ctx context.Context, name string) error
- func (m *Manager) Wait() error
- type ManagerOption
- type Task
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
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 ¶
Identifier returns the group's identifier for logging and debugging purposes.
func (*Group) IsStarted ¶
IsStarted reports whether the group has entered its lifecycle. It remains true once Start has been invoked successfully.
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 ¶
GetRunningTasks returns a slice of names of all currently running tasks.
func (*Manager) GetTaskCount ¶
GetTaskCount returns the number of currently running tasks.
func (*Manager) StartTask ¶
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 ¶
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 ¶
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).
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.