Documentation
¶
Index ¶
- Constants
- Variables
- type CancelReason
- type Middleware
- type Service
- type Task
- type TaskFunc
- type TaskManager
- func (tm *TaskManager) CancelAll()
- func (tm *TaskManager) CancelTask(id uuid.UUID)
- func (tm *TaskManager) ExecuteTask(id uuid.UUID) (interface{}, error)
- func (tm *TaskManager) GetResults() <-chan interface{}
- func (tm *TaskManager) GetTask(id uuid.UUID) (task Task, ok bool)
- func (tm *TaskManager) GetTasks() []Task
- func (tm *TaskManager) RegisterTask(tasks ...Task)
- func (tm *TaskManager) Start(numWorkers int)
- func (tm *TaskManager) Stop()
Constants ¶
const ( // ContextDeadlineReached means the context is past its deadline. ContextDeadlineReached = CancelReason(1) // RateLimited means the number of concurrent tasks per second exceeded the maximum allowed. RateLimited = CancelReason(2) // Cancelled means `CancelTask` was invked and the `Task` was cancelled. Cancelled = CancelReason(3) )
CancelReason values
- 1: `ContextDeadlineReached`
- 2: `RateLimited`
- 3: `Cancelled`
Variables ¶
var ( // ErrInvalidTaskID is returned when a task has an invalid ID ErrInvalidTaskID = errors.New("invalid task id") // ErrInvalidTaskFunc is returned when a task has an invalid function ErrInvalidTaskFunc = errors.New("invalid task function") // ErrTaskNotFound is returned when a task is not found ErrTaskNotFound = errors.New("task not found") )
Functions ¶
This section is empty.
Types ¶
type CancelReason ¶ added in v0.0.2
type CancelReason uint8
CancelReason is a value used to represent the cancel reason.
type Middleware ¶
Middleware describes a `Service` middleware.
type Service ¶
type Service interface {
// RegisterTask registers a new task to the worker
RegisterTask(tasks ...Task)
// Start the task manager
Start(numWorkers int)
// Stop the task manage
Stop()
// GetResults gets the results channel
GetResults() <-chan interface{}
// GetTask gets a task by its ID
GetTask(id uuid.UUID) (task Task, ok bool)
// GetTasks gets all tasks
GetTasks() []Task
// ExecuteTask executes a task given its ID and returns the result
ExecuteTask(id uuid.UUID) (interface{}, error)
// CancelAll cancels all tasks
CancelAll()
// CancelTask cancels a task by its ID
CancelTask(id uuid.UUID)
}
Service is an interface for a task manager
func NewTaskManager ¶
NewTaskManager creates a new task manager
- `maxTasks` is the maximum number of tasks that can be executed at once, defaults to 1
- `tasksPerSecond` is the rate limit of tasks that can be executed per second, defaults to 1
func RegisterMiddleware ¶
func RegisterMiddleware(svc Service, mw ...Middleware) Service
RegisterMiddleware registers middlewares to the `Service`.
type Task ¶
type Task struct {
ID uuid.UUID `json:"id"` // ID is the id of the task
Priority int `json:"priority"` // Priority is the priority of the task
Fn TaskFunc `json:"-"` // Fn is the function that will be executed by the task
Ctx context.Context `json:"context"` // Ctx is the context of the task
Cancel context.CancelFunc `json:"-"` // Cancel is the cancel function of the task
Error atomic.Value `json:"error"` // Error is the error of the task
Started atomic.Int64 `json:"started"` // Started is the time the task started
Completed atomic.Int64 `json:"completed"` // Completed is the time the task completed
Cancelled atomic.Int64 `json:"cancelled"` // Cancelled is the time the task was cancelled
CancelReason CancelReason `json:"cancel_reason"` // CancelReason is the reason the task was cancelled
// contains filtered or unexported fields
}
Task represents a function that can be executed by the task manager
type TaskFunc ¶ added in v0.0.2
type TaskFunc func() interface{}
TaskFunc signature of `Task` function
type TaskManager ¶
type TaskManager struct {
Registry sync.Map // Registry is a map of registered tasks
Results chan interface{} // Results is the channel of results
// contains filtered or unexported fields
}
TaskManager is a struct that manages a pool of goroutines that can execute tasks
func (*TaskManager) CancelTask ¶
func (tm *TaskManager) CancelTask(id uuid.UUID)
CancelTask cancels a task by its ID
func (*TaskManager) ExecuteTask ¶ added in v0.0.2
func (tm *TaskManager) ExecuteTask(id uuid.UUID) (interface{}, error)
ExecuteTask executes a task given its ID and returns the result
func (*TaskManager) GetResults ¶
func (tm *TaskManager) GetResults() <-chan interface{}
GetResults gets the results channel
func (*TaskManager) GetTask ¶
func (tm *TaskManager) GetTask(id uuid.UUID) (task Task, ok bool)
GetTask gets a task by its ID
func (*TaskManager) RegisterTask ¶
func (tm *TaskManager) RegisterTask(tasks ...Task)
RegisterTask registers a new task to the task manager
func (*TaskManager) Start ¶
func (tm *TaskManager) Start(numWorkers int)
Start starts the task manager and its goroutines
- `numWorkers` is the number of workers to start, if not specified, the number of CPUs will be used
func (*TaskManager) Stop ¶
func (tm *TaskManager) Stop()
Stop stops the task manager and its goroutines