Documentation ¶
Overview ¶
Package ssw is a wrapper for any service that would abstract a service
Index ¶
Examples ¶
Constants ¶
const ErrServiceAlreadyRanOnce = "service already ran once, cannot run again"
ErrServiceAlreadyRanOnce tells us that the service already ran and you cannot run it again, if you need to run it again you need to create a new objecct
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientStart ¶
type ClientStart interface {
Start() error
}
ClientStart for clients that are able to start
type ClientStop ¶
type ClientStop interface {
Stop() error
}
ClientStop for clients that are able to start, the client should make sure that the stop is blocking and it will only continue after it has successfully shutdown
type Config ¶
type Config struct { Timeout int `env:"SERVICE_TIMEOUT_MS" envDefault:"500"` DelayStart time.Duration `env:"SERVICE_DELAY_START" envDefault:"0"` TickInterval time.Duration `env:"SERVICE_TICK_INTERVAL" envDefault:"1s"` }
Config contains configuration for service
type HandlerError ¶
HandlerError is a function that will get called if there is an error, here you can decide if you want to terminate the application or not
type HandlerGeneric ¶
type HandlerGeneric func() error
HandlerGeneric is a generic function that will be executed at various points
type HandlerInterrupt ¶
HandlerInterrupt is a generic function that is executed on a specific signal
type Service ¶
type Service struct { HandleStart HandlerGeneric HandleStop HandlerGeneric HandleError HandlerError // contains filtered or unexported fields }
Service represents a service handling requests, what it does it bootstraps the process and wraps it in a nice package where you don't have to worry about starting/stopping the services
func New ¶
New creates a new instance of service with provided configuration and details
Example ¶
package main import ( "fmt" "github.com/ilijamt/ssw" "go.uber.org/zap" "os" "syscall" ) func main() { cfg := ssw.NewConfig() version := ssw.NewVersion("Test", "Desc", "Ver", "Hash", "Date", "Clean") svc := ssw.WithLogger( ssw.New("Test", cfg, version), zap.NewNop(), ) svc.HandleStart = func() error { fmt.Println("Handle start") return nil } svc.HandleStop = func() error { fmt.Println("Handle stop") return nil } svc.HandleError = func(err error) error { return err } handler := func(signal os.Signal) error { fmt.Printf("We have a %s, we can do whatever we want here", signal) return nil } svc.Handler(handler, syscall.SIGHUP, syscall.SIGUSR1) // This will start the service and register all handlers and everything svc.Run() }
Output:
func WithLogger ¶
WithLogger wraps the service with a logger
func (*Service) Handler ¶
func (o *Service) Handler(fn HandlerInterrupt, signals ...os.Signal)
Handler add a specific function that will run when the signal is detected, which function will not initiate the stopping of the application, so it can be triggered as many times as necessary
func (*Service) Run ¶
Run runs the service and waits for interruption signal, or the signal handlers
func (*Service) SendSignal ¶
SendSignal send a signal to the handler if we want to manually trigger a handler without waiting for it from the OS
type Version ¶
type Version struct { // Name of the application Name string // Description is the description of the application Description string // Version is the version of the build Version string // Hash is the hash of the version Hash string // Date is the time of when the application was built Date string // Clean is the binary built from a clean source tree Clean string }
Version contains all the necessary information about a build
func NewVersion ¶
func NewVersion(name string, desc string, ver string, hash string, date string, clean string) *Version
NewVersion returns a new Version
func (*Version) GetDetails ¶
GetDetails gets the formatted details
func (*Version) GetVersion ¶
GetVersion gets the version string formatted