# README
golly/lifecycle
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:
- Import the
golly/lifecycle
package and thesimple_component.go
file.
import (
"oss.nandlabs.io/golly/lifecycle"
"oss.nandlabs.io/golly/lifecycle/examples/simple_component"
)
- Create an instance of the
SimpleComponent
struct.
simpleComponent := &simple_component.SimpleComponent{}
- Add the
SimpleComponent
to theLifecycle
struct.
lifecycle.AddComponent(simpleComponent)
- Start and stop the components using the
Start
andStop
methods of theLifecycle
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:
- Implement the
Component
interface in your custom component struct. This interface defines theStart
andStop
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
}
- Create an instance of your custom component and add it to the
Lifecycle
struct. TheLifecycle
struct manages the lifecycle of all registered components.
lifecycle := &lifecycle.Lifecycle{}
myComponent := &MyComponent{}
lifecycle.AddComponent(myComponent)
- Start and stop the components using the
Start
andStop
methods of theLifecycle
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.