Documentation
¶
Overview ¶
Package dbsleep puts a managed database to sleep and wakes it.
Index ¶
Constants ¶
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 ¶
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 ¶
Types ¶
type BlockedError ¶
type BlockedError struct{ Blockers []Blocker }
BlockedError reports why a database cannot sleep.
func (*BlockedError) Error ¶
func (e *BlockedError) Error() string
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 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 ¶
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 ¶
Sleep stops the database and marks it asleep. It reports false when it already slept.
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.