dbsleep

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 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

Overview

Package dbsleep puts a managed database to sleep and wakes it.

Index

Constants

View Source
const (
	CodeStopped        = "database_stopped"
	CodeLinkedApps     = "linked_apps_running"
	CodeRestoreRunning = "restore_running"
	CodeImportRunning  = "import_or_restore_running"
	CodeUpgradeRunning = "upgrade_running"
	CodeBackupRunning  = "backup_running"
	CodeProtected      = "protected_environment"
	CodeUnverifiedApps = "linked_apps_unverified"
	CodePoolerEnabled  = "pooler_enabled"
	CodeDependentBusy  = "dependent_database_busy"
)

Blocker codes, stable for API and CLI callers.

Variables

View Source
var ErrBusy = errors.New("an import or restore is running for this database, retry when it finishes")

ErrBusy means an import or restore holds the database, so it cannot be woken yet.

Functions

func Lock

func Lock(name string) func()

Lock serializes sleep, wake, manual stop and manual start per database, across Service values. The returned func releases it.

Types

type BlockedError

type BlockedError struct{ Blockers []Blocker }

BlockedError reports why a database cannot sleep.

func (*BlockedError) Error

func (e *BlockedError) Error() string

type Blocker

type Blocker struct {
	Code   string `json:"code"`
	Detail string `json:"detail"`
}

Blocker is one reason a database cannot sleep now.

func Evaluate

func Evaluate(f Facts) []Blocker

Evaluate returns the blockers for f, empty when the database may sleep.

type Facts

type Facts struct {
	// Stopped means stopped by hand, not asleep.
	Stopped    bool
	LinkedApps []string
	// UnverifiedApps are running apps with env values that cannot be shown not to name the database.
	UnverifiedApps []string
	// HiddenLinked and HiddenUnverified count apps of those two kinds the caller may not see.
	HiddenLinked     int
	HiddenUnverified int
	PoolerEnabled    bool
	// DependentsBusy counts databases still being built from this one.
	DependentsBusy int
	Restoring      bool
	Importing      bool
	Upgrading      bool
	BackingUp      bool
	// ProtectedEnv is the name of the protected environment the database is in.
	ProtectedEnv string
}

Facts is everything eligibility is decided from.

type Nudger

type Nudger interface{ Nudge() }

Nudger asks the reconciler for an early pass.

type Request

type Request struct {
	Name   string
	Reason string
	Actor  string
}

Request says who asked and why; Reason is a store sleep reason.

type Runtime

type Runtime struct {
	// Hosts are the container names that reach a database or its pooler.
	Hosts func(database string) []string
	// TryHold marks a database busy so no import or restore can start.
	TryHold func(database string) (release func(), free bool)
	// Held reports whether an import or restore holds the database.
	Held func(database string) bool
	// ProviderRef reports whether a value points at an external secret provider.
	ProviderRef func(value string) bool
}

Runtime is supplied by the caller so this package stays cheap to depend on. All fields are required.

type SecretReader

type SecretReader interface {
	Exists(ctx context.Context, serviceName, envKey string) (bool, error)
	Resolve(ctx context.Context, serviceName, envKey string) (string, error)
}

SecretReader reads stored secret values so they can be checked; they are never logged or returned.

type Service

type Service struct {
	Store   Store
	Runtime Runtime
	Nudger  Nudger
	Logger  *slog.Logger
	Now     func() time.Time
	// Secrets lets eligibility check secret env values; without it such apps block a sleep.
	Secrets  SecretReader
	MeshZone string
	// CanSeeApp limits which app names blockers may reveal; nil shows all.
	CanSeeApp func(app string) bool
	// Assume treats these apps as already stopped; only planning may set it.
	Assume map[string]bool
}

Service sleeps and wakes managed databases.

func (*Service) Dependents

func (s *Service) Dependents(ctx context.Context, name string) ([]string, error)

Dependents names every app, running or not, known to reach the database. The names are unfiltered, so callers apply their own visibility before showing them.

func (*Service) Sleep

func (s *Service) Sleep(ctx context.Context, req Request) (bool, error)

Sleep stops the database and marks it asleep. It reports false when it already slept.

func (*Service) Status

func (s *Service) Status(ctx context.Context, name string) (State, error)

Status reports name's sleep state and what blocks a sleep.

func (*Service) Wake

func (s *Service) Wake(ctx context.Context, req Request) (bool, error)

Wake starts a sleeping database. It reports false when it was not asleep, so a database stopped by hand stays stopped.

type State

type State struct {
	Name           string
	Sleeping       bool
	Since          time.Time
	Stopped        bool
	LastWakeReason string
	// Blockers is empty for a sleeping database.
	Blockers []Blocker
}

State is a database's sleep state as callers should show it.

type Store

type Store interface {
	ListProjectEnvVars(ctx context.Context, projectID string) (map[string]string, error)
	ListProjectSecretEnvKeys(ctx context.Context, projectID string) ([]string, error)
	ListOrganizationEnvVarsForProject(ctx context.Context, projectID string) (map[string]string, error)
	GetProjectOrganizationID(ctx context.Context, projectID string) (string, error)
	ListOrganizationSecretEnvKeys(ctx context.Context, orgID string) ([]string, error)
	ListEnvironmentEnvVars(ctx context.Context, environmentID string) (map[string]string, error)
	ListEnvironmentSecretEnvKeys(ctx context.Context, environmentID string) ([]string, error)
	ListDatabaseBranchesByParent(ctx context.Context, parent string) ([]store.DatabaseBranch, error)
	GetDesiredDatabase(ctx context.Context, name string) (*store.DesiredDatabase, error)
	SleepDatabase(ctx context.Context, name string, auto bool, since time.Time) (bool, error)
	ListDesiredServices(ctx context.Context) ([]store.DesiredService, error)
	EnvironmentOfDatabase(ctx context.Context, name string) (*store.EnvironmentRef, error)
	GetDatabaseSleep(ctx context.Context, name string) (store.DatabaseSleep, error)
	WakeDatabase(ctx context.Context, name string) (bool, error)
	SetDatabaseWakeReason(ctx context.Context, name, reason string) error
	AddSleepEvent(ctx context.Context, e store.SleepEvent) (int64, error)
	ListRestoreHistory(ctx context.Context, name string) ([]store.RestoreHistory, error)
	ListPITRRestoreHistory(ctx context.Context, name string) ([]store.PITRRestoreHistory, error)
	ListBaseBackupHistory(ctx context.Context, name string) ([]store.BaseBackupHistory, error)
	ListRunningBackups(ctx context.Context, startedBefore time.Time) ([]store.BackupHistory, error)
	ListDBUpgradeRuns(ctx context.Context, name string, limit int) ([]store.DBUpgradeRun, error)
	ListMajorUpgrades(ctx context.Context, name string) ([]store.MajorUpgrade, error)
}

Store is what sleeping and waking a database needs.

Jump to

Keyboard shortcuts

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