lifecycle

package
v0.2.4-test-3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2023 License: GPL-3.0 Imports: 2 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

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 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