kernel

package
v0.0.0-...-854b266 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: Apache-2.0 Imports: 8 Imported by: 14

Documentation

Overview

Kernel is a simple microkernel that allows for Service's to be deployed within an application.

It manages the complete lifecycle of the application with muliple stages each called in sequence: Init, PostInit, Start & Run. Once the kernel gets to the Start phase then any error will cause the Stop phase to be invoked to allow any Started service to cleanup.

For most simple applications you can simply use kernel.Launch( s ) where s is an uninitiated service and it will create a Kernel add that service and run it.

For more complex applications which need multiple unrelated services deployed then it can do by calling NewKernel() to create a new kernel, add each one via AddService() and then call Run() - this is what Launch() does internally.

A Service is simply an Object implementing the Service interface and one or more of the various lifecycle interfaces.

If a service has dependencies then it should implement Init() and call AddService to add them - the kernel will handle the rest.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Launch

func Launch(services ...Service) error

Launch is a convenience method to launch a single service. This does the boiler plate work and requires the single service adds any dependencies within it's Init() method, if any

Types

type InitialisableService

type InitialisableService interface {
	// Init initialises a Service when it's added to the Kernel
	Init(*Kernel) error
}

A Service that expects to be called in the Init lifecycle phase

type Kernel

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

Kernel is the core container for deployed services

func (*Kernel) AddService

func (k *Kernel) AddService(s Service) (Service, error)

AddService adds a service to the kernel

type MemUsage

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

MemUsage is a Kernel service which will log on shutdown the duration of the process and how much memory it has used.

To use simply include it as the first service when launching the kernel:

func main() {
  err := kernel.Launch( &kernel.MemUsage{}, &mylib.MyService{} )
  if err != nil {
    log.Fatal( err )
  }
}

When the service stops then some statistics are logged showing how long the process has run, how much memory it's used and how often the garbage collector has run.

Notes:

The process duration time is from when the Start phase begins. If the kernel fails before that then no stats are generated. This is because services only get stopped if they have started.

func (*MemUsage) Name

func (m *MemUsage) Name() string

func (*MemUsage) Start

func (m *MemUsage) Start() error

Starts the service. The process duration time reported is from when this is called.

func (*MemUsage) Stop

func (m *MemUsage) Stop()

Stops the service

type PostInitialisableService

type PostInitialisableService interface {
	// Init initialises a Service when it's added to the Kernel
	PostInit() error
}

A Service that expects to be called in the PostInit lifecycle phase

type RunnableService

type RunnableService interface {
	// Run executes the service
	Run() error
}

A Service that is expected to run in the Run lifecycle phase

type Service

type Service interface {
	// Name returns the unique name of this service
	Name() string
}

Service to be deployed within the Kernel

type StartableService

type StartableService interface {
	// Start called when the Kernel starts but before services Run
	Start() error
}

A Service that expects to be called in the Start lifecycle phase

type StoppableService

type StoppableService interface {
	Stop()
}

A Service that expects to be called when the kernel shutsdown if it's in the Start or Run lifecycle phases

Directories

Path Synopsis
A simple kernel service wich provides access to a single github.com/etcd-io/bbolt object store
A simple kernel service wich provides access to a single github.com/etcd-io/bbolt object store
This provides the Kernel a managed Cron service.
This provides the Kernel a managed Cron service.

Jump to

Keyboard shortcuts

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