service

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MIT Imports: 20 Imported by: 1

Documentation

Overview

Package service builds on github.com/kardianos/service to run sidecars as OS services/daemons

Index

Constants

View Source
const (
	ExitStatusOK    int = iota // ExitStatusOK indicates everything is fine
	ExitStatusMinor            // ExitStatusMinor indicates there was a minor problem
	ExitStatusMajor            // ExitStatusMajor indicates there was a major problem
)

Variables

This section is empty.

Functions

func AsExitStatus

func AsExitStatus(err error) (int, error)

AsExitStatus looks at an Execute() error and decides how the process should finish

func CmdName

func CmdName() string

CmdName returns the arg[0] of this execution

func WithService added in v0.1.0

func WithService(ctx context.Context, svc *Service) context.Context

WithService stores the given Service on a sub-context.

Types

type Config

type Config struct {
	service.Config

	// SanityDelay indicates how long we wait for
	// the run command to fail before claiming a
	// successful start
	SanityDelay time.Duration `default:"2s"`
}

Config describes the Service we are assembling

func (*Config) GetBoolOption added in v0.1.0

func (cfg *Config) GetBoolOption(name string, defaultValue bool) bool

GetBoolOption gets a boolean option from the service.Config's KeyValue store.

func (*Config) GetEnv added in v0.1.0

func (cfg *Config) GetEnv(name, defaultValue string) string

GetEnv gets an environment variable from the service.Config.

func (*Config) GetOption added in v0.1.0

func (cfg *Config) GetOption(name string) (any, bool)

GetOption gets an arbitrary option from the service.Config's KeyValue store.

func (*Config) GetStringOption added in v0.1.0

func (cfg *Config) GetStringOption(name, defaultValue string) string

GetStringOption gets a string option from the service.Config's KeyValue store.

func (*Config) SetBoolOption added in v0.1.0

func (cfg *Config) SetBoolOption(name string, value bool)

SetBoolOption sets a boolean option to the service.Config's KeyValue store.

func (*Config) SetDefaults

func (cfg *Config) SetDefaults()

SetDefaults fills gaps in the Config

func (*Config) SetEnv added in v0.1.0

func (cfg *Config) SetEnv(name, value string)

SetEnv sets an environment variable on the service.Config.

func (*Config) SetOption added in v0.1.0

func (cfg *Config) SetOption(name string, value any)

SetOption sets an arbitrary option on the service.Config's KeyValue store.

func (*Config) SetStringOption added in v0.1.0

func (cfg *Config) SetStringOption(name, value string)

SetStringOption sets a string option to the service.Config's KeyValue store.

type ErrorExitCode

type ErrorExitCode struct {
	Code int
	Err  error
}

ErrorExitCode is an error wrapper that knows how the application should exit

func (ErrorExitCode) Error

func (e ErrorExitCode) Error() string

func (ErrorExitCode) ExitStatus

func (e ErrorExitCode) ExitStatus() int

ExitStatus tells the code for os.Exit()

func (ErrorExitCode) Unwrap

func (e ErrorExitCode) Unwrap() error

type Service

type Service struct {
	Config Config
	// contains filtered or unexported fields
}

Service is an application that runs supervised by the OS

func Build added in v0.1.0

func Build(rootCmd, serveCmd *cobra.Command) (*Service, error)

Build creates a new service from a given root and serve cobra.Command. service.Config parameters can be modified until Execute is called or via PersistentPreRunE on the root command.

func GetService added in v0.1.0

func GetService(ctx context.Context) (*Service, bool)

GetService gets the current Service from the context.

func MustBuild added in v0.1.0

func MustBuild(rootCmd, serveCmd *cobra.Command) *Service

MustBuild creates a new service from a given root and serve cobra.Command, and panics if there is a problem.

func (*Service) AddCommand

func (s *Service) AddCommand(cmd *cobra.Command)

AddCommand adds an extra interactive command to the tool

func (*Service) Cancelled added in v0.1.0

func (s *Service) Cancelled() <-chan struct{}

Cancelled returns a channel that is closed when shutdown has been initiated.

func (*Service) Command

func (s *Service) Command() *cobra.Command

Command returns the root cobra.Command

func (*Service) Done added in v0.1.0

func (s *Service) Done() <-chan struct{}

Done returns a channel that is closed when the worker has finished.

func (*Service) Err

func (s *Service) Err() error

Err returns the recorded cancellation cause.

func (*Service) Execute

func (s *Service) Execute() error

Execute either runs via cobra or the service invoked process

func (*Service) ExecuteContext added in v0.1.0

func (s *Service) ExecuteContext(ctx context.Context) error

ExecuteContext either runs via cobra or the service invoked process

func (*Service) Interactive added in v0.1.0

func (s *Service) Interactive() bool

Interactive returns false if running under the OS service manager and true otherwise.

func (*Service) NewContext added in v0.1.0

func (s *Service) NewContext(ctx context.Context) (context.Context, error)

NewContext prepares a context for cmd.ExecuteContext() or cmd.SetContext()

func (*Service) PersistentFlags

func (s *Service) PersistentFlags() *pflag.FlagSet

PersistentFlags returns the flags also passed to sub-commands

func (*Service) Platform added in v0.1.0

func (s *Service) Platform() string

Platform returns a description of the system service.

func (*Service) SystemLogger added in v0.1.0

func (s *Service) SystemLogger() slog.Logger

SystemLogger returns the OS system logger. Entries will be recorded JSON encoded.

func (*Service) Wait

func (s *Service) Wait() error

Wait waits until the worker has finished and returns its error value

Jump to

Keyboard shortcuts

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