routine

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 6 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogExitedCallback

func NewLogExitedCallback(le *logrus.Entry) func(err error)

NewLogExitedCallback returns a ExitedCb which logs when a controller exited.

Types

type Option

type Option interface {
	// ApplyToRoutineContainer applies the option to the RoutineContainer.
	ApplyToRoutineContainer(k *RoutineContainer)
}

Option is an option for a RoutineContainer instance.

func WithBackoff added in v1.1.2

func WithBackoff(bo backoff.BackOff, le *logrus.Entry) Option

WithBackoff returns an exited callback which restarts the routine after a backoff if the routine returned an error.

Resets the backoff if the routine returned successfully. le is an optional logger to log the backoff.

func WithExitCb

func WithExitCb(cb func(err error)) Option

WithExitCb adds a callback after a routine exits.

func WithExitLogger

func WithExitLogger(le *logrus.Entry) Option

WithExitLogger adds a exited callback which logs information about the exit.

type Routine

type Routine func(ctx context.Context) error

Routine is a function called as a goroutine. If nil is returned, exits cleanly permanently. If an error is returned, can be restarted later.

type RoutineContainer

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

RoutineContainer contains a Routine.

func NewRoutineContainer

func NewRoutineContainer(opts ...Option) *RoutineContainer

NewRoutineContainer constructs a new RoutineContainer. Note: routines won't start until SetContext is called.

func NewRoutineContainerWithLogger

func NewRoutineContainerWithLogger(le *logrus.Entry, opts ...Option) *RoutineContainer

NewRoutineContainerWithLogger constructs a new RoutineContainer instance. Logs when a controller exits without being canceled.

Note: routines won't start until SetContext is called.

func (*RoutineContainer) ClearContext added in v1.1.2

func (k *RoutineContainer) ClearContext() bool

ClearContext clears the context and shuts down all routines.

Returns if the routine was stopped or restarted.

func (*RoutineContainer) RestartRoutine

func (k *RoutineContainer) RestartRoutine() bool

RestartRoutine restarts the existing routine (if set). Returns if the routine was restarted. Returns false if the context is currently nil or the routine is unset.

func (*RoutineContainer) SetContext

func (k *RoutineContainer) SetContext(ctx context.Context, restart bool) bool

SetContext updates the root context.

nil context is valid and will shutdown the routines. if restart is true, errored routines will also restart.

Returns if the routine was stopped or restarted.

func (*RoutineContainer) SetRoutine

func (k *RoutineContainer) SetRoutine(routine Routine) bool

SetRoutine sets the routine to execute, resetting the existing, if set. If the specified routine is nil, shuts down the current routine. If routine = nil, waits to return until the existing routine fully shuts down. Otherwise, returns right away without blocking. Returns if the current routine was stopped or overwritten.

func (*RoutineContainer) WaitExited added in v1.0.1

func (k *RoutineContainer) WaitExited(ctx context.Context, returnIfNotRunning bool, errCh <-chan error) error

WaitExited waits for the routine to exit and returns the error if any. Note: Will NOT return after the routine is restarted normally. If returnIfNotRunning is set, returns nil if no routine is running. errCh is an optional error channel (can be nil)

Jump to

Keyboard shortcuts

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