service

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 22 Imported by: 3

Documentation

Overview

Package service provides a framework for creating and managing service processes with support for configuration loading, lifecycle management, and component handling. It offers flexible configuration options, including file-based, HTTP-based, and stdin-based configuration loading, as well as template processing capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(opts ...RunOption)

Run is a convenience function for running a service with a default configuration. It creates and runs a BaseService with an empty context. This function always exits the program: - It exits with the error code if an error occurs. - It exits with code 0 if the service runs successfully. It is recommended to use this function unless you need to customize the Service or want to prevent the program from exiting.

func RunService

func RunService(s Service) error

RunService starts and manages the lifecycle of the given service. It handles initialization, starting, shutdown, and uninitialization of the service. This function returns any error encountered during the service lifecycle. Use this function if you need to run a custom Service implementation or if you want to handle errors without exiting the program.

Types

type BaseService

type BaseService[T any] struct {
	// contains filtered or unexported fields
}

BaseService implements the Service interface with a generic context type T.

func NewBaseService

func NewBaseService[T any](config Config[T]) *BaseService[T]

NewBaseService creates a new BaseService with the given configuration.

func (*BaseService[T]) Config

func (s *BaseService[T]) Config() *Config[T]

Config returns the current configuration.

func (*BaseService[T]) GetComponent

func (s *BaseService[T]) GetComponent(uuid string) component.Component

GetComponent returns a component by its UUID.

func (*BaseService[T]) Init

func (s *BaseService[T]) Init(ctx context.Context) error

Init implements the Service Init method, setting up logging and initializing components.

func (*BaseService[T]) Logger

func (s *BaseService[T]) Logger() *slog.Logger

Logger returns the logger instance for the service.

func (*BaseService[T]) SetVersionFunc

func (s *BaseService[T]) SetVersionFunc(f func())

SetVersionFunc sets the version function to be called when the service is started with the -v flag. If set to nil, the version function is disabled.

func (*BaseService[T]) Shutdown

func (s *BaseService[T]) Shutdown(ctx context.Context) error

Shutdown implements the Service Shutdown method, shutting down all components.

func (*BaseService[T]) Start

func (s *BaseService[T]) Start(ctx context.Context) error

Start implements the Service Start method, starting all components.

func (*BaseService[T]) Uninit

func (s *BaseService[T]) Uninit(ctx context.Context) error

Uninit implements the Service Uninit method, uninitializing all components.

type Config

type Config[T any] struct {
	Context    T                  `json:",omitempty"`
	Components []component.Config `json:",omitempty"`
}

Config represents a generic configuration structure for services. It includes a context of type T and a list of component configurations.

type RunOption

type RunOption func(*runOptions)

RunOption is a functional option for configuring the Run function.

func WithDecoder

func WithDecoder(decoder encoding.Decoder) RunOption

WithDecoder sets the decoder function for the Run function.

func WithEncoder

func WithEncoder(encoder encoding.Encoder) RunOption

WithEncoder sets the encoder function for the Run function.

type Service

type Service interface {
	lifecycle.Lifecycle
	component.Container

	// Logger returns the logger instance for the service.
	Logger() *slog.Logger
}

Service represents a process with lifecycle management and component handling capabilities.

Jump to

Keyboard shortcuts

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