Documentation
¶
Index ¶
- type Group
- func (g *Group) Add(tasks ...Task)
- func (s *Group) AutoShutdown()
- func (g *Group) Cancel()
- func (g *Group) Exited() chan struct{}
- func (t *Group) Handle(h func(error) error)
- func (t *Group) IfError(h func(error) error)
- func (s *Group) Shutdown(tasks ...Task)
- func (g *Group) ShutdownOrder(order Order)
- func (g *Group) StartOrder(order Order)
- func (g *Group) Wait(ctx context.Context)
- type Order
- type Task
- func Async(f func() <-chan error) Task
- func AsyncWithContext(f func(ctx context.Context) <-chan error) Task
- func Cancel(cancel context.CancelFunc) Task
- func Sync(f func() error) Task
- func SyncWithContext(f functionSync) Task
- func TaskFunc(f func() Task) Task
- func TaskFuncWithContext(f func(ctx context.Context) Task) Task
- func Wait(event chan struct{}) Task
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group coordinates the lifecycle of multiple Tasks.
A Group has two independent ordering policies:
- StartOrder controls how tasks are started.
- ShutdownOrder controls how task shutdowns are started.
By default, both are Parallel.
A Group can itself be used as a Task, which allows lifecycle hierarchies to be composed of smaller groups.
func NewGroup ¶ added in v1.0.1
NewGroup creates an empty Group and optionally registers the provided tasks.
Tasks are not started until Wait is called.
func (*Group) Add ¶
Add registers one or more tasks.
Registered tasks are started when Wait is called.
func (*Group) AutoShutdown ¶ added in v1.2.0
func (s *Group) AutoShutdown()
AutoShutdown makes normal task completion trigger its shutdown phase.
Without AutoShutdown, Shutdown is normally triggered by context cancellation. With AutoShutdown enabled, returning from Run also cancels the task context, which starts Shutdown.
This is useful for components whose lifetime is defined by the completion of their main operation.
func (*Group) Cancel ¶
func (g *Group) Cancel()
Cancel cancels the Group context.
Cancellation is propagated to all tasks that observe the Group context. For tasks with an autonomous shutdown, cancellation also starts their shutdown phase.
func (*Group) Exited ¶ added in v1.2.0
func (g *Group) Exited() chan struct{}
Exited returns a channel that is closed when the Group has completely finished.
Completion includes running tasks, but NOT shutdown tasks.
func (*Group) Handle ¶ added in v1.0.1
Handle registers an error handler.
A handler may transform an error by returning another error. The transformed error is passed to the owning lifecycle level.
func (*Group) IfError ¶ added in v1.0.1
IfError registers an error handler that is called only for non-nil errors.
func (*Group) Shutdown ¶ added in v1.2.0
func (s *Group) Shutdown(tasks ...Task)
Shutdown registers tasks that form this component's shutdown phase.
Shutdown is represented internally as a Group with Sequential startup. Therefore, shutdown tasks are always started in the order they are registered.
The context passed to the shutdown task is already canceled because Shutdown is triggered by cancellation of the owning task's context.
Use context.WithoutCancel when cleanup requires a usable context.
func (*Group) ShutdownOrder ¶ added in v1.2.0
ShutdownOrder configures how the Group starts task shutdowns.
ShutdownOrder does not change the ordering inside an individual Task's Shutdown group.
func (*Group) StartOrder ¶ added in v1.2.0
StartOrder configures how tasks are started.
type Task ¶ added in v1.0.1
type Task interface {
// IfError registers a handler invoked only for non-nil errors.
IfError(func(error) error)
// Handle registers an error transformation handler.
Handle(func(error) error)
// AutoShutdown makes task completion trigger Shutdown.
AutoShutdown()
// Shutdown registers the component's shutdown tasks.
Shutdown(...Task)
Cancel()
Exited() chan struct{}
Wait(ctx context.Context)
// contains filtered or unexported methods
}
Task represents a unit of work managed by fire.
A Task has a Run phase and an optional Shutdown phase:
Tasks may be nested. Group, TaskFunc, Sync, and Async all implement Task.
func Async ¶ added in v1.0.1
Async creates a Task from an asynchronous function.
The returned error channel represents the lifetime of the Run phase. Closing the channel completes the task.
func AsyncWithContext ¶ added in v1.0.1
AsyncWithContext creates a Task from an asynchronous context-aware function.
func Cancel ¶ added in v1.1.1
func Cancel(cancel context.CancelFunc) Task
Cancel creates a Task that cancels the supplied context.
This is useful as a small lifecycle primitive, for example when a component needs to cancel another context during Shutdown.
func Sync ¶ added in v1.0.1
Sync creates a Task from a synchronous function.
The function is executed once when the Task starts. Returning from the function completes the Run phase.
func SyncWithContext ¶ added in v1.0.1
func SyncWithContext(f functionSync) Task
SyncWithContext creates a Task from a synchronous context-aware function.
The function receives the Task's lifecycle context. The context is canceled when the Task is canceled or its owning Group begins shutdown.
func TaskFunc ¶ added in v1.1.0
TaskFunc creates a Task lazily using a Task factory.
The factory is called when the Task starts. The returned Task becomes the actual Run phase of the created task.
func TaskFuncWithContext ¶ added in v1.1.0
TaskFuncWithContext creates a context-aware lazy Task.
The factory receives the Task's lifecycle context and may use it to construct a context-dependent child Task.