kernel

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 19 Imported by: 89

README

go-kernel

A small microkernel got golang applications

This was originally part of the golib project but has now been split out as it's really a separate project in it's own right.

Documentation

Overview

Package 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 injectionPoints 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 injectionPoints within it's Init() method, if any

func Register

func Register(services ...Service)

Register will add the specified services to the kernel. If the kernel has been started then this will panic.

This is normally used within a packages' init() function to automatically deploy services.

func RegisterAPI

func RegisterAPI(api interface{}, service Service)

RegisterAPI registers an API

Types

type Daemon

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

func (*Daemon) ClearDaemon

func (d *Daemon) ClearDaemon()

func (*Daemon) IsDaemon

func (d *Daemon) IsDaemon() bool

func (*Daemon) IsWebserver

func (d *Daemon) IsWebserver() bool

func (*Daemon) SetDaemon

func (d *Daemon) SetDaemon()

func (*Daemon) SetWebserver

func (d *Daemon) SetWebserver()

type InitialisableService

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

InitialisableService 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

func (*Kernel) DependsOn

func (k *Kernel) DependsOn(services ...Service) error

DependsOn just adds injectionPoints on other services, it does not return the resolved Service's. This is short of _,err:=instance.AddService() for each dependency.

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) 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 NamedService

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

NamedService is the original Service where the Name() function returns the unique name. This is now optional, a service does not require Name() anymore.

type PostInitialisableService

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

PostInitialisableService a Service that expects to be called in the PostInit lifecycle phase

type RunnableService

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

RunnableService a Service that is expected to run in the Run lifecycle phase

type Service

type Service interface {
}

Service to be deployed within the Kernel

type StartableService

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

StartableService a Service that expects to be called in the Start lifecycle phase

type StoppableService

type StoppableService interface {
	Stop()
}

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

type Worker

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

func (*Worker) AddPriorityTask

func (w *Worker) AddPriorityTask(priority int, task task.Task) task.Queue

AddPriorityTask adds a task with a specific priority. Tasks with a higher priority value will run AFTER those with a lower value.

func (*Worker) AddTask

func (w *Worker) AddTask(task task.Task) task.Queue

AddTask adds a task with priority 0

func (*Worker) Run

func (w *Worker) Run() error

Run kernel stage. This just calls RunTasks()

func (*Worker) Start

func (w *Worker) Start() error

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
Package cron This provides the Kernel a managed Cron service.
Package cron This provides the Kernel a managed Cron service.
A simple amqp library for connecting to RabbitMQ
A simple amqp library for connecting to RabbitMQ
A basic REST server supporting HTTP.
A basic REST server supporting HTTP.
test

Jump to

Keyboard shortcuts

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