lifecycle

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShutdownProviders

func ShutdownProviders(providers []foundation.ServiceProvider) []foundation.ShutdownProvider

ShutdownProviders 按注册逆序返回实现了 ShutdownProvider 的实例。

func SignalContext

func SignalContext(parent context.Context) (context.Context, context.CancelFunc)

SignalContext 监听 SIGINT / SIGTERM,收到信号后取消 context。

Types

type ConfigPublisher

type ConfigPublisher func(provider foundation.ServiceProvider) error

ConfigPublisher 负责写入 Provider 默认配置。

type Hooks

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

Hooks 管理 Booting / Booted / Terminating 回调。

func NewHooks

func NewHooks() *Hooks

func (*Hooks) FireBooted

func (h *Hooks) FireBooted(app foundation.Application)

func (*Hooks) FireBooting

func (h *Hooks) FireBooting(app foundation.Application)

func (*Hooks) FireTerminating

func (h *Hooks) FireTerminating(ctx context.Context)

func (*Hooks) OnBooted

func (h *Hooks) OnBooted(callback func(foundation.Application))

func (*Hooks) OnBooting

func (h *Hooks) OnBooting(callback func(foundation.Application))

func (*Hooks) OnTerminating

func (h *Hooks) OnTerminating(callback func(context.Context))

type Manager

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

Manager 编排 Register、Boot、Run、Shutdown 阶段。

func NewManager

func NewManager(hooks *Hooks) *Manager

func (*Manager) BootProviders

func (m *Manager) BootProviders(ctx context.Context, app foundation.Application, providers []foundation.ServiceProvider) error

BootProviders 触发 Booting 回调、Provider Boot、Booted 回调。

func (*Manager) HasBooted

func (m *Manager) HasBooted() bool

func (*Manager) Hooks

func (m *Manager) Hooks() *Hooks

func (*Manager) LastError

func (m *Manager) LastError() error

func (*Manager) MarkRunning

func (m *Manager) MarkRunning() error

func (*Manager) RegisterProviders

func (m *Manager) RegisterProviders(ctx context.Context, publish ConfigPublisher, providers ...foundation.ServiceProvider) error

RegisterProviders 对每个 Provider 仅执行一次 Register,并发布配置。

func (*Manager) Registry

func (m *Manager) Registry() *Registry

func (*Manager) Run

func (m *Manager) Run(ctx context.Context, runnable func(context.Context) error) error

Run 启动阻塞型 runnable,并在 context 取消后返回。

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context, providers []foundation.ServiceProvider) error

Shutdown 按 Provider 注册逆序关闭资源。

func (*Manager) State

func (m *Manager) State() State

type ProviderPhase

type ProviderPhase string

ProviderPhase 记录 Provider 已完成阶段。

const (
	ProviderPhaseNone       ProviderPhase = ""
	ProviderPhaseRegistered ProviderPhase = "registered"
	ProviderPhaseBooted     ProviderPhase = "booted"
)

type Registry

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

Registry 跟踪已注册 Provider,避免重复 Register。

func NewRegistry

func NewRegistry() *Registry

func (*Registry) BootedShutdownProviders

func (r *Registry) BootedShutdownProviders() []foundation.ShutdownProvider

BootedProviders 按注册逆序返回已 Boot 的 ShutdownProvider。

func (*Registry) MarkBooted

func (r *Registry) MarkBooted(provider foundation.ServiceProvider)

func (*Registry) Phase

func (r *Registry) Phase(provider foundation.ServiceProvider) ProviderPhase

func (*Registry) Providers

func (r *Registry) Providers() []foundation.ServiceProvider

Providers 返回按注册顺序排列的 Provider 列表。

func (*Registry) RegisterOnce

func (r *Registry) RegisterOnce(provider foundation.ServiceProvider, register func(foundation.ServiceProvider) error) error

RegisterOnce 仅对尚未注册的 Provider 执行 register 回调,并记录顺序。

type State

type State string

State 生命周期状态。

const (
	StateCreated     State = "created"
	StateRegistering State = "registering"
	StateRegistered  State = "registered"
	StateBooting     State = "booting"
	StateBooted      State = "booted"
	StateRunning     State = "running"
	StateStopping    State = "stopping"
	StateStopped     State = "stopped"
	StateFailed      State = "failed"
)

type StateMachine

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

StateMachine 并发安全的生命周期状态机。

func NewStateMachine

func NewStateMachine() *StateMachine

func (*StateMachine) Is

func (s *StateMachine) Is(states ...State) bool

func (*StateMachine) MustTransition

func (s *StateMachine) MustTransition(next State)

func (*StateMachine) State

func (s *StateMachine) State() State

func (*StateMachine) Transition

func (s *StateMachine) Transition(next State) error

Source Files

  • hooks.go
  • manager.go
  • provider.go
  • signal.go
  • state.go

Jump to

Keyboard shortcuts

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