agdservice

package
v0.0.0-...-b6e3791 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: AGPL-3.0 Imports: 7 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.

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.
	Context func() (ctx context.Context, cancel context.CancelFunc)

	// Refresher is the entity being refreshed.
	Refresher Refresher

	// ErrColl is used to collect errors during refreshes.
	//
	// TODO(a.garipov): Remove this and make all Refreshers handle their own
	// errors.
	ErrColl errcoll.Interface

	// Name is the name of this worker.  It is used for logging and error
	// collecting.
	//
	// TODO(a.garipov): Consider accepting a slog.Logger or removing this and
	// making all Refreshers handle their own logging.
	Name string

	// 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

	// RoutineLogsAreDebug, if true, instructs the worker to write initial and
	// final log messages for each singular refresh on the Debug level rather
	// than on the Info one.  This is useful to prevent routine logs from
	// workers with a small interval from overflowing with messages.
	RoutineLogsAreDebug 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(ctx context.Context) (err error)
}

Refresher is the interface for entities that can update themselves.

Jump to

Keyboard shortcuts

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