service

package
v0.0.0-...-01fc81e Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseService

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

Classical-inheritance-style service declarations. Services can be started, then stopped, but cannot be restarted.

Users must implement OnStart/OnStop methods. In the absence of errors, these methods are guaranteed to be called at most once. If OnStart returns an error, service won't be marked as started, so the user can call Start again.

The BaseService implementation ensures that the OnStop method is called after the context passed to Start is canceled.

Typical usage:

type FooService struct {
	BaseService
	// private fields
}

func NewFooService() *FooService {
	fs := &FooService{
		// init
	}
	fs.BaseService = *NewBaseService(log, "FooService", fs)
	return fs
}

func (fs *FooService) OnStart(ctx context.Context) error {
	// initialize private fields
	// start subroutines, etc.
}

func (fs *FooService) OnStop() {
	// close/destroy private fields and releases resources
}

func NewBaseService

func NewBaseService(logger log.Logger, name string, impl Implementation) *BaseService

NewBaseService creates a new BaseService.

func (*BaseService) IsRunning

func (bs *BaseService) IsRunning() bool

IsRunning implements Service by returning true or false depending on the service's state.

func (*BaseService) Start

func (bs *BaseService) Start(ctx context.Context) error

Start starts the Service and calls its OnStart method. An error will be returned if the service is stopped, but not if it is already running.

func (*BaseService) Stop

func (bs *BaseService) Stop()

Stop manually terminates the service by calling OnStop method from the implementation and releases all resources related to the service.

func (*BaseService) String

func (bs *BaseService) String() string

String provides a human-friendly representation of the service.

func (*BaseService) Wait

func (bs *BaseService) Wait()

Wait blocks until the service is stopped.

type Implementation

type Implementation interface {
	// Called by the Services Start Method
	OnStart(context.Context) error

	// Called when the service's context is canceled.
	OnStop()
}

Implementation describes the implementation that the BaseService implementation wraps.

type Service

type Service interface {
	// Start is called to start the service, which should run until
	// the context terminates. If the service is already running, Start
	// must report an error.
	Start(context.Context) error

	// Manually terminates the service
	Stop()

	// Return true if the service is running
	IsRunning() bool

	// Wait blocks until the service is stopped.
	Wait()
}

Service defines a service that can be started, stopped, and reset.

Jump to

Keyboard shortcuts

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