selector

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ErrEmpty = errs.Kind("empty")

	ErrExecutorsList = errs.Entity("executors list")
)

Variables

View Source
var ErrEmptyExecutorsList = errs.WithDomain(errSelectorDomain, ErrEmpty, ErrExecutorsList)

Functions

func WithBlock

func WithBlock() cfg.Option[*Config]

WithBlock configures the Selector to block (wait) for the underlying executor.Executor to complete the task.

By default, the returned Selector from New is a non-blocking Selector. It mostly relies on the setup of the components to at least register the error as metrics or logs, but Exec calls return nil errors if the local context times out.

WithBlock waits until the execution is done, so an accurate error value is returned from the Selector.

func WithExecutors

func WithExecutors(executors ...executor.Executor) cfg.Option[*Config]

WithExecutors configures the Selector with the input executor.Executor(s).

This call returns a cfg.NoOp cfg.Option if the input set of executor.Executor is empty, or contains only nil and / or no-op executor.Executor.

func WithLogHandler

func WithLogHandler(handler slog.Handler) cfg.Option[*Config]

WithLogHandler decorates the Selector with logging using the input log handler.

func WithLogger

func WithLogger(logger *slog.Logger) cfg.Option[*Config]

WithLogger decorates the Selector with the input logger.

func WithMetrics

func WithMetrics(m Metrics) cfg.Option[*Config]

WithMetrics decorates the Selector with the input metrics registry.

func WithTimeout

func WithTimeout(dur time.Duration) cfg.Option[*Config]

WithTimeout configures a (non-blocking) Selector to wait a certain duration before detaching of the executable task, before continuing to select the next one.

By default, the local context timeout is set to one second. Any negative or zero duration values result in a cfg.NoOp cfg.Option being returned.

func WithTrace

func WithTrace(tracer trace.Tracer) cfg.Option[*Config]

WithTrace decorates the Selector with the input trace.Tracer.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

type Metrics

type Metrics interface {
	// IncSelectorSelectCalls increases the count of Select calls, by the Selector.
	IncSelectorSelectCalls()
	// IncSelectorSelectErrors increases the count of Select call errors, by the Selector.
	IncSelectorSelectErrors()
}

Metrics describes the actions that register Selector-related metrics.

type Selector

type Selector interface {
	// Next picks up the following scheduled job to execute from its configured (set of) executor.Executor, and
	// calls its Exec method.
	//
	// This call also imposes a minimum step duration of 50ms, to ensure that early-runs are not executed twice due to the
	// nature of using clocks in Go. This sleep is deferred to come in after the actual execution of the job.
	//
	// The Selector allows multiple executor.Executor to be configured, and multiple executor.Executor can share similar
	// execution times. If that is the case, the executor is launched in an executor.Multi call.
	//
	// The error returned from a Next call is the error raised by the executor.Executor's Exec call.
	Next(ctx context.Context) error
}

Selector describes the capabilities of a cron selector, which picks up the next job to execute (out of a set of executor.Executor)

Implementations of Selector must focus on the logic within its only method, Next, that will set the strategy to picking up the following job to run. The default implementation looks for the nearest job (in time) to execute, with support for multiple executions in one-go.

Custom implementations could, for example, check for preconditions, run clean-up jobs, and more.

The runtime of a Selector depends on the input context.Context when calling its Next method, as it can be used to signal cancellation or used for timeouts.

func AddLogs

func AddLogs(s Selector, handler slog.Handler) Selector

AddLogs replaces the input Selector's logger with a different one, using the input slog.Handler.

If the input logger is nil, the Selector's logger will be set to be a no-op.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned.

If the input Selector is a valid Selector, then its logger is replaced with a new one using the input handler.

Otherwise, the Selector is returned as-is.

func AddMetrics

func AddMetrics(s Selector, m Metrics) Selector

AddMetrics replaces the input Selector's metrics, using the input Metrics interface.

If the input metrics is nil, the Selector's metrics will be set to be a no-op.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned.

If the input Selector is a valid Selector, then its metrics collector is replaced with the input one.

Otherwise, the Selector is returned as-is.

func AddTraces

func AddTraces(s Selector, tracer trace.Tracer) Selector

AddTraces replaces the input Selector's tracer with a different one, using the input trace.Tracer.

If the input tracer is nil, the Selector's tracer will be set to be a no-op.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned.

If the input Selector is a valid Selector, then its tracer is replaced with the input one.

Otherwise, the Selector is returned as-is.

func New

func New(options ...cfg.Option[*Config]) (Selector, error)

New creates a Selector with the input cfg.Option(s), also returning an error if raised.

Creating a Selector requires at least one executor.Executor, which can be added through the WithExecutors option. To allow this configuration to be variadic as well, it is served as a cfg.Option.

func NoOp

func NoOp() Selector

NoOp returns a no-op Selector.

Jump to

Keyboard shortcuts

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