spokes

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2021 License: MIT Imports: 10 Imported by: 0

README

Spokes - application life cycle manager.

Go Report Card Go Doc codecov

Spokes is a tools that creates a long-running service process, managing the live-cycle of them.

Spokes will take care of:

  1. Initialization phase of a top level service
  2. Register sub service.
  3. Run initiate and run services
  4. Shutdown take care of gently stop sub services and close service

Usage

with context

one of use cased is that your service can use context.Done() to terminate itself.

package main

import (
	"context"
	"time"

	"github.com/golang-tire/spokes"
	"go.uber.org/zap"
)

var _ spokes.Service = (*sampleService)(nil)

type sampleService struct{}

func (d *sampleService) Init(ctx context.Context, logger *zap.Logger) error {

	go func() {
		<-ctx.Done()
		// close ...
		logger.Info("context cancel signal received")
	}()

	return nil
}
func (d *sampleService) Shutdown() error { return nil }
func (d *sampleService) Run() error {
	time.Sleep(time.Second * 10)
	return nil
}

func main() {

	ctx := context.Background()
	s, err := spokes.New("service", "v0.1.0", spokes.WithContext(ctx))
	spokes.Panic(err)

	d := &sampleService{}
	err = s.Register("dummy-service", d)
	spokes.Panic(err)

	err = s.Run()
	spokes.Panic(err)
}
without context
package main

import (
	"context"

	"github.com/golang-tire/spokes"
	"go.uber.org/zap"
)
var _ spokes.Service = (*sampleService)(nil)

type sampleService struct{}

func (d *sampleService) Init(ctx context.Context, logger *zap.Logger) error { return nil }
func (d *sampleService) Shutdown() error               { return nil }
func (d *sampleService) Run() error                    { return nil }

func main() {
	s, err := spokes.New("service", "v0.1.0")
	spokes.Panic(err)

	d := &sampleService{}
	err = s.Register("dummy-service", d)
	spokes.Panic(err)

	err = s.Run()
	spokes.Panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Panic

func Panic(err error)

Panic is a function to check and fail on errors

Types

type Option

type Option func(*Spokes) error

Option defines Spokes option type.

func WithConsoleLogger

func WithConsoleLogger(level zapcore.Level) Option

WithConsoleLogger is an option that uses a zap Logger with to be used for debugging in the console

func WithContext

func WithContext(ctx context.Context) Option

WithContext is an option that allows you provide a context to use in service

func WithDevelopmentLogger

func WithDevelopmentLogger() Option

WithDevelopmentLogger is an option that uses a zap Logger with development configurations

func WithLogger

func WithLogger(logger *zap.Logger) Option

WithLogger is an option that allows you to provide your own customized logger.

func WithProductionLogger

func WithProductionLogger() Option

WithProductionLogger is an option that uses a zap Logger with production configurations

func WithStackedLogger

func WithStackedLogger(level zapcore.Level) Option

WithStackedLogger is an option that uses a zap production Logger compliant with the GCP/Stackdriver format.

type Service

type Service interface {
	Init(ctx context.Context, logger *zap.Logger) error
	Run() error
	Shutdown() error
}

Service defines a Spokes service.

type Spokes

type Spokes struct {
	Name    string
	Version string
	// contains filtered or unexported fields
}

Spokes defines the services life-cycle manager.

func New

func New(name, version string, opts ...Option) (*Spokes, error)

New instantiates a new Spokes service

func (*Spokes) Logger

func (s *Spokes) Logger() *zap.Logger

Logger returns the spokes logger. will be nil if New failed

func (*Spokes) Register

func (s *Spokes) Register(name string, service Service) error

func (*Spokes) Run

func (s *Spokes) Run() error

Run run the service. will run Init function of each sub service in order and Run functions

func (*Spokes) Services

func (s *Spokes) Services() []string

Top Services list of registered services

func (*Spokes) Shutdown

func (s *Spokes) Shutdown()

Shutdown will terminate service with os SIGTERM

func (*Spokes) Top

func (s *Spokes) Top() []string

Top like linux top but just the name of running services

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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