Documentation ¶
Index ¶
- Variables
- type Component
- type ComponentManager
- type ComponentState
- type SimpleComponent
- type SimpleComponentManager
- func (scm *SimpleComponentManager) GetState(id string) ComponentState
- func (scm *SimpleComponentManager) List() []Component
- func (scm *SimpleComponentManager) Register(component Component) Component
- func (scm *SimpleComponentManager) Start(id string) (err error)
- func (scm *SimpleComponentManager) StartAll() error
- func (scm *SimpleComponentManager) StartAndWait()
- func (scm *SimpleComponentManager) Stop(id string) error
- func (scm *SimpleComponentManager) StopAll() error
- func (scm *SimpleComponentManager) Unregister(id string)
- func (scm *SimpleComponentManager) Wait()
Constants ¶
This section is empty.
Variables ¶
var ErrCompAlreadyStarted = errors.New("component already started")
var ErrCompAlreadyStopped = errors.New("component already stopped")
var ErrCompNotFound = errors.New("component not found")
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.