controller

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package controller implements controller and manager.

Controllers can be used to run a specific task repetitively at regular intervals of time. A controller stores the resulting stats of runs with itself until pulled from it.

A manager is used to orchestrate multiple controllers running concurrently.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

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

Controller runs a specific operation infinitely until the context is canceled at regular intervals of time.

func NewController

func NewController(ctx context.Context, opts *Opts) (*Controller, error)

NewController creates a new controller with given name and type and runs the function at regular `interval`s of time without blocking.

func (*Controller) ID

func (c *Controller) ID() string

ID returns the ID of the controller.

func (*Controller) Interval

func (c *Controller) Interval() time.Duration

Interval returns the interval after which the controller executes the function.

func (*Controller) Name

func (c *Controller) Name() string

Name returns the name of the controller.

func (*Controller) PullAllStats

func (c *Controller) PullAllStats() map[time.Time]*RunStat

PullAllStats fetches the stats for the controller and also cleans up the stats.

func (*Controller) PullLatestStat

func (c *Controller) PullLatestStat() *RunStat

PullLatestStat fetches only the latest stat from the controller and cleans up the stats.

func (*Controller) Start

func (c *Controller) Start()

Start starts the execution of the controller.

func (*Controller) Stop

func (c *Controller) Stop()

Stop stops the execution of the controller. It does not wait for controller to complete. Use `Wait` for that.

func (*Controller) UpdateFunc

func (c *Controller) UpdateFunc(fn RunnerFunc) error

UpdateFunc updates the controller function.

func (*Controller) UpdateInterval

func (c *Controller) UpdateInterval(interval time.Duration) error

UpdateInterval updates the interval of the controller.

func (*Controller) Wait

func (c *Controller) Wait()

Wait waits for execution of the controller to be complete.

type Manager

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

Manager manages multiple controllers and running at the same time.

func NewManager

func NewManager(ctx context.Context) *Manager

NewManager creates a new manager with no controllers.

func (*Manager) Close

func (m *Manager) Close()

Close closes all the controller irrespective of whether they shutdown properly.

func (*Manager) ListControllers

func (m *Manager) ListControllers() map[string]string

ListControllers lists all the controllers managed by the manager.

This returns a map of controller ID with it's name.

func (*Manager) PullAllStats

func (m *Manager) PullAllStats() map[string][]*RunStat

PullAllStats gets all the stats for all the controllers registered with the manager.

func (*Manager) PullLatestStats

func (m *Manager) PullLatestStats() map[string]*RunStat

PullLatestStats gets latest stat for each controller in the manager.

func (*Manager) RemoveAll

func (m *Manager) RemoveAll()

RemoveAll removes all the controllers from the manager.

func (*Manager) RemoveAllAndWait

func (m *Manager) RemoveAllAndWait()

RemoveAllAndWait removes all the controllers and waits for them to stop completely.

func (*Manager) RemoveController

func (m *Manager) RemoveController(id string)

RemoveController removes the controller from manager if it exists. If it doesn't, it does nothing. It does not wait for controller to stop.

func (*Manager) RemoveControllerAndWait

func (m *Manager) RemoveControllerAndWait(id string)

RemoveControllerAndWait is like RemoveController but waits for completion of controller.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown gracefully attempts to close the manager and terminate and remove all the controllers. In case of error, the manager is not closed, and hence `Close` is required to be called to terminate the manager.

func (*Manager) UpdateController

func (m *Manager) UpdateController(opts *Opts) error

UpdateController updates the controller if it exists with the same name or creates a new controller if it doesn't.

func (*Manager) Wait

func (m *Manager) Wait()

Wait waits for the manager to terminate.

type Opts

type Opts struct {
	ID       string
	Name     string
	Interval time.Duration
	Func     RunnerFunc
}

Opts are the options required to create a new controller.

type RunStat

type RunStat struct {
	ID   string
	Name string

	Err error
	Res interface{}
}

RunStat are the statistics for single run of the runner function.

type RunnerFunc

type RunnerFunc func(context.Context) (interface{}, error)

RunnerFunc is the function that runs after each tick.

Jump to

Keyboard shortcuts

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