worker

package
v0.0.2-0...-db6250e Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: CC0-1.0, CC0-1.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Uninitialised = State(iota) // Uninitialised
	Waiting                     // Waiting for a task
	Running                     // Running
	Error                       // Exited in an error state
	Shutdown                    // Exited cleanly
)

The valid worker states.

Variables

This section is empty.

Functions

func Range

func Range(f RangeFunc, start int, finish int, minRangeSize int) (err error)

Range applies the given function f to the range start <= i < finish, returning any errors. The minimum range per worker can be specified by 'minRangeSize', however this is only used as a guide.

Types

type Group

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

Group represents a group of workers, and satisfies the worker Interface.

func NewGroup

func NewGroup(ws ...Interface) *Group

NewGroup returns a new group for the given workers.

func NewGroupFromWorkFunc

func NewGroupFromWorkFunc(ctx context.Context, f WorkFunc, n int) *Group

NewGroupUsingWorkFunc returns a new group populated with n workers created by wrapping the given WorkFunc f (which must be non-nil).

func (*Group) Add

func (g *Group) Add(x interface{}) error

Add adds a task for a worker in the group. This will block until space becomes available on the worker's queue.

func (*Group) AddWorker

func (g *Group) AddWorker(w Interface) error

AddWorker adds a worker to the group.

func (*Group) Done

func (g *Group) Done() <-chan struct{}

Done returns a channel that will be closed when the workers in the group have shut down (or have entered an error state).

func (*Group) Err

func (g *Group) Err() error

Err returns an error if one of the workers in the group has entered an error state.

func (*Group) Shutdown

func (g *Group) Shutdown() error

Shutdown asks the workers in the group to shut down. This will block until all the workers have shut down (or have entered an error state). If after shut down one of the workers in the group is in an error state, that error will be returned.

func (*Group) Status

func (g *Group) Status() State

Status returns the group's current state, as determined by the workers in the group.

func (*Group) String

func (g *Group) String() string

String returns a string description of this group of workers in the form "Workers(%s1,%s2,...,%sn)" where "%s1", "%s2", ..., "%sn" are the states of the workers in this group.

type Interface

type Interface interface {
	Add(x interface{}) error // Add submits the task x to the worker.
	Close() error            // Close asks the worker to exit. This will block until the worker has exited.
}

Interface defines the interface satisfied by a worker.

type RangeFunc

type RangeFunc func(i int) error

RangeFunc defines the function used by Range.

type State

type State int

State represents the state of a worker.

func (State) String

func (t State) String() (s string)

String returns a string representation of the state.

type WorkFunc

type WorkFunc func(ctx context.Context, x interface{}) error

WorkFunc defines a function that can be wrapped by Wrap to create a worker. A task x will be passed to the function. If the function returns an error then the worker will be placed in an error state and will stop accepting tasks.

type Worker

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

Worker provides a worker by wrapping a WorkFunc.

func Wrap

func Wrap(ctx context.Context, f WorkFunc) *Worker

Wrap returns a new worker wrapping the given function. The work function f (which must be non-nil) will be called for every task to be processed by the worker. If the work function returns an error then the worker will be placed into an error state and will stop. The given context will be passed to f.

func (*Worker) Add

func (w *Worker) Add(x interface{}) error

Add submits the task x to the worker. This will block until the worker is free to accept the task. If the worker has exited (because it has Shutdown or is in an Error state) then ErrWorkerNotRunning will be returned.

Note that Add returning nil only guarantees that the task was passed to the worker's WorkFunc, and not that the WorkFunc successfully processed the task. If the WorkFunc fails to successfully process the task (i.e. if the WorkFunc returns an error) then the worker will be placed in an Error state, and the task x, along with the resulting error, can be recovered by calling TaskAndErr.

func (*Worker) Close

func (w *Worker) Close() error

Close asks the worker to exit. This will block until the worker has exited (because it has Shutdown or is in an Error state). If the worker exited in an Error state, the worker's last error will be returned. On return, Done is closed.

func (*Worker) Done

func (w *Worker) Done() <-chan struct{}

Done returns a channel that will be closed when the worker has exited (because it has Shutdown or is in an Error state).

func (*Worker) Err

func (w *Worker) Err() error

Err returns the worker's last error (if any). This is non-nil if and only if the worker is in an Error state. After Err returns a non-nil error, successive calls to Err return the same error.

func (*Worker) State

func (w *Worker) State() State

State returns the worker's current state.

func (*Worker) String

func (w *Worker) String() string

String returns the string "Worker(%s)" where "%s" is the worker's current state.

func (*Worker) TaskAndErr

func (w *Worker) TaskAndErr() (x interface{}, err error)

TaskAndErr returns the worker's last error err (if any) and the task x that caused this error. This is non-nil if and only if the worker is in an Error state. After TaskAndErr returns a non-nil task and error, successive calls to TaskAndErr return the same task and error.

Jump to

Keyboard shortcuts

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