task

package
v0.0.2-beta.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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 (*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 returns whether the group has been started.

func (*Group) IsStopped

func (g *Group) IsStopped() bool

IsStopped returns whether the group has been stopped.

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 cancels the group context, triggering shutdown of all managed tasks. Returns an error if the group was not started, or nil if already stopped.

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

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 concurrently and can be stopped individually using StopTask.

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. Returns any errors encountered during the shutdown process.

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. The task is removed from the manager after successful shutdown.

func (*Manager) Wait

func (m *Manager) Wait() error

Wait blocks until all managed tasks complete execution. Returns any error encountered by the running tasks.

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