lifecycle

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package lifecycle provides a life cycle manager abstracting the starting and stopping of processes by registered start or stop hooks.

The following features as supported:

  • Start hooks can either be called synchronously or asynchronously.
  • Start hooks can use the application context (hard shutdown) or background context (graceful shutdown).
  • Stop hooks are synchronous and use a shutdown context with 10s timeout.
  • Ordering of start and stop hooks.
  • Any error from start hooks immediately triggers graceful shutdown.
  • Closing application context triggers graceful shutdown.
  • Any error from stop hooks immediately triggers hard shutdown.

Index

Constants

View Source
const (
	StartAggSigDB OrderStart = iota
	StartMonitoringAPI
	StartValidatorAPI
	StartP2PPing
	StartLeaderCast
	StartSimulator
	StartScheduler

	StopTracing OrderStop = iota
	StopScheduler
	StopP2PPeerDB
	StopP2PTCPNode
	StopP2PUDPNode
	StopMonitoringAPI
	StopBeaconMock // Need to close this before validator API, since it can hold long lived connections.
	StopValidatorAPI
)

Global ordering of start and stop hooks.

Variables

This section is empty.

Functions

This section is empty.

Types

type HookFunc

type HookFunc func(ctx context.Context) error

HookFunc wraps a standard hook function (context and error) as a IHookFunc.

func (HookFunc) Call

func (fn HookFunc) Call(ctx context.Context) error

type HookFuncCtx

type HookFuncCtx func(ctx context.Context)

HookFuncCtx wraps a context (no error) hook function as a IHookFunc.

func (HookFuncCtx) Call

func (fn HookFuncCtx) Call(ctx context.Context) error

type HookFuncErr

type HookFuncErr func() error

HookFuncErr wraps an error (no context) hook function as a IHookFunc.

func (HookFuncErr) Call

func (fn HookFuncErr) Call(context.Context) error

type HookFuncMin

type HookFuncMin func()

HookFuncMin wraps a minimum (no context, no error) hook function as a IHookFunc.

func (HookFuncMin) Call

func (fn HookFuncMin) Call(context.Context) error

type HookStartType

type HookStartType int

HookStartType defines the type of start hook.

const (
	// AsyncAppCtx defines a start hook that will be called asynchronously (non-blocking)
	// with the application context. Using the application usually results in hard shutdown.
	AsyncAppCtx HookStartType = iota + 1

	// SyncBackground defines a start hook that wil be called synchronously (blocking)
	// with a fresh background context. Processes that support graceful shutdown can
	// associate this with a call to RegisterStop.
	SyncBackground

	// AsyncBackground defines a start hook that wil be called asynchronously (non-blocking)
	// with a fresh background context. Processes that support graceful shutdown can
	// associate this with a call to RegisterStop.
	AsyncBackground
)

type IHookFunc

type IHookFunc interface {
	Call(context.Context) error
}

IHookFunc is the life cycle hook function interface. Users will mostly wrap functions using one of the types below.

type Manager

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

Manager manages process life cycle by registered start and stop hooks.

func (*Manager) RegisterStart

func (m *Manager) RegisterStart(typ HookStartType, order OrderStart, fn IHookFunc)

RegisterStart registers a start hook. The type defines whether it is sync or async and which context is used. The order defines the order in which hooks are called.

func (*Manager) RegisterStop

func (m *Manager) RegisterStop(order OrderStop, fn IHookFunc)

RegisterStop registers a synchronous stop hook that will be called with the shutdown context that may timeout.

func (*Manager) Run

func (m *Manager) Run(appCtx context.Context) error

Run the lifecycle; start all hooks, waiting for shutdown, stop all hooks.

type OrderStart

type OrderStart int

OrderStart defines the order hooks are started.

func (OrderStart) String

func (i OrderStart) String() string

type OrderStop

type OrderStop int

OrderStop defines the order hooks are stopped.

func (OrderStop) String

func (i OrderStop) String() string

Jump to

Keyboard shortcuts

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