worker

package module
v0.0.0-...-5b6e765 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Tideland Go Worker

GitHub release GitHub license Go Module GoDoc

Description

Handle synchronous and asynchronous tasks enqueued in the background.

Contributors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enqueue

func Enqueue(w *Worker, task Task) error

Enqueue passes a task to a worker.

func EnqueueWaiting

func EnqueueWaiting(w *Worker, task Task) error

EnqueueWaiting passes a task to a worker and waits until it has been processed.

func Shutdown

func Shutdown(w *Worker) error

Shutdown tells the worker to stop working. It's an asynchronous operation and returns immediately. The worker will finish all tasks gracefully before stopping.

Types

type Awaiter

type Awaiter func() error

Awaiter is returned by AsyncAwait to wait for the worker to finish a task.

func AsyncAwait

func AsyncAwait(w *Worker, task Task, timeout time.Duration) (Awaiter, error)

AsyncAwait passes a task to a worker and returns an Awaiter to wait for the worker to finish the task. The timeout is the maximum time to wait for the worker to finish the task. After enqueuing the task different work can be done before waiting for the worker to finish the task.

type NotStartedError

type NotStartedError struct{}

NotStartedError signals that the worker processor did not start.

func (NotStartedError) Error

func (NotStartedError) Error() string

type Option

type Option func(act *Worker) error

Option defines the signature of an option setting function.

func WithRateBurst

func WithRateBurst(rate, burst int) Option

WithRateBurst sets the rate and the burst values of a worker. Rate is the number of tasks processed per second, burst the number of tasks processed at once (means enqueued to the worker at ounce). If burst is 0 the rate is used as burst.

Be aware that a high rate and burst can lead to a high CPU load.

func WithTimeout

func WithTimeout(d time.Duration) Option

type ShuttingDownError

type ShuttingDownError struct{}

ShuttingDownError signals that the worker processor is shutting down.

func (ShuttingDownError) Error

func (ShuttingDownError) Error() string

type Task

type Task func() error

Task defines the signature of a task functionto be processed by a worker.

type TimeoutError

type TimeoutError struct{}

TimeoutError signals that the worker processor did not start in time.

func (TimeoutError) Error

func (TimeoutError) Error() string

type Worker

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

Worker help to enqueue tasks and process them in the background in order. Stopping the worker with the according command ensures that all tasks before are processed.

func New

func New(ctx context.Context, options ...Option) (*Worker, error)

New creates a new worker. The options are used to configure the worker. If no options are given the worker is created with default settings.

Jump to

Keyboard shortcuts

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