runtime

package
v0.0.0-...-0a40313 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package runtime provides the core application lifecycle management for the Swarm framework. It handles module registration, building, startup, graceful shutdown, and signal handling.

Package runtime defines core module interfaces used by the Swarm framework. A module is a reusable, composable component that can be built and optionally started/stopped during the application lifecycle.

Package runtime contains the ModuleManager responsible for constructing, tracking, starting, and stopping modules. It supports:

  • registering module factories
  • building modules lazily
  • starting lifecycle-aware modules
  • stopping modules in reverse order
  • thread-safe module access

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lifecycle

type Lifecycle interface {
	Module
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Lifecycle extends Module with Startup and Shutdown logic. Modules implementing this interface will participate in the app lifecycle.

type Module

type Module interface {
	Name() string
}

Module is the base interface for all modules. Every module must have a stable, unique name.

type ModuleFactoryFunc

type ModuleFactoryFunc func() (Module, error)

ModuleFactoryFunc describes a factory function used to construct modules. This is used by SwarmApp and ModuleManager to lazily create modules.

type ModuleManager

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

ModuleManager manages module factories and constructed module instances. It ensures consistent and thread-safe lifecycle handling.

func NewModuleManager

func NewModuleManager() *ModuleManager

NewModuleManager creates an empty module manager.

func (*ModuleManager) Build

func (mm *ModuleManager) Build() error

Build constructs all missing modules from registered factories. It does NOT start them.

func (*ModuleManager) Get

func (mm *ModuleManager) Get(name string) (Module, bool)

Get returns an already-built module instance by name. Returns false if the module does not exist.

func (*ModuleManager) RegisterFactory

func (mm *ModuleManager) RegisterFactory(name string, factory ModuleFactoryFunc)

RegisterFactory registers a new module factory under a given name. This does NOT construct the module immediately. Panics on duplicate names or invalid input.

func (*ModuleManager) Start

func (mm *ModuleManager) Start(ctx context.Context) error

Start runs startup for all lifecycle-enabled modules. Modules are copied into a snapshot to avoid holding locks during initialization.

func (*ModuleManager) Stop

func (mm *ModuleManager) Stop(ctx context.Context) error

Stop gracefully stops all lifecycle-enabled modules in reverse order.

type SwarmApp

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

SwarmApp represents the main application instance. It coordinates module registration, initialization, lifecycle management, and graceful shutdown.

func NewSwarmApp

func NewSwarmApp() *SwarmApp

NewSwarmApp creates a new application with an empty module manager.

func (*SwarmApp) Build

func (a *SwarmApp) Build() error

Build constructs all modules registered via factories. It does not start them.

func (*SwarmApp) GetModule

func (a *SwarmApp) GetModule(name string) (Module, bool)

GetModule returns a module instance by name. Second return value indicates whether the module exists.

func (*SwarmApp) RegisterModule

func (a *SwarmApp) RegisterModule(name string, factory ModuleFactoryFunc)

RegisterModule registers a module factory under a given name. Modules will be constructed during Build() or Start().

func (*SwarmApp) Run

func (a *SwarmApp) Run() error

Run starts the app with a background context. Blocks until stopped.

func (*SwarmApp) Start

func (a *SwarmApp) Start(ctx context.Context) error

Start builds all modules (if not built yet), starts them, and blocks until the application is stopped either by:

  • context cancellation
  • termination signal (SIGINT, SIGTERM)

func (*SwarmApp) Stop

func (a *SwarmApp) Stop(ctx context.Context) error

Stop gracefully stops all lifecycle-enabled modules. A timeout protects against modules hanging during shutdown.

Jump to

Keyboard shortcuts

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