Documentation ¶
Overview ¶
Package agdservice defines types and interfaces for long-running services.
TODO(a.garipov): Move more to golibs.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RefreshWorker ¶
type RefreshWorker struct {
// contains filtered or unexported fields
}
RefreshWorker is an [Interface] implementation that updates its Refresher every tick of the provided ticker.
func NewRefreshWorker ¶
func NewRefreshWorker(c *RefreshWorkerConfig) (w *RefreshWorker)
NewRefreshWorker returns a new valid *RefreshWorker with the provided parameters. c must not be nil.
func (*RefreshWorker) Shutdown ¶
func (w *RefreshWorker) Shutdown(ctx context.Context) (err error)
Shutdown implements the service.Interface interface for *RefreshWorker.
NOTE: The context provided by [RefreshWorkerConfig.Context] is not used for the shutdown refresh.
func (*RefreshWorker) Start ¶
func (w *RefreshWorker) Start(_ context.Context) (err error)
Start implements the service.Interface interface for *RefreshWorker. err is always nil.
type RefreshWorkerConfig ¶
type RefreshWorkerConfig struct { // Context is used to provide a context for the Refresh method of Refresher. // // NOTE: It is not used for the shutdown refresh. // // TODO(a.garipov): Consider ways of fixing that. Context func() (ctx context.Context, cancel context.CancelFunc) // Refresher is the entity being refreshed. Refresher Refresher // Logger is used for logging the operation of the worker. Logger *slog.Logger // Interval is the refresh interval. Must be greater than zero. // // TODO(a.garipov): Consider switching to an interface à la // github.com/robfig/cron/v3.Schedule. Interval time.Duration // RefreshOnShutdown, if true, instructs the worker to call the Refresher's // Refresh method before shutting down the worker. This is useful for items // that should persist to disk or remote storage before shutting down. RefreshOnShutdown bool // RandomizeStart, if true, instructs the worker to sleep before starting a // refresh. The duration of the sleep is a random duration of up to 10 % of // Interval. // // TODO(a.garipov): Switch to something like a cron schedule and see if this // is still necessary RandomizeStart bool }
RefreshWorkerConfig is the configuration structure for a *RefreshWorker.
type Refresher ¶
type Refresher interface { // Refresh is called by a [RefreshWorker]. The error returned by Refresh is // only returned from [RefreshWorker.Shutdown] and only when // [RefreshWorkerConfig.RefreshOnShutdown] is true. In all other cases, the // error is ignored, and refreshers must handle error reporting themselves. Refresh(ctx context.Context) (err error) }
Refresher is the interface for entities that can update themselves.
type RefresherFunc ¶
RefresherFunc is an adapter to allow the use of ordinary functions as Refresher.
type RefresherWithErrColl ¶
type RefresherWithErrColl struct {
// contains filtered or unexported fields
}
RefresherWithErrColl reports all refresh errors to errColl and logs them using a provided logging function.
func NewRefresherWithErrColl ¶
func NewRefresherWithErrColl( refr Refresher, logger *slog.Logger, errColl errcoll.Interface, prefix string, ) (wrapped *RefresherWithErrColl)
NewRefresherWithErrColl wraps refr into a refresher that collects errors and logs them.