run

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	// Unit for Group registration and identification
	Unit
	// FlagSet returns an object's FlagSet
	FlagSet() *FlagSet
	// Validate checks an object's stored values
	Validate() error
}

Config interface should be implemented by Group Unit objects that manage their own configuration through the use of flags. If a Unit's Validate returns an error it will stop the Group immediately.

type FlagSet

type FlagSet struct {
	*pflag.FlagSet
	Name string
}

FlagSet holds a pflag.FlagSet as well as an exported Name variable for allowing improved help usage information.

func NewFlagSet

func NewFlagSet(name string) *FlagSet

NewFlagSet returns a new FlagSet for usage in Config objects.

type Group

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

Group builds on https://github.com/oklog/run to provide a deterministic way to manage service lifecycles. It allows for easy composition of elegant monoliths as well as adding signal handlers, metrics services, etc.

func NewGroup added in v0.2.0

func NewGroup(name string) Group

func (Group) ListUnits

func (g Group) ListUnits() string

ListUnits returns a list of all Group phases and the Units registered to each of them.

func (Group) Name

func (g Group) Name() string

Name shows the name of the group

func (*Group) Register

func (g *Group) Register(units ...Unit) []bool

Register will inspect the provided objects implementing the Unit interface to see if it needs to register the objects for any of the Group bootstrap phases. If a Unit doesn't satisfy any of the bootstrap phases it is ignored by Group. The returned array of booleans is of the same size as the amount of provided Units, signalling for each provided Unit if it successfully registered with Group for at least one of the bootstrap phases or if it was ignored.

func (*Group) RegisterFlags

func (g *Group) RegisterFlags() *FlagSet

func (*Group) Run

func (g *Group) Run() (err error)

Run will execute all phases of all registered Units and block until an error occurs. If RunConfig has been called prior to Run, the Group's Config phase will be skipped and Run continues with the PreRunner and Service phases.

The following phases are executed in the following sequence:

Config phase (serially, in order of Unit registration)
  - FlagSet()        Get & register all FlagSets from Config Units.
  - Flag Parsing     Using the provided args (os.Args if empty)
  - Validate()       Validate Config Units. Exit on first error.

PreRunner phase (serially, in order of Unit registration)
  - PreRun()         Execute PreRunner Units. Exit on first error.

Service phase (concurrently)
  - Serve()          Execute all Service Units in separate Go routines.
  - Wait             Block until one of the Serve() methods returns
  - GracefulStop()   Call interrupt handlers of all Service Units.

Run will return with the originating error on:
- first Config.Validate()  returning an error
- first PreRunner.PreRun() returning an error
- first Service.Serve()    returning (error or nil)

func (*Group) RunConfig

func (g *Group) RunConfig() (interrupted bool, err error)

RunConfig runs the Config phase of all registered Config aware Units. Only use this function if needing to add additional wiring between config and (pre)run phases and a separate PreRunner phase is not an option. In most cases it is best to use the Run method directly as it will run the Config phase prior to executing the PreRunner and Service phases. If an error is returned the application must shut down as it is considered fatal.

func (*Group) WaitTillReady

func (g *Group) WaitTillReady()

type PreRunner

type PreRunner interface {
	// Unit for Group registration and identification
	Unit
	PreRun() error
}

PreRunner interface should be implemented by Group Unit objects that need a pre run stage before starting the Group Services. If a Unit's PreRun returns an error it will stop the Group immediately.

func NewPreRunner

func NewPreRunner(name string, fn func() error) PreRunner

NewPreRunner takes a name and a standalone pre runner compatible function and turns them into a Group compatible PreRunner, ready for registration.

type Service

type Service interface {
	// Unit for Group registration and identification
	Unit
	// Serve starts the GroupService and blocks.
	Serve() StopNotify
	// GracefulStop shuts down and cleans up the GroupService.
	GracefulStop()
}

Service interface should be implemented by Group Unit objects that need to run a blocking service until an error occurs or a shutdown request is made. The Serve method must be blocking and return an error on unexpected shutdown. Recoverable errors need to be handled inside the service itself. GracefulStop must gracefully stop the service and make the Serve call return.

Since Service is managed by Group, it is considered a design flaw to call any of the Service methods directly in application code.

type StopNotify

type StopNotify <-chan struct{}

type Tester

type Tester struct {
	ID string
	// contains filtered or unexported fields
}

func NewTester

func NewTester(ID string) *Tester

func (*Tester) GracefulStop

func (t *Tester) GracefulStop()

func (*Tester) Name

func (t *Tester) Name() string

func (*Tester) Serve

func (t *Tester) Serve() StopNotify

func (*Tester) WaitUntilStarted

func (t *Tester) WaitUntilStarted() error

type Unit

type Unit interface {
	Name() string
}

Unit is the default interface an object needs to implement for it to be able to register with a Group. Name should return a short but good identifier of the Unit.

Jump to

Keyboard shortcuts

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