Documentation
¶
Index ¶
- Variables
- type Group
- 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 Task
Constants ¶
This section is empty.
Variables ¶
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 (*Group) Identifier ¶
Identifier returns the group's identifier for logging and debugging purposes.
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() *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 concurrently and can be stopped individually using StopTask.
func (*Manager) StopAll ¶
StopAll stops all currently running tasks concurrently. It waits for all tasks to complete shutdown before returning. Returns any errors encountered during the shutdown process.
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.