Documentation
¶
Overview ¶
Package beatfx connects one beat.Beat to an Fx lifecycle. Module requires beat.Config and a *job.Runner. beat.Handler and Options are optional. Provide Options once as an ordered slice; static Module options are applied first, followed by the slice. Repeated setter options use their last value.
A ready set can be supplied with fx.Supply(beatfx.Options{...}). A constructor returning Options can use dependencies from the container. The application owns Fx startup and shutdown budgets. Use one Module per Fx application.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Module ¶
Module provides a private Beat and registers its Start and Stop hooks. Static opts are applied before the optional container-provided Options.
Example ¶
package main
import (
"context"
"log/slog"
"time"
"github.com/uchaloop/beat"
"github.com/uchaloop/beatfx"
"github.com/uchaloop/job"
"github.com/uchaloop/job/middleware/recovery"
"go.uber.org/fx"
)
func main() {
app := fx.New(
fx.Supply(beat.Config{Period: time.Minute}),
fx.Supply(slog.Default()),
// The runner carries the work, its middleware and its timeout.
fx.Provide(func(log *slog.Logger) (*job.Runner, error) {
return job.MakeRunner(
job.Config{Timeout: 45 * time.Second},
func(ctx context.Context) (int64, error) { return 0, ctx.Err() },
job.WithMiddleware(recovery.Middleware(recovery.WithLogger(log))),
)
}),
fx.Provide(func() beatfx.Options {
return beatfx.Options{beat.WithGracefulStop()}
}),
beatfx.Module(),
fx.StopTimeout(time.Minute),
fx.NopLogger,
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := app.Start(ctx); err != nil {
panic(err)
}
if err := app.Stop(ctx); err != nil {
panic(err)
}
}
Output:
Types ¶
type Options ¶
Options is the ordered set of beat options an application builds from the container. Provide it once, and the order inside it is the order applied:
fx.Provide(func(db *sql.DB) beatfx.Options {
return beatfx.Options{beat.WithOnStart(db.PingContext)}
})
A set that needs nothing from the container can be supplied outright with fx.Supply(beatfx.Options{...}), though such options can also go straight to Module.