servicemanager

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrServiceNotFound    = errors.New("service not found")
	ErrNoEnabledServices  = errors.New("no enabled services")
	ErrCyclicDependency   = errors.New("cyclic dependency detected")
	ErrDependencyNotFound = errors.New("dependency not found")
	ErrMaxRetriesReached  = errors.New("max retries reached")
	ErrStopTimeout        = errors.New("service stop timed out")
	ErrServicePanic       = errors.New("service panicked")
	ErrNoCommands         = errors.New("service has no commands")
)

Functions

func ResetInstance

func ResetInstance()

Types

type AllowedFailure

type AllowedFailure interface {
	IsAllowedFailure() bool
}

AllowedFailure is optionally implemented by services whose failure should not bring down the entire service manager.

type AllowedFailureMockService

type AllowedFailureMockService struct {
	*MockService
}

func NewAllowedFailureMockService

func NewAllowedFailureMockService(
	name string,
) *AllowedFailureMockService

func (*AllowedFailureMockService) IsAllowedFailure

func (a *AllowedFailureMockService) IsAllowedFailure() bool

type Commander

type Commander interface {
	Commands() []*cobra.Command
}

Commander is optionally implemented by services that expose CLI subcommands. The returned commands are added under the service name: ./app <servicename> <subcommand>.

type Dependent

type Dependent interface {
	Dependencies() []string
}

Dependent is optionally implemented by services that must start after other services. Dependencies returns the names of services this service depends on.

type DependentMockService

type DependentMockService struct {
	*MockService
	// contains filtered or unexported fields
}

func NewDependentMockService

func NewDependentMockService(
	name string,
	deps ...string,
) *DependentMockService

func (*DependentMockService) Dependencies

func (d *DependentMockService) Dependencies() []string

type FullMockService

type FullMockService struct {
	*MockService
	// contains filtered or unexported fields
}

func NewFullMockService

func NewFullMockService(name string) *FullMockService

func (*FullMockService) Dependencies

func (f *FullMockService) Dependencies() []string

func (*FullMockService) IsAllowedFailure

func (f *FullMockService) IsAllowedFailure() bool

func (*FullMockService) MaxRetries

func (f *FullMockService) MaxRetries() int

func (*FullMockService) RetryDelay

func (f *FullMockService) RetryDelay() time.Duration

func (*FullMockService) WithAllowFailure

func (f *FullMockService) WithAllowFailure(
	v bool,
) *FullMockService

func (*FullMockService) WithDependencies

func (f *FullMockService) WithDependencies(
	deps ...string,
) *FullMockService

func (*FullMockService) WithMaxRetries

func (f *FullMockService) WithMaxRetries(
	n int,
) *FullMockService

func (*FullMockService) WithRetryDelay

func (f *FullMockService) WithRetryDelay(
	d time.Duration,
) *FullMockService

type MockService

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

func NewMockService

func NewMockService(name string) *MockService

func (*MockService) IsRunning

func (m *MockService) IsRunning() bool

func (*MockService) Name

func (m *MockService) Name() string

func (*MockService) Run

func (m *MockService) Run(ctx context.Context) error

func (*MockService) RunCount

func (m *MockService) RunCount() int

func (*MockService) Stop

func (m *MockService) Stop(_ context.Context) error

func (*MockService) WasRunCalled

func (m *MockService) WasRunCalled() bool

func (*MockService) WasStopCalled

func (m *MockService) WasStopCalled() bool

func (*MockService) WithOnRun

func (m *MockService) WithOnRun(
	fn func(),
) *MockService

func (*MockService) WithRunDelay

func (m *MockService) WithRunDelay(
	delay time.Duration,
) *MockService

func (*MockService) WithRunError

func (m *MockService) WithRunError(
	err error,
) *MockService

func (*MockService) WithRunErrors

func (m *MockService) WithRunErrors(
	errs ...error,
) *MockService

func (*MockService) WithStopError

func (m *MockService) WithStopError(
	err error,
) *MockService

type ReadyMockService

type ReadyMockService struct {
	*MockService
	// contains filtered or unexported fields
}

func NewReadyMockService

func NewReadyMockService(
	name string,
	deps ...string,
) *ReadyMockService

func (*ReadyMockService) Dependencies

func (r *ReadyMockService) Dependencies() []string

func (*ReadyMockService) Ready

func (r *ReadyMockService) Ready() <-chan struct{}

func (*ReadyMockService) SignalReady

func (r *ReadyMockService) SignalReady()

type ReadyNotifier

type ReadyNotifier interface {
	Ready() <-chan struct{}
}

ReadyNotifier is optionally implemented by services that need to signal when they're actually ready to serve. The service manager waits for the Ready channel to close before starting dependent services. Services that don't implement this are considered ready immediately after their goroutine is launched.

type Retryable

type Retryable interface {
	MaxRetries() int
	RetryDelay() time.Duration
}

Retryable is optionally implemented by services that want automatic restart on failure. MaxRetries returns the maximum number of retry attempts (0 means no retries). RetryDelay returns the delay between retries. Use human-readable durations like 1s, 2m, 1h30m via time.Duration.

type RetryableMockService

type RetryableMockService struct {
	*MockService
	// contains filtered or unexported fields
}

func NewRetryableMockService

func NewRetryableMockService(
	name string,
	maxRetries int,
) *RetryableMockService

func (*RetryableMockService) MaxRetries

func (r *RetryableMockService) MaxRetries() int

func (*RetryableMockService) RetryDelay

func (r *RetryableMockService) RetryDelay() time.Duration

func (*RetryableMockService) WithRetryDelay

func (r *RetryableMockService) WithRetryDelay(
	d time.Duration,
) *RetryableMockService

type Service

type Service interface {
	Name() string
	Run(ctx context.Context) error
	Stop(ctx context.Context) error
}

type ServiceFactory

type ServiceFactory func() (Service, error)

ServiceFactory is a function that creates a service instance.

type ServiceManager

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

func GetInstance

func GetInstance() *ServiceManager

func (*ServiceManager) Add

func (s *ServiceManager) Add(services ...Service)

func (*ServiceManager) ClearServices

func (s *ServiceManager) ClearServices()

func (*ServiceManager) Commands

func (s *ServiceManager) Commands() []*cobra.Command

Commands returns lazy cobra commands for each registered service factory. Each parent command instantiates only its own service when invoked, so ./app <service> <subcommand> doesn't trigger initialization of all services.

func (*ServiceManager) Instantiate

func (s *ServiceManager) Instantiate(
	name string,
) (Service, error)

Instantiate creates a single service by calling its factory. Used for Commander commands that need only one service.

func (*ServiceManager) Register

func (s *ServiceManager) Register(
	name string,
	factory ServiceFactory,
)

Register stores a service factory for lazy instantiation. The factory is only called when the service is actually needed (during Run or when a Commander command is invoked).

func (*ServiceManager) RegisteredNames

func (s *ServiceManager) RegisteredNames() []string

RegisteredNames returns the names of all registered service factories.

func (*ServiceManager) Run

func (s *ServiceManager) Run(ctx context.Context) error

func (*ServiceManager) Stop

func (s *ServiceManager) Stop(ctx context.Context)

type TestService

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

func NewTestService

func NewTestService(name string) *TestService

func (*TestService) Name

func (s *TestService) Name() string

func (*TestService) Run

func (s *TestService) Run(ctx context.Context) error

func (*TestService) Stop

func (s *TestService) Stop(_ context.Context) error

Jump to

Keyboard shortcuts

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