Documentation ¶
Overview ¶
Package task is an utility package for simplifying execution of tasks.
Index ¶
- Variables
- type ConcurrentGroup
- type Context
- type Func
- type SerialGroup
- type StandardTask
- func (st *StandardTask) AddChild(child Task)
- func (st *StandardTask) Cancel(err error)
- func (st *StandardTask) Cancelled() <-chan struct{}
- func (st *StandardTask) Err() error
- func (st *StandardTask) Finish() error
- func (st *StandardTask) Finished() <-chan struct{}
- func (st *StandardTask) SetDeadline(t time.Time)
- func (st *StandardTask) SetSignals(s ...os.Signal)
- func (st *StandardTask) SetTimeout(d time.Duration)
- func (st *StandardTask) Start() error
- func (st *StandardTask) Started() <-chan struct{}
- func (st *StandardTask) Wait() error
- type Task
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTaskCancelled happens when the task can be cancelled (it is in a // cancellable state) and is cancelled. ErrTaskCancelled = errors.New("task cancelled") // ErrTaskDeadlineExceeded happens when a timeout or deadline is defined for // the task, and it is triggered before the task finishes executing. ErrTaskDeadlineExceeded = errors.New("task deadline exceeded") // ErrTaskSignalReceived happens when one or more signals defined in the task // are triggered by the OS, before the task finishes executing. ErrTaskSignalReceived = errors.New("task received signal") // ErrInvalidTaskState happens when the task tries to do to an invalid state // transition (eg., starting or finishing the same task twice, etc...). ErrInvalidTaskState = errors.New("invalid task state") )
Functions ¶
This section is empty.
Types ¶
type ConcurrentGroup ¶
type ConcurrentGroup struct { *StandardTask // MaxConcurrency is a limit to the maximum number of goroutines that can // execute simultaneously. If you set it above 0 this limit is enforced, // otherwise it is unlimited. By default, it is set to 0 (unlimited). MaxConcurrency int }
ConcurrentGroup is a task that runs its sub-tasks concurrently. ConcurrentGroup implements the Task interface so you can compose it with other tasks.
func NewConcurrentGroup ¶
func NewConcurrentGroup() *ConcurrentGroup
NewConcurrentGroup creates a new ConcurrentGroup.
func (*ConcurrentGroup) Run ¶
func (g *ConcurrentGroup) Run(ctx Context)
Run runs all tasks concurrently, according to the maximum concurrency setting, and waits until all of them are done. On the first error returned by one of the child tasks, the group will be cancelled. If, in any way, the group is cancelled, no more child tasks will be run. This function will always wait until no more child tasks in the group are running.
type Context ¶
type Context interface { // Get gets the value associated to the given key, or nil if there is none. Get(key string) interface{} // GetOk gets the value, and a boolean indicating the existence // of such key. When the key exists (value, true) will be returned, otherwise // (nil, false) will be returned. GetOk(key string) (interface{}, bool) // Set sets the given value in the given key. Set(key string, value interface{}) // Delete deletes the given key. Delete(key string) }
Context is an interface for helping you share data between tasks.
type SerialGroup ¶
type SerialGroup struct {
*StandardTask
}
SerialGroup is a task that runs its sub-tasks serially (in sequence). SerialGroup implements the Task interface so you can compose it with other tasks.
func (*SerialGroup) Run ¶
func (g *SerialGroup) Run(ctx Context)
Run runs all tasks serially and waits until all of them are done. On the first error returned by one of the tasks, no more child tasks are run, and the group is cancelled allowing currently executing tasks to cleanup and finish.
type StandardTask ¶
type StandardTask struct {
// contains filtered or unexported fields
}
StandardTask is an interface that helps you more easily implement Tasks. StandardTask implements all functionality required by the Task interface, with the exception of the Run function which is not included. This makes it ideal for embedding StandardTask in your struct and then implementing the required Run function.
func NewStandardTask ¶
func NewStandardTask() *StandardTask
NewStandardTask creates a new StandardTask object.
func (*StandardTask) AddChild ¶
func (st *StandardTask) AddChild(child Task)
AddChild adds a child task to this task.
func (*StandardTask) Cancel ¶
func (st *StandardTask) Cancel(err error)
Cancel cancels the task with an error. If nil is passed, the default ErrTaskCancelled is used instead.
func (*StandardTask) Cancelled ¶
func (st *StandardTask) Cancelled() <-chan struct{}
Cancelled returns a channel that's closed when the task is cancelled.
func (*StandardTask) Err ¶
func (st *StandardTask) Err() error
Err returns the error (if any) the task was cancelled with.
func (*StandardTask) Finish ¶
func (st *StandardTask) Finish() error
Finish finishes the task after being used.
func (*StandardTask) Finished ¶
func (st *StandardTask) Finished() <-chan struct{}
Finished returns a channel that's closed when the task is finished.
func (*StandardTask) SetDeadline ¶
func (st *StandardTask) SetDeadline(t time.Time)
SetDeadline sets the time after which the task should be automatically cancelled.
func (*StandardTask) SetSignals ¶
func (st *StandardTask) SetSignals(s ...os.Signal)
SetSignals sets the list of signals that should cancel this task should they be triggered by the OS. Please note that an additional goroutine will be created in order to detect the signals and cancel the task.
func (*StandardTask) SetTimeout ¶
func (st *StandardTask) SetTimeout(d time.Duration)
SetTimeout sets the duration after which the task should be automatically cancelled.
func (*StandardTask) Start ¶
func (st *StandardTask) Start() error
Start prepares the task before being used.
func (*StandardTask) Started ¶
func (st *StandardTask) Started() <-chan struct{}
Started returns a channel that's closed when the task is started.
func (*StandardTask) Wait ¶
func (st *StandardTask) Wait() error
Wait waits for the task to finish executing, and returns the resulting error (if any). Wait also returns immediately if the task is cancelled before even starting execution.
type Task ¶
type Task interface { // AddChild adds a child task to this task. AddChild(child Task) // SetDeadline sets the time after which the task should be // automatically cancelled. SetDeadline(t time.Time) // SetTimeout sets the duration after which the task should be // automatically cancelled. SetTimeout(d time.Duration) // SetSignals sets the list of signals that should cancel this task should // they be triggered by the OS. Please note that an additional goroutine will // be created in order to detect the signals and cancel the task. SetSignals(s ...os.Signal) // Run runs the task. Run(ctx Context) // Start prepares the task before being used. Start() error // Started returns a channel that's closed when the task is started. Started() <-chan struct{} // Cancel cancels the task with an error. If nil is passed, the default // ErrTaskCancelled is used instead. Cancel(error) // Cancelled returns a channel that's closed when the task is cancelled. Cancelled() <-chan struct{} // Finish finishes the task after being used. Finish() error // Finished returns a channel that's closed when the task is finished. Finished() <-chan struct{} // Wait waits for the task to finish executing, and returns the resulting // error (if any). Wait also returns immediately if the task is cancelled // before even starting execution. Wait() error // Err returns the error (if any) the task was cancelled with. Err() error }
Task is an interface that helps you define, run and control a task. You can set deadlines and timeouts, you can cancel it, or wait for the it until it finishes execution or is canceled before starting.
func NewTaskWithFunc ¶
NewTaskWithFunc takes a function and wraps it in a Task.