lifecycle

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 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.

README

ota/lifecycle

Ordered start/stop hooks with rollback. It is the chain ota runs an update through, and it works for any program that starts and stops components in a fixed order.

import "github.com/ArmornetIO/ota/lifecycle"

var m lifecycle.Manager
m.Append(lifecycle.Hook{Name: "db", OnStart: openDB, OnStop: closeDB})
m.Append(lifecycle.Hook{Name: "http", OnStart: listen, OnStop: shutdown})

if err := m.Start(ctx); err != nil { // "db" is already stopped again if "http" failed
	return err
}
defer m.Stop(ctx) // stops "http", then "db"
  • Hooks start in order and stop in reverse.
  • If a hook fails to start, the hooks already started are stopped in reverse and the error is returned.
  • Stop runs every hook even if some fail, and returns the combined error.
  • Every transition is logged through log/slog.
  • Hooks, Index, Insert and Remove let a caller edit a chain built by a library, addressing steps by name. That is how adopters add a step before ota's swap or drop one they do not need.

Zero dependencies, Go 1.27+. Part of ota.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aware

type Aware interface {
	OnStart(ctx context.Context) error
	OnStop(ctx context.Context) error
}

Aware is an optional extension for tools or components that need startup/shutdown. The caller checks via type assertion and registers the hook automatically.

type Hook

type Hook struct {
	Name    string
	OnStart func(ctx context.Context) error
	OnStop  func(ctx context.Context) error
}

Hook is a startup/shutdown pair. Either field may be nil. Name is optional — when set it appears in all lifecycle log output for this hook.

type Lifecycle

type Lifecycle interface {
	Append(Hook)
}

Lifecycle collects ordered hooks. Agents and servers register hooks in their Create/New calls; the engine or runner drives Start/Stop.

type Manager

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

Manager is the concrete, engine-owned implementation of Lifecycle. Hooks start in append order and stop in reverse order. Every hook transition is logged automatically; callers do not need their own logging.

func (*Manager) Append

func (m *Manager) Append(h Hook)

Append adds h to the end of the chain: it starts last and stops first.

func (*Manager) Hooks

func (m *Manager) Hooks() []Hook

Hooks returns a copy of the chain in start order, so a caller can inspect a default chain before deciding what to change without mutating it by accident.

func (*Manager) Index

func (m *Manager) Index(name string) int

Index returns the position of the first hook with this name, or -1. Names are how adopters address steps; positions shift as other steps are added.

func (*Manager) Insert

func (m *Manager) Insert(i int, h Hook)

Insert places h at index i, clamped to the ends of the chain. Chains built by a library are edited by adopters (a step before the swap, a step after the commit), and Append alone can only ever add to the end.

func (*Manager) Remove

func (m *Manager) Remove(name string) bool

Remove drops the first hook with this name and reports whether one existed.

func (*Manager) Start

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

Start runs all OnStart hooks in registration order. On the first failure it rolls back already-started hooks in reverse and returns the error.

func (*Manager) Stop

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

Stop runs all OnStop hooks in reverse order. All hooks run regardless of errors; errors are combined and returned.

Jump to

Keyboard shortcuts

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