selector

package
v0.0.0-...-757fb74 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 11 Imported by: 0

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 decorates the input Selector with logging, using the input slog.Handler.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned. If the input slog.Handler is nil or a no-op handler, then a default slog.Handler is configured (a text handler printing to standard-error)

If the input Selector is already a logged Selector, then this logged Selector is returned with the new handler as its logger's handler.

Otherwise, the Selector is decorated with logging within a custom type that implements Selector.

func AddMetrics

func AddMetrics(s Selector, m Metrics) Selector

AddMetrics decorates the input Selector with metrics, using the input Metrics interface.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned. If the input Metrics is nil or if it is a no-op Metrics interface, then the input Selector is returned as-is.

If the input Selector is already a Selector with metrics, then this Selector with metrics is returned with the new Metrics interface configured in place of the former.

Otherwise, the Selector is decorated with metrics within a custom type that implements Selector.

func AddTraces

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

AddTraces decorates the input Selector with tracing, using the input trace.Tracer.

If the input Selector is nil or a no-op Selector, a no-op Selector is returned. If the input trace.Tracer is nil, then the input Selector is returned as-is.

If the input Selector is already a Selector with tracing, then this Selector with tracing is returned with the new trace.Tracer configured in place of the former.

Otherwise, the Selector is decorated with tracing within a custom type that implements Selector.

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