ssw

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 10 Imported by: 0

README

Simple Service Wrapper

Go Report Card Build Status License

Serves as a wrapper for simple service where full systemd integration is not needed.

It provides some basic functionality out of the box

  • Start/Stop of the application
  • Allows you to do action based on signals from the OS, like reloading configuration or activating/deactivating new functionality
  • Allows you to start and stop services if the implement the ClientStart and/or ClientStop interfaces
package main

import (
	"fmt"
	"os"
	"syscall"
	"github.com/ilijamt/ssw"
	"go.uber.org/zap"
)

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 {
        fmt.Println("Handle error")
		return nil
	}
	
	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
	if err := svc.Run(); err != nil {
		panic(err)
	}
}

Documentation

Overview

Package ssw is a wrapper for any service that would abstract a service

Index

Examples

Constants

View Source
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 Client

type Client interface {
	Name() string
}

Client generic interface

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

func NewConfig

func NewConfig() Config

NewConfig creates a new config instance from environment

type HandlerError

type HandlerError func(err error) error

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

type HandlerInterrupt func(signal os.Signal) error

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

func New(name string, cfg Config, details *Version, clients ...Client) *Service

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

func WithLogger(service *Service, logger *zap.Logger) *Service

WithLogger wraps the service with a logger

func (*Service) Close

func (o *Service) Close() error

Close notifies that the service wants to be closed

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

func (o *Service) Run() (err error)

Run runs the service and waits for interruption signal, or the signal handlers

func (*Service) SendSignal

func (o *Service) SendSignal(signal os.Signal) error

SendSignal send a signal to the handler if we want to manually trigger a handler without waiting for it from the OS

func (*Service) Start

func (o *Service) Start() error

Start is a wrapper around Run

func (*Service) Stop

func (o *Service) Stop() error

Stop is a wrapper around Close

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

func (bd *Version) GetDetails() string

GetDetails gets the formatted details

func (*Version) GetVersion

func (bd *Version) GetVersion() string

GetVersion gets the version string formatted

Jump to

Keyboard shortcuts

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