agent

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

Agent GoDoc

The agent package provides life-cycle managment agent for plugins. It intented tp be used as a base point of your program used in main package.

func main() {
	plugin := myplugin.NewPlugin()
	
	a := agent.NewAgent(
		agent.Plugins(plugin),
	)
	if err := a.Run(); err != nil {
		log.Fatal(err)
	}
}

Agent options

There are various options available to customize agent.

  • Version(ver, date, id) sets version of the program
  • QuitOnClose(chan) sets signal used to quit running agent when closed
  • QuitSignals(signals) sets signals used to quit running agent (default: SIGINT, SIGTERM)
  • StartTimeout(dur)/StopTimeout(dur) sets start/stop timeout (defaults: 15s/5s)
  • add plugins to list of plugins managed by agent using:
    • Plugins(...) adds just single plugins
    • AllPlugins(...) adds plugin along with all of its plugin deps

See all options here.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BuildVersion describes version for the build. It is usually set using `git describe --always --tags --dirty`.
	BuildVersion = "dev"
	// BuildDate describes time of the build.
	BuildDate string
	// CommitHash describes commit hash for the build.
	CommitHash string
)

Variables set by the compiler using ldflags

View Source
var (
	// DefaultStartTimeout is default timeout for starting agent
	DefaultStartTimeout = time.Second * 15
	// DefaultStopTimeout is default timeout for stopping agent
	DefaultStopTimeout = time.Second * 5
)

Functions

This section is empty.

Types

type Agent

type Agent interface {
	// Run is a blocking call which starts the agent with all of its plugins,
	// waits for a signal from OS (SIGINT, SIGTERM by default), context cancellation or
	// close of quit channel (can be set via options) and then stops the agent.
	// Returns nil if all the plugins were intialized and closed successfully.
	Run() error
	// Start starts the agent with all the plugins, calling their Init() and optionally AfterInit().
	// Returns nil if all the plugins were initialized successfully.
	Start() error
	// Stop stops the agent with all the plugins, calling their Close().
	// Returns nil if all the plugins were closed successfully.
	Stop() error
	// Options returns all agent's options configured via constructor.
	Options() Options

	// Wait waits until agent is stopped  and returns same error as Stop().
	Wait() error
	// After returns a channel that is closed before the agents is stopped.
	// Note: It is not certain the all plugins are stopped, see Error()..
	After() <-chan struct{}
	// Error returns an error that occurret when the agent was stopped.
	// Note: This essentially just calls Stop()..
	Error() error
}

Agent implements startup & shutdown procedures for plugins.

func NewAgent

func NewAgent(opts ...Option) Agent

NewAgent creates a new agent using given options and registers all flags defined for plugins via config.ForPlugin.

type Option

type Option func(*Options)

Option is a function that operates on an Agent's Option

func AllPlugins

func AllPlugins(plugins ...infra.Plugin) Option

AllPlugins creates an Option that adds all of the nested plugins recursively to the Agent's plugin list.

func Context

func Context(ctx context.Context) Option

Context returns an Option that sets the context for the Agent

func Plugins

func Plugins(plugins ...infra.Plugin) Option

Plugins creates an Option that adds a list of Plugins to the Agent's Plugin list

func QuitOnClose

func QuitOnClose(ch chan struct{}) Option

QuitOnClose returns an Option that will set channel which stops Agent on close

func QuitSignals

func QuitSignals(sigs ...os.Signal) Option

QuitSignals returns an Option that will set signals which stop Agent

func StartTimeout

func StartTimeout(timeout time.Duration) Option

StartTimeout returns an Option that sets timeout for the start of Agent.

func StopTimeout

func StopTimeout(timeout time.Duration) Option

StopTimeout returns an Option that sets timeout for the stop of Agent.

func Version

func Version(buildVer, buildDate, commitHash string) Option

Version returns an Option that sets the version of the Agent to the entered string

type Options

type Options struct {
	StartTimeout time.Duration
	StopTimeout  time.Duration
	QuitSignals  []os.Signal
	QuitChan     chan struct{}
	Context      context.Context
	Plugins      []infra.Plugin
	// contains filtered or unexported fields
}

Options specifies option list for the Agent

Jump to

Keyboard shortcuts

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