Documentation
¶
Index ¶
- Variables
- func RunnerInvoke[T runner]() fx.Option
- func TaskInvoke[T task]() fx.Option
- func WorkerInvoke[T worker]() fx.Option
- type App
- func (app *App) Done() <-chan struct{}
- func (app *App) Err() error
- func (app *App) InitFxApp() *fx.App
- func (app *App) Run(ctx context.Context) (err error)
- func (app *App) Shutdown(ctx context.Context) (err error)
- func (app *App) ShutdownCause(ctx context.Context, cause error) error
- func (app *App) Start(ctx context.Context) (err error)
- func (app *App) Validate() error
- func (app *App) WithOpts(opts ...fx.Option) *App
- type Config
- type RunnerFunc
- type Shutdowner
Constants ¶
This section is empty.
Variables ¶
var ErrAppAlreadyStarted = errors.New("app already started")
Functions ¶
func RunnerInvoke ¶
RunnerInvoke creates a fx.Option that integrates a long-running process into the application's lifecycle. It accepts a generic type T that must implement the runner interface (a Run(ctx) method). OnStart, it launches the Run method in a separate goroutine. OnStop, it cancels the context passed to the Run method and waits for it to finish. When the Run method completes, it triggers a graceful shutdown of the entire application.
func TaskInvoke ¶
TaskInvoke creates a fx.Option that integrates a task into the application's lifecycle. It accepts a generic type T that must implement the task interface (Run/Shutdown methods). OnStart, it launches the Run method in a separate goroutine. OnStop, it calls the Shutdown method and waits for the Run method to complete. When the Run method completes, it triggers a graceful shutdown of the entire application.
func WorkerInvoke ¶
WorkerInvoke creates a fx.Option that integrates a worker process into the application's lifecycle. It accepts a generic type T that must implement the worker interface (Start/Shutdown/Done/Err methods). OnStart, it launches the Start method and then monitors Done() in a separate goroutine. OnStop, it calls Shutdown method of the worker. If Err() reports non-nil error when Done() closes, it triggers a graceful shutdown of the entire application.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is an application wrapper that manages the lifecycle of a fx-based application. It handles starting, running, and gracefully shutting down the application. The application can be in one of three states: stopped, starting, or started.
func (*App) Done ¶
func (app *App) Done() <-chan struct{}
Done returns a channel that is closed after the application has fully stopped (all fx stop hooks have completed).
func (*App) Err ¶
Err returns the first non-nil error that caused or occurred during shutdown. It is nil if the application stopped cleanly.
func (*App) InitFxApp ¶
InitFxApp constructs (but does not start) a new fx.App using the accumulated options. This is primarily useful for tests or advanced customization.
func (*App) Run ¶
Run starts the application and blocks until it stops, either due to an internal error or context cancellation. It returns any start/stop error or the first error that triggered shutdown.
func (*App) Shutdown ¶
Shutdown requests a graceful stop of the application and waits for it to finish or for ctx to time out. Equivalent to ShutdownCause(ctx, nil).
func (*App) ShutdownCause ¶
ShutdownCause requests a graceful stop and records the provided cause as the terminal error returned by Err(). If a start is in progress, it first waits for it to complete. If the app is not started, it returns nil. The call blocks until the app finishes stopping or ctx is done.
func (*App) Start ¶
Start builds and starts the underlying fx.App if it is not already started. If a start is already in progress, Start blocks until it completes. If the app is already started, Start is a no-op and returns nil. The method returns quickly and runs shutdown monitoring in the background; use Done() to wait for the app to stop.
type Config ¶
type Config struct {
// StartTimeout is the maximum duration to wait for the application's services to start.
StartTimeout time.Duration `envconfig:"APP_START_TIMEOUT" default:"10s"`
// ShutdownTimeout is the maximum duration to wait for the application's services to shut down gracefully.
ShutdownTimeout time.Duration `envconfig:"APP_SHUTDOWN_TIMEOUT" default:"10s"`
}
type RunnerFunc ¶
type Shutdowner ¶
type Shutdowner interface {
// Shutdown requests a graceful stop and waits until complete or ctx timeout.
Shutdown(ctx context.Context) error
// ShutdownCause requests a graceful stop and records err as the terminal cause.
ShutdownCause(ctx context.Context, err error) error
}
Shutdowner allows any component to request a graceful application shutdown.