supervisor

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2018 License: MIT Imports: 6 Imported by: 8

README

supervisor

GoDoc Build Status Codecov

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrPrematurelyClosed notifies that Close() method
	// was called before Open()
	ErrPrematurelyClosed = errors.New("prematurely closed")
)
View Source
var (
	// ErrTimeout returned by Timeout if timeout exceeded
	ErrTimeout = errors.New("timeout exceeded")
)

Functions

This section is empty.

Types

type Chain

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

Chain supervises Components in order. All supervised components are open in FIFO order and closed in LIFO order.

Chain collects and returns error from corresponding Component methods. If more than one Components returns errors they will be wrapped in errslice.Error.

func NewChain

func NewChain(ctx context.Context, components ...Component) (c *Chain)

NewChain creates new Chain. Provided context manages whole Chain. Close Context is equivalent to call Chain.Close().

func (Chain) Close

func (c Chain) Close() (err error)

Close initialises shutdown for all Components. This method may be called many times and will return equal results. It's guaranteed that Close() method of all components will be called only once.

func (Chain) Open

func (c Chain) Open() (err error)

Open blocks until all components are opened. This method should be called before Close(). Otherwise Open() will return error. If Open() method of one of components returns error all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Open() method of all components will be called only once.

func (Chain) Wait

func (c Chain) Wait() (err error)

Wait blocks until all components are exited. If one of Wait() method of one of Components is exited before Close() all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Wait() method of all components will be called only once.

type Component

type Component interface {

	// Open runs Component initialisation and should blocks until
	// Component is initialised.
	Open() (err error)

	// Close initialises Component shutdown.
	Close() (err error)

	// Wait should blocks until Component shutdown.
	Wait() (err error)
}

Component is basic building block to build supervisor trees

type Control

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

Control is simplest embeddable component

Example (Layered)
package main

import (
	"context"
	"fmt"
	"github.com/akaspin/supervisor"
)

// LayeredControl uses two controls to fully control component lifecycle.
// Use this pattern only if you need complex logic because all supervisor
// primitives guarantees that Open(), Close() and Wait() methods of supervised
// components will be called only once.
type LayeredControl struct {
	*supervisor.Control // managed externally
	doneControl         *supervisor.Control
}

func NewLayeredControl(ctx context.Context) (c *LayeredControl) {
	c = &LayeredControl{
		Control:     supervisor.NewControl(ctx),
		doneControl: supervisor.NewControl(context.Background()),
	}
	go func() {
		<-c.Ctx().Done()
		fmt.Println("shutting down")
		c.doneControl.Close()
	}()
	return c
}

func (c *LayeredControl) Open() (err error) {
	if c.Control.IsOpen() {
		return nil
	}
	fmt.Println("opening")
	c.Control.Open()
	c.doneControl.Open()
	return nil
}

func (c *LayeredControl) Close() (err error) {
	if c.Control.IsClosed() {
		return nil
	}
	fmt.Println("closing")
	return c.Control.Close()
}

func (c *LayeredControl) Wait() (err error) {
	if c.doneControl.IsClosed() {
		return nil
	}
	c.doneControl.Wait()
	fmt.Println("exited")
	return
}

func main() {
	c := NewLayeredControl(context.Background())
	c.Open()
	c.Close()
	c.Wait()

}
Output:

opening
closing
shutting down
exited

func NewControl

func NewControl(ctx context.Context) (c *Control)

NewControl returns new Control

func (*Control) Close

func (c *Control) Close() (err error)

Close closes Control context

func (*Control) Ctx

func (c *Control) Ctx() context.Context

Ctx returns Control context

func (*Control) IsClosed

func (c *Control) IsClosed() (ok bool)

IsClosed returns true if control is closed

func (*Control) IsOpen

func (c *Control) IsOpen() (ok bool)

IsOpen returns true if Control is opened

func (*Control) Open

func (c *Control) Open() (err error)

Open sets Control in open state

func (*Control) Wait

func (c *Control) Wait() (err error)

Wait blocks until internal done context is not done. See Done().

type Group

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

Group supervises Components in parallel. All supervised components are open and closed concurrently.

Group collects and returns error from corresponding Component methods. If more than one Components returns errors they will be wrapped in errslice.Error.

func NewGroup

func NewGroup(ctx context.Context, components ...Component) (g *Group)

NewGroup creates new Group. Provided context manages whole Group. Close Context is equivalent to call Group.Close().

func (Group) Close

func (c Group) Close() (err error)

Close initialises shutdown for all Components. This method may be called many times and will return equal results. It's guaranteed that Close() method of all components will be called only once.

func (Group) Open

func (c Group) Open() (err error)

Open blocks until all components are opened. This method should be called before Close(). Otherwise Open() will return error. If Open() method of one of components returns error all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Open() method of all components will be called only once.

func (Group) Wait

func (c Group) Wait() (err error)

Wait blocks until all components are exited. If one of Wait() method of one of Components is exited before Close() all opened components will be closed. This method may be called many times and will return equal results. It's guaranteed that Wait() method of all components will be called only once.

type Timeout

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

Timeout supervises shutdown process of own descendant

func NewTimeout

func NewTimeout(ctx context.Context, timeout time.Duration, component Component) (t *Timeout)

NewTimeout creates new Timeout

func (*Timeout) Close

func (t *Timeout) Close() (err error)

Close closes supervised component and starts timer

func (*Timeout) Open

func (t *Timeout) Open() (err error)

Open opens supervised component and return error if any

func (*Timeout) Wait

func (t *Timeout) Wait() (err error)

Wait blocks until Wait() of supervised component is exited or timeout is reached.

type Trap

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

Trap can be used as watchdog in supervisor tree.

func NewTrap

func NewTrap(ctx context.Context) (t *Trap)

NewTrap returns new Trap bounded to given Context

func (*Trap) Close

func (t *Trap) Close() (err error)

Close closes trap

func (*Trap) Open

func (*Trap) Open() (err error)

Open opens Trap and never returns any errors

func (*Trap) Trap

func (t *Trap) Trap(err error)

Trap accepts given error and closes Trap if error is not nil

func (*Trap) Wait

func (t *Trap) Wait() (err error)

Wait returns last accepted error

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/akaspin/supervisor"
)

func main() {
	trap := supervisor.NewTrap(context.Background())
	trap.Open()
	go func() {
		if err := trap.Wait(); err != nil && err.Error() != "bang" {
			fmt.Println(trap.Wait())
		}
	}()

	trap.Trap(errors.New("bang"))
	fmt.Println(trap.Wait())
}
Output:

bang

Jump to

Keyboard shortcuts

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