lifecycle

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 7 Imported by: 2

README

golly/lifecycle

Go Reference

Overview

The golly/lifecycle package provides a lifecycle management library for Go applications. It allows you to easily manage the lifecycle of your application components, such as starting and stopping them in a controlled manner.

Installation

To install the package, use the go get command:

go get oss.nandlabs.io/golly/lifecycle

Usage

The golly/lifecycle package provides a simple and flexible way to manage the lifecycle of your application components. It allows you to start and stop components in a controlled manner, ensuring that they are properly initialized and cleaned up.

Simple Component

The simple_component.go file contains a simple implementation of a component that can be used with the golly/lifecycle package. This component demonstrates the basic structure and behavior of a component.

To use the SimpleComponent, follow these steps:

  1. Import the golly/lifecycle package and the simple_component.go file.
import (
    "oss.nandlabs.io/golly/lifecycle"
    "oss.nandlabs.io/golly/lifecycle/examples/simple_component"
)
  1. Create an instance of the SimpleComponent struct.
simpleComponent := &simple_component.SimpleComponent{}
  1. Add the SimpleComponent to the Lifecycle struct.
lifecycle.AddComponent(simpleComponent)
  1. Start and stop the components using the Start and Stop methods of the Lifecycle struct.
err := lifecycle.Start()
if err != nil {
    // handle start error
}

// ...

err = lifecycle.Stop()
if err != nil {
    // handle stop error
}

By following these steps, you can use the SimpleComponent in your application and manage its lifecycle along with other components in the golly/lifecycle package.

Cusom Components

The component.go file contains the interfaces that define the behavior of components in the golly/lifecycle package. These interfaces allow you to create custom components and integrate them into the lifecycle management system.

To use the component.go interfaces, follow these steps:

  1. Implement the Component interface in your custom component struct. This interface defines the Start and Stop methods that will be called when the component is started or stopped.
type MyComponent struct {
    // component fields
}

func (c *MyComponent) Start() error {
    // implementation of start logic
    return nil
}

func (c *MyComponent) Stop() error {
    // implementation of stop logic
    return nil
}
  1. Create an instance of your custom component and add it to the Lifecycle struct. The Lifecycle struct manages the lifecycle of all registered components.
lifecycle := &lifecycle.Lifecycle{}

myComponent := &MyComponent{}
lifecycle.AddComponent(myComponent)
  1. Start and stop the components using the Start and Stop methods of the Lifecycle struct.
err := lifecycle.Start()
if err != nil {
    // handle start error
}

// ...

err = lifecycle.Stop()
if err != nil {
    // handle stop error
}

By following these steps, you can integrate your custom components into the golly/lifecycle package and manage their lifecycle in a controlled manner.

For more information, refer to the GoDoc documentation.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCompAlreadyStarted = errors.New("component already started")
View Source
var ErrCompAlreadyStopped = errors.New("component already stopped")
View Source
var ErrCompNotFound = errors.New("component not found")
View Source
var ErrInvalidComponentState = errors.New("invalid component state")

Functions

This section is empty.

Types

type Component

type Component interface {
	// Id is the unique identifier for the component.
	Id() string
	//OnChange is the function that will be called when the component state changes.
	OnChange(prevState, newState ComponentState)
	// Start will starting the LifeCycle.
	Start() error
	// Stop will stop the LifeCycle.
	Stop() error
	// State will return the current state of the LifeCycle.
	State() ComponentState
}

Component is the interface that wraps the basic Start and Stop methods.

type ComponentManager

type ComponentManager interface {
	// GetState will return the current state of the LifeCycle for the component with the given id.
	GetState(id string) ComponentState
	//List will return a list of all the Components.
	List() []Component
	// Register will register a new Components.
	Register(component Component) Component
	// StartAll will start all the Components. Returns the number of components started
	StartAll() error
	//StartAndWait will start all the Components and wait for them to finish.
	StartAndWait()
	// Start will start the LifeCycle for the component with the given id.
	// It returns an error if the component was not found or if the component failed to start.
	Start(id string) error
	// StopAll will stop all the Components.
	StopAll() error
	// Stop will stop the LifeCycle for the component with the given id. It returns if the component was stopped.
	Stop(id string) error
	// Unregister will unregister a Component.
	Unregister(id string)
	// Wait will wait for all the Components to finish.
	Wait()
}

