controller

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoopFunc

func NoopFunc(_ctx context.Context) error

NoopFunc is a nil function.

Types

type ConfigurationStatus

type ConfigurationStatus struct {
	ErrorRetry    bool
	ShouldBackOff bool
	Interval      string
}

ConfigurationStatus represents the configuration of controller.

type Controller

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

Controller is the actual underlying controller. Each controller is created for a specific task which is specified in `controller.internal`

func (*Controller) Name

func (c *Controller) Name() string

Name returns the controller name.

func (*Controller) RunController

func (c *Controller) RunController()

RunController starts running the controller. TODO: improve this, currently it waits for the current request to finish and then waits for interval duration to run the function again. This is not a constant interval check we are looking for, so wrap the runFunc inside a goroutine.

func (*Controller) Status

func (c *Controller) Status() *Status

Status returns the current status of the controller.

func (*Controller) Type

func (c *Controller) Type() string

Type returns the controller type.

type Func

type Func struct {
	Function Function
	Params   []FuncParam
	// contains filtered or unexported fields
}

Func represents an underlying function executed by the controller. A controller function is uniquely identified by its name.

func NewControllerFunction

func NewControllerFunction(function Function, params ...FuncParam) (*Func, error)

NewControllerFunction returns an instance of a new controller function.

func (*Func) Run

func (cf *Func) Run(ctx context.Context) error

Run the actual function that ControllerFunction actually represents. The context passed as an argument to the Run function is propogated to the underlying Function.

func (*Func) Validate

func (cf *Func) Validate() error

Validate validates the controller function parameters.

type FuncParam

type FuncParam interface{}

FuncParam is an interface staisfied by all the function parameter types.

type Function

type Function interface{}

Function is an interface satisfied by all the functions.

type Internal

type Internal struct {
	// DoFunc is the function that will be run until it succeeds and/or
	// using the interval RunInterval if not 0.
	// An unset DoFunc is an error and will be logged as one.
	DoFunc *Func

	// StopFunc is called when the controller stops. It is intended to run any
	// clean-up tasks for the controller (e.g. deallocate/release resources)
	// It is guaranteed that DoFunc is called at least once before StopFunc is
	// called.
	// An unset StopFunc is not an error (and will be a no-op)
	// Note: Since this occurs on controller exit, error counts and tracking may
	// not be checked after StopFunc is run.
	StopFunc *Func

	// If set to any other value than 0, will cause DoFunc to be run in the
	// specified interval. The interval starts from when the DoFunc has
	// returned last
	RunInterval time.Duration

	// ErrorRetryBaseDuration is the initial time to wait to run DoFunc
	// again on return of an error. On each consecutive error, this value
	// is multiplied by the number of consecutive errors to provide a
	// constant back off. The default is 1s.
	ErrorRetryBaseDuration time.Duration

	// Should we have a constant back off for retries during errors.
	RetryBackOff bool

	// NoErrorRetry when set to true, disables retries on errors
	NoErrorRetry bool

	// SkipFirstRun skips the first run of the controller function.
	SkipFirstRun bool
	// contains filtered or unexported fields
}

Internal contains all parameters of a controller, including the functions to run and other metadata related to runs.

func NewControllerInternalWithCron

func NewControllerInternalWithCron(cronStr string, f *Func) (Internal, error)

NewControllerInternalWithCron returns a new controller Internal type with a cron based scheduler for the controller function

type Manager

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

Manager manages a ControllerMap and perform actions on it.

func NewManager

func NewManager() *Manager

NewManager Creates a new manager instance for the controller map.

func (*Manager) GetAllControllers

func (m *Manager) GetAllControllers() []string

GetAllControllers Returns the name of all the controllers that are managed by this manager.

func (*Manager) GetStats

func (m *Manager) GetStats() []*Status

GetStats Return the entire stats for manager.

func (*Manager) Lookup

func (m *Manager) Lookup(name string) *Controller

Lookup looks up for the controller with the provided name.

func (*Manager) RemoveAll

func (m *Manager) RemoveAll()

RemoveAll stops and removes all controllers of the manager

func (*Manager) RemoveAllAndWait

func (m *Manager) RemoveAllAndWait()

RemoveAllAndWait stops and removes all controllers of the manager and then waits for all controllers to exit

func (*Manager) RemoveController

func (m *Manager) RemoveController(name string) error

RemoveController stops and removes a controller from the manager. If DoFunc is currently running, DoFunc is allowed to complete in the background.

func (*Manager) RemoveControllerAndWait

func (m *Manager) RemoveControllerAndWait(name string) error

RemoveControllerAndWait stops and removes a controller using RemoveController() and then waits for it to run to completion.

func (*Manager) Terminate

func (m *Manager) Terminate()

Terminate terminates all the controllers managed by the manager.

func (*Manager) UpdateController

func (m *Manager) UpdateController(name, cType string, internal Internal) error

UpdateController installs or updates a controller in the manager. A controller is identified by its name. If a controller with the name already exists, the controller will be shut down and replaced with the provided controller. Updating a controller will cause the DoFunc to be run immediately regardless of any previous conditions. It will also cause any statistics to be reset.

func (*Manager) Wait

func (m *Manager) Wait()

Wait waits for the manager to terminate the controllers.

type Map

type Map map[string]*Controller

Map is the map of a controller name with the underlying Controller.

type RunStatus

type RunStatus struct {
	SuccessCount     int64
	LastSuccessStamp string

	FailureCount     int64
	LastFailureStamp string

	ConsecutiveFailureCount int64
}

RunStatus represents the status of a running controller.

type Scheduler

type Scheduler interface {
	// Next returns the time when to run the controller function again.
	Next(time.Time) time.Time
}

Scheduler is the interface for custom scheduler implementation of controller function.

type Status

type Status struct {
	Name          string
	Configuration ConfigurationStatus

	Status RunStatus
}

Status represents status of controller.

Jump to

Keyboard shortcuts

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