lifecycle

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

Lifecycle

Lifecycle interface manages the lifecycle of a service and tracks its state changes. It also listens for signals from the process to ensure a graceful shutdown.

Concept

Service Interface

Service is an interface for Lifecycle to manage. The component that plans to use Lifecycle needs to implement this interface.

// Service provides abstract methods to control the lifecycle of a service
// Every service must implement Service interface.
type Service interface {
	// Name defines the unique identifier of a service, which cannot be repeated globally.
	Name() string
	// Start a service, for resource application, start background coroutine and 
	// other startup operations.
	//
	// Start method should be used in non-block way, for example, a blocked 
	// listening socket should open a goroutine separately internally.
	Start(ctx context.Context) error
	// Stop a service, close the goroutines inside the service, recycle resources, 
	// and ensure the graceful shutdown of the service.
	Stop(ctx context.Context) error
}
Example
    ctx := context.Background()
    svcLifecycle.RegisterServices(service...)
	// blocks the svcLifecycle for waiting signals to shut down the process
    svcLifecycle.Signals(syscall.SIGINT, syscall.SIGTERM ...).Init(ctx).StartServices(ctx).Wait(ctx)

Documentation

Overview

Package lifecycle is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lifecycle

type Lifecycle interface {
	// RegisterServices registers service to ServiceLifecycle for managing.
	RegisterServices(modular ...Service)
	// StartServices starts all registered services by calling Service.Start method.
	StartServices(ctx context.Context) Lifecycle
	// StopServices stops all registered services by calling Service.Stop method.
	StopServices(ctx context.Context)
	// Signals listens the system signals for gracefully stop the registered services.
	Signals(sigs ...os.Signal) Lifecycle
	// Wait waits the signal for stopping the ServiceLifecycle, before stopping
	// the ServiceLifecycle will call StopServices stops all registered services.
	Wait(ctx context.Context)
}

Lifecycle is an interface to describe how service is managed. The Lifecycle tracks the Service lifecycle, listens for signals from the process to ensure a graceful shutdown.

All managed services must firstly call RegisterServices to register with Lifecycle.

type MockLifecycle added in v0.2.4

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

MockLifecycle is a mock of Lifecycle interface.

func NewMockLifecycle added in v0.2.4

func NewMockLifecycle(ctrl *gomock.Controller) *MockLifecycle

NewMockLifecycle creates a new mock instance.

func (*MockLifecycle) EXPECT added in v0.2.4

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockLifecycle) RegisterServices added in v0.2.4

func (m *MockLifecycle) RegisterServices(modular ...Service)

RegisterServices mocks base method.

func (*MockLifecycle) Signals added in v0.2.4

func (m *MockLifecycle) Signals(sigs ...os.Signal) Lifecycle

Signals mocks base method.

func (*MockLifecycle) StartServices added in v0.2.4

func (m *MockLifecycle) StartServices(ctx context.Context) Lifecycle

StartServices mocks base method.

func (*MockLifecycle) StopServices added in v0.2.4

func (m *MockLifecycle) StopServices(ctx context.Context)

StopServices mocks base method.

func (*MockLifecycle) Wait added in v0.2.4

func (m *MockLifecycle) Wait(ctx context.Context)

Wait mocks base method.

type MockLifecycleMockRecorder added in v0.2.4

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

MockLifecycleMockRecorder is the mock recorder for MockLifecycle.

func (*MockLifecycleMockRecorder) RegisterServices added in v0.2.4

func (mr *MockLifecycleMockRecorder) RegisterServices(modular ...interface{}) *gomock.Call

RegisterServices indicates an expected call of RegisterServices.

func (*MockLifecycleMockRecorder) Signals added in v0.2.4

func (mr *MockLifecycleMockRecorder) Signals(sigs ...interface{}) *gomock.Call

Signals indicates an expected call of Signals.

func (*MockLifecycleMockRecorder) StartServices added in v0.2.4

func (mr *MockLifecycleMockRecorder) StartServices(ctx interface{}) *gomock.Call

StartServices indicates an expected call of StartServices.

func (*MockLifecycleMockRecorder) StopServices added in v0.2.4

func (mr *MockLifecycleMockRecorder) StopServices(ctx interface{}) *gomock.Call

StopServices indicates an expected call of StopServices.

func (*MockLifecycleMockRecorder) Wait added in v0.2.4

func (mr *MockLifecycleMockRecorder) Wait(ctx interface{}) *gomock.Call

Wait indicates an expected call of Wait.

type MockService added in v0.2.4

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

MockService is a mock of Service interface.

func NewMockService added in v0.2.4

func NewMockService(ctrl *gomock.Controller) *MockService

NewMockService creates a new mock instance.

func (*MockService) EXPECT added in v0.2.4

func (m *MockService) EXPECT() *MockServiceMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockService) Name added in v0.2.4

func (m *MockService) Name() string

Name mocks base method.

func (*MockService) Start added in v0.2.4

func (m *MockService) Start(ctx context.Context) error

Start mocks base method.

func (*MockService) Stop added in v0.2.4

func (m *MockService) Stop(ctx context.Context) error

Stop mocks base method.

type MockServiceMockRecorder added in v0.2.4

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

MockServiceMockRecorder is the mock recorder for MockService.

func (*MockServiceMockRecorder) Name added in v0.2.4

func (mr *MockServiceMockRecorder) Name() *gomock.Call

Name indicates an expected call of Name.

func (*MockServiceMockRecorder) Start added in v0.2.4

func (mr *MockServiceMockRecorder) Start(ctx interface{}) *gomock.Call

Start indicates an expected call of Start.

func (*MockServiceMockRecorder) Stop added in v0.2.4

func (mr *MockServiceMockRecorder) Stop(ctx interface{}) *gomock.Call

Stop indicates an expected call of Stop.

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 Every service must implement Service interface.

Jump to

Keyboard shortcuts

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