starter

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 8 Imported by: 0

README

Starter

Build Status GoDoc Go Report Card Software License

This stable version.

Description

This is a package for organizing the launch of services inside the application with convenient initialization of components.

  • The component can be initialized
  • The services can be started and stopped
import (
	"github.com/urfave/cli/v2"
)

type Component interface {
	Name() string
	Init(ctx *cli.Context) error
	Destroy(ctx *cli.Context) error
}

type Service interface {
	Name() string
	Start(ctx *cli.Context) error
	Stop(ctx *cli.Context) error
}
Supports
  • Signals for graceful shutdown
  • Init functions
  • Components
  • Service startup
  • Customer logger

Installation

$ go get github.com/mantyr/starter

Example

package main

import (
	"syscall"

	"github.com/urfave/cli/v2"
	"github.com/mantyr/starter"

	"service1"
	"service2"
)

func main() {
	var ctx cli.Context
	ctx.Set("server.gracefulstop.duration", "30m")

	s1, err := service1.New()
	if err != nil {
		panic(err)
	}
	s2, err := service2.New()
	if err != nil {
		panic(err)
	}

	s := starter.New()
	s.Signals(
		syscall.SIGINT,
		syscall.SIGTERM,
	).Init(
		ctx,
		db,
		s1,
		s2,
		starter.NewComponent("service3").SetInit(func() error{return nil}).SetDestroy(func() error{return nil}),
	).RunServices(
		ctx,
		s1,
		s2,
	).Wait()

Author

Oleg Shevelev

Documentation

Index

Constants

View Source
const (
	ServicesGracefulstopTimeout string = "services.gracefulstop.timeout"
)

Variables

View Source
var Flags = []cli.Flag{
	altsrc.NewDurationFlag(&cli.DurationFlag{
		Name:    ServicesGracefulstopTimeout,
		Usage:   "Maximum time to gracefully stop services",
		EnvVars: []string{"SERVICES_GRACEFULSTOP_TIMEOUT"},
	}),
}

Functions

This section is empty.

Types

type Component

type Component interface {
	Name() string
	Init(ctx *cli.Context) error
	Destroy(ctx *cli.Context) error
}

type CompositeComponent added in v0.0.2

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

func NewComponent

func NewComponent(name string) *CompositeComponent

func (*CompositeComponent) Destroy added in v0.0.2

func (c *CompositeComponent) Destroy(ctx *cli.Context) error

func (*CompositeComponent) Init added in v0.0.2

func (c *CompositeComponent) Init(ctx *cli.Context) error

func (*CompositeComponent) Name added in v0.0.2

func (c *CompositeComponent) Name() string

func (*CompositeComponent) SetDestroy added in v0.0.2

func (*CompositeComponent) SetInit added in v0.0.2

type DestroyFunc added in v0.0.2

type DestroyFunc func(ctx *cli.Context) error

type Func

type Func func(ctx *cli.Context, parent context.Context) error

type InitFunc added in v0.0.2

type InitFunc func(ctx *cli.Context) error

type Logger added in v0.1.0

type Logger interface {
	Println(v ...any)
	Printf(format string, v ...any)
}

type Service

type Service interface {
	Name() string
	Start(ctx *cli.Context) error
	Stop(ctx *cli.Context) error
}

type Starter

type Starter interface {
	// Logger set customer logger
	Logger(l Logger) Starter

	// Signals
	Signals(signals ...os.Signal) Starter

	// Init ...
	Init(ctx *cli.Context, components ...Component) Starter

	// Run ...
	Run(ctx *cli.Context, f Func) Starter

	// RunServices ...
	RunServices(ctx *cli.Context, services ...Service) Starter

	// Done
	Done() <-chan struct{}

	// Stop ...
	Stop() Starter

	// Wait ...
	Wait(ctx *cli.Context) Starter

	// Error return pipeline error
	Error() error
}

func New

func New() (Starter, error)

Jump to

Keyboard shortcuts

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