ComponentManager is the interface that manages multiple components.

func NewSimpleComponentManager

func NewSimpleComponentManager() ComponentManager

NewSimpleComponentManager will return a new SimpleComponentManager.

type ComponentState

type ComponentState int
const (
	// Unknown is the state of the component when it is not known.
	Unknown ComponentState = iota
	// Error is the state of the component when it is in error.
	Error
	// Stopped is the state of the component when it is stopped.
	Stopped
	//Stopping is the state of the component when it is stopping.
	Stopping
	// Running is the state of the component when it is running.
	Running
	// Starting is the state of the component when it is starting.
	Starting
)

type SimpleComponent

type SimpleComponent struct {
	CompId string
	// AfterStart is the function that will be called after the component is started
	// The function will be called with the error returned by the StartFunc.
	AfterStart func(err error)
	// BeforeStart is the function that will be called before the component is started
	BeforeStart func()
	// AfterStop is the function that will be called after the component is stopped
	// The function will be called with the error returned by the StopFunc.
	AfterStop func(err error)
	// BeforeStop is the function that will be called before the component is stopped.
	BeforeStop func()
	// CompState is the current state of the component.
	CompState ComponentState
	// OnStateChange is the function that will be called when the component state changes.
	OnStateChange func(prevState, newState ComponentState)
	//StartFunc is the function that will be called when the component is started.
	// It returns an error if the component failed to start.
	StartFunc func() error
	// StopFunc is the function that will be called when the component is stopped.
	// It returns an error if the component failed to stop.
	StopFunc func() error
}

SimpleComponent is the struct that implements the Component interface.

func (*SimpleComponent) Id

func (sc *SimpleComponent) Id() string

ComponentId is the unique identifier for the component.

func (*SimpleComponent) OnChange

func (sc *SimpleComponent) OnChange(prevState, newState ComponentState)

OnChange is the function that will be called when the component state changes.

func (*SimpleComponent) Start

func (sc *SimpleComponent) Start() (err error)

Start will starting the LifeCycle.

func (*SimpleComponent) State

func (sc *SimpleComponent) State() ComponentState

State will return the current state of the LifeCycle.

func (*SimpleComponent) Stop

func (sc *SimpleComponent) Stop() (err error)

Stop will stop the LifeCycle.

type SimpleComponentManager

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

SimpleComponentManager is the struct that manages the component.

func (*SimpleComponentManager) GetState

func (scm *SimpleComponentManager) GetState(id string) ComponentState

GetState will return the current state of the LifeCycle for the component with the given id.

func (*SimpleComponentManager) List

func (scm *SimpleComponentManager) List() []Component

List will return a list of all the Components.

func (*SimpleComponentManager) Register

func (scm *SimpleComponentManager) Register(component Component) Component

Register will register a new Components. if the component is already registered, get the old component.

func (*SimpleComponentManager) Start

func (scm *SimpleComponentManager) Start(id string) (err error)

Start will start the LifeCycle for the component with the given id. It returns if the component was started.

func (*SimpleComponentManager) StartAll

func (scm *SimpleComponentManager) StartAll() error

StartAll will start all the Components. Returns the number of components started

func (*SimpleComponentManager) StartAndWait

func (scm *SimpleComponentManager) StartAndWait()

StartAndWait will start all the Components. And will wait for them to be stopped.

func (*SimpleComponentManager) Stop

func (scm *SimpleComponentManager) Stop(id string) error

Stop will stop the LifeCycle for the component with the given id. It returns if the component was stopped.

func (*SimpleComponentManager) StopAll

func (scm *SimpleComponentManager) StopAll() error

StopAll will stop all the Components.

func (*SimpleComponentManager) Unregister

func (scm *SimpleComponentManager) Unregister(id string)

Unregister will unregister a Component.

func (*SimpleComponentManager) Wait

func (scm *SimpleComponentManager) Wait()

Wait will wait for all the Components to finish.

Jump to

Keyboard shortcuts

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