Documentation
¶
Overview ¶
Package lifecycle implements a simple mechanism for managing a lifetime of an application. It provides a way to register functions that will be called when the application is about to exit. It distinguishes between starting, running and teardown phases.
The application is considered to be starting before calling Life.Run(). You can use Life.StartingContext() to get a context that can be used to control the startup phase. Starting context is cancelled when the startup phase is over.
The application is considered to be running after calling Life.Run() and before calling Life.Die(). You can use Life.Context() to get a context that can be used to control the running phase. It is cancelled when the application is about to exit.
The application is considered to be tearing down after calling Life.Die(). You can use Life.TeardownContext() to get a context that can be used to control the teardown phase. It is cancelled when the teardown timeout is reached.
Index ¶
- Variables
- func Async(c *callback)
- func PanicOnError(c *callback)
- func Timeout(timeout time.Duration) exitCallbackOpt
- func WithExitErrorCallback(callback func(error)) opt
- func WithSignal(s1 os.Signal, sMany ...os.Signal) opt
- func WithStartingTimeout(timeout time.Duration) opt
- func WithTeardownTimeout(timeout time.Duration) opt
- type Life
- func (l *Life) Context() context.Context
- func (l *Life) Die(reason error)
- func (l *Life) OnExit(callback func(), exitOpts ...exitCallbackOpt)
- func (l *Life) OnExitWithContext(callback func(context.Context), exitOpts ...exitCallbackOpt)
- func (l *Life) OnExitWithContextError(callback func(context.Context) error, exitOpts ...exitCallbackOpt)
- func (l *Life) OnExitWithError(callback func() error, exitOpts ...exitCallbackOpt)
- func (l *Life) Run() error
- func (l *Life) StartingContext() context.Context
- func (l *Life) TeardownContext() context.Context
Constants ¶
This section is empty.
Variables ¶
var ErrSignaled = fmt.Errorf("received signal")
Functions ¶
func Async ¶
func Async(c *callback)
Async sets the callback to be executed in a separate goroutine.
func PanicOnError ¶
func PanicOnError(c *callback)
PanicOnError sets the callback to panic with the error returned by the callback.
func WithExitErrorCallback ¶
func WithExitErrorCallback(callback func(error)) opt
WithExitErrorCallback sets the callback that will be called when an error occurs during the teardown phase.
func WithSignal ¶
WithSignal calls Life.Die() when the specified signal is received.
func WithStartingTimeout ¶
WithStartingTimeout sets the timeout for the starting phase.
func WithTeardownTimeout ¶
WithTeardownTimeout sets the timeout for the teardown phase.
Types ¶
type Life ¶
type Life struct {
// contains filtered or unexported fields
}
func (*Life) Context ¶
Context returns a context that will be cancelled when the application is about to exit. It can be used to control the lifetime of the application. It will be cancelled after calling Life.Die().
func (*Life) OnExit ¶
func (l *Life) OnExit(callback func(), exitOpts ...exitCallbackOpt)
OnExit registers a callback that will be called when the application is about to exit. Has no effect after calling Life.Run(). Use life.Async option to execute the callback in a separate goroutine. life.PanicOnError has no effect on this function.
func (*Life) OnExitWithContext ¶
OnExitWithContext registers a callback that will be called when the application is about to exit. Has no effect after calling Life.Run(). The callback will receive a context that will be cancelled after the teardown timeout. Use life.Async option to execute the callback in a separate goroutine. life.PanicOnError has no effect on this function.
func (*Life) OnExitWithContextError ¶
func (l *Life) OnExitWithContextError(callback func(context.Context) error, exitOpts ...exitCallbackOpt)
OnExitWithContextError registers a callback that will be called when the application is about to exit. Has no effect after calling Life.Run(). The callback will receive a context that will be cancelled after the teardown timeout. The callback can return an error that will be passed to the error handler. Use life.Async option to execute the callback in a separate goroutine. Use life.PanicOnError to panic with the error returned by the callback.
func (*Life) OnExitWithError ¶
OnExitWithError registers a callback that will be called when the application is about to exit. Has no effect after calling Life.Run(). The callback can return an error that will be passed to the error handler. Use life.Async option to execute the callback in a separate goroutine. Use life.PanicOnError to panic with the error returned by the callback.
func (*Life) Run ¶
Run starts the application. It will block until the application is stopped by calling Die. It will also block until all the registered callbacks are executed. If the teardown timeout is set, it will be used to cancel the context passed to the callbacks. Returns the error that caused the application to stop.
func (*Life) StartingContext ¶
StartingContext returns a context that will be cancelled after the starting timeout. It can be used to control the startup of the application. It will be cancelled after calling Life.Run().
func (*Life) TeardownContext ¶
TeardownContext returns a context that will be cancelled after the teardown timeout. It can be used to control the shutdown of the application. This context is the same as the one passed to the callbacks registered with OnExit* methods.