lifecycle

package
v0.1.1-pprof-4 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: GPL-3.0 Imports: 7 Imported by: 0

README

Service Lifecycle

This package provides useful method to manage the lifecycle of a service or a group of services with convenient initializations of components.

Interface

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

Feature

  • Start and stop a group of services
  • Monitor signals to stop services
  • Graceful shutdown

Example

package main

import (
	"context"
	"syscall"
	"time"

	"github.com/bnb-chain/greenfield-storage-provider/pkg/lifecycle"
	"http_server"
	"rpc_server"
)

func main() {
	ctx := context.Background()
	l := lifecycle.NewService(5 * time.Second)
	l.RegisterServices(http_server, rpc_server)
	l.Signals(syscall.SIGINT, syscall.SIGTERM).Init(ctx).StartServices(ctx).Wait(ctx)
}

Documentation

Index

Constants

View Source
const StopTimeout = 30

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service interface {
	// Name describe service name
	Name() string
	// Start a service, this method should be used in non-block form
	Start(ctx context.Context) error
	// Stop a service, this method should be used in non-block form
	Stop(ctx context.Context) error
}

Service provides abstract methods to control the lifecycle of a service

type ServiceLifecycle

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

ServiceLifecycle manages services' lifecycle

func NewServiceLifecycle

func NewServiceLifecycle() *ServiceLifecycle

NewServiceLifecycle returns a singleton instance of ServiceLifecycle

func (*ServiceLifecycle) Done

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

Done check context is done

func (*ServiceLifecycle) RegisterServices

func (s *ServiceLifecycle) RegisterServices(services ...Service)

RegisterServices register services of an application

func (*ServiceLifecycle) Signals

func (s *ServiceLifecycle) Signals(sigs ...os.Signal) *ServiceLifecycle

Signals registers monitor signals

func (*ServiceLifecycle) StartServices

func (s *ServiceLifecycle) StartServices(ctx context.Context) *ServiceLifecycle

StartServices starts running services

func (*ServiceLifecycle) StopServices

func (s *ServiceLifecycle) StopServices(ctx context.Context)

StopServices stop services when context is done or timeout

func (*ServiceLifecycle) Wait

func (s *ServiceLifecycle) Wait(ctx context.Context)

Wait blocks until context is done

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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