agdservice

package
v0.0.0-...-f179113 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

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

type RefresherFunc func(ctx context.Context) (err error)

RefresherFunc is an adapter to allow the use of ordinary functions as Refresher.

func (RefresherFunc) Refresh

func (f RefresherFunc) Refresh(ctx context.Context) (err error)

Refresh implements the Refresher interface for RefresherFunc.

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.

func (*RefresherWithErrColl) Refresh

func (r *RefresherWithErrColl) Refresh(ctx context.Context) (err error)

Refresh implements the Refresher interface for *RefresherWithErrColl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL