application

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	StateStart = State(iota) // initial state
	StateInit
	StateRunning
	StateShutdown
	StateUninit
	StateTerminated
)

Variables

View Source
var (
	ServiceNotFound = errors.New("service not found")
)

Functions

func ChangeState

func ChangeState(oldState *State, newState State)

ChangeState atomically changes the application state

func IsStateEqual

func IsStateEqual(current State, target State) bool

IsStateEqual compare application state with target state. True if states is equal, false otherwise

Types

type Application

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

func CreateApplication

func CreateApplication(logger zerolog.Logger, shutdownTimeout time.Duration, initTimeout time.Duration,
	mainFunc MainFunc, recoverFunc RecoverFunc) Application

CreateApplication create new application

func (*Application) RegisterService

func (app *Application) RegisterService(serviceName string, service Service) *Application

func (*Application) Run

func (app *Application) Run() error

Run application lifecycle. Must be called from main function

func (*Application) SetSysSignals

func (app *Application) SetSysSignals(signals ...syscall.Signal) *Application

SetSysSignals append os signal(s) to application TODO: check if windows. Signals works on windows?

type MainFunc

type MainFunc func(ctx context.Context, wait func(), keeper *ServiceKeeper) error

type RecoverFunc

type RecoverFunc func() error

type Service

type Service interface {
	// Init initialization service
	Init(ctx context.Context) error
	// Ping check if service keep alive
	Ping(ctx context.Context) error
	// Close resources
	Close(ctx context.Context) error
	// Get getting service
	Get() any
}

type ServiceKeeper

type ServiceKeeper struct {
	Services    map[string]*Service
	PingPeriod  time.Duration
	PingTimeout time.Duration
}

func NewServiceKeeper

func NewServiceKeeper(services map[string]*Service, pingPeriod time.Duration,
	pingTimeout time.Duration) *ServiceKeeper

func (*ServiceKeeper) Get

func (keeper *ServiceKeeper) Get(serviceName string) (any, error)

func (*ServiceKeeper) HealthCheck

func (keeper *ServiceKeeper) HealthCheck() error

type ServiceNotFoundError

type ServiceNotFoundError struct {
	ServiceName string
	Err         error
}

func (*ServiceNotFoundError) Error

func (e *ServiceNotFoundError) Error() string

type Stage

type Stage struct {
}

func NewApplicationStage

func NewApplicationStage() *Stage

func (*Stage) Init

func (c *Stage) Init(ctx context.Context, state *State, keeper *ServiceKeeper) error

func (*Stage) Shutdown

func (c *Stage) Shutdown(ctx context.Context, state *State, shutdown chan struct{}) error

func (*Stage) Start

func (c *Stage) Start(ctx context.Context, state *State, keeper *ServiceKeeper,
	mainFunc MainFunc, waitFunc func()) error

func (*Stage) Uninit

func (c *Stage) Uninit(ctx context.Context, state *State, keeper *ServiceKeeper) error

type State

type State int32

Jump to

Keyboard shortcuts

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