maintenance

package
v0.0.0-...-95891f2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 16 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 maintenance holds the site-wide "back in a few minutes" mode used while a restore replaces the database. One State is shared by the global gate middleware (gate.go), the restore plugin that drives it, and the health probe that reports it.

The state is mirrored to a small marker file so that a process restart in the middle of a restore comes back closed instead of serving a half-loaded database. The same file is what scripts/restore.sh reads to confirm that Chronicle itself, not a human, asked it to run against a live site.

Index

Constants

View Source
const MarkerName = ".chronicle-maintenance"

MarkerName is the marker file's basename inside the backup directory.

View Source
const RetryAfterSeconds = "60"

RetryAfterSeconds is what a refused client is told to wait.

Variables

View Source
var (
	ErrBusy      = errors.New("maintenance: a restore is already in progress")
	ErrNotActive = errors.New("maintenance: not in maintenance")
	ErrStillBusy = errors.New("maintenance: the restore is still running")
)

Errors returned by State transitions.

Functions

func MarkerExists

func MarkerExists(path string) bool

MarkerExists reports whether a restore marker is on disk. main reads it before the app exists, to tolerate a database a failed restore left behind.

func SessionCookieNames

func SessionCookieNames() []string

SessionCookieNames returns the cookie names the gate reads a session from.

func SessionHash

func SessionHash(r *http.Request) string

SessionHash returns the hex SHA-256 of the request's session token, or "" when it has none. The raw token is never stored.

Types

type OwnerHandler

type OwnerHandler func(c *echo.Context) (handled bool, err error)

OwnerHandler serves the restoring admin's own pages while the gate is closed. It returns handled=false for any path it does not own, which then gets the ordinary refusal. It runs without the router, the auth middleware or the database, because the database is the thing being replaced.

type Phase

type Phase string

Phase is where a restore is in its life.

const (
	// PhaseIdle means no restore has run since boot (or the marker was cleared).
	PhaseIdle Phase = "idle"
	// PhaseRunning means the site is closed and the restore is working.
	PhaseRunning Phase = "running"
	// PhaseDone means the restore finished and the site is open again.
	PhaseDone Phase = "done"
	// PhaseFailed means the restore stopped with an error; the site stays closed.
	PhaseFailed Phase = "failed"
	// PhaseInterrupted means the process restarted while a restore was running.
	PhaseInterrupted Phase = "interrupted"
)

type Snapshot

type Snapshot struct {
	// Active is true while the gate is closed.
	Active bool
	Phase  Phase
	// Step is a short plain-language line for what is happening now.
	Step     string
	Manifest string
	// Actor is the user id of the admin who started the restore.
	Actor      string
	StartedAt  time.Time
	FinishedAt time.Time
	// Safety is the basename of the manifest holding the copy of the
	// database taken just before it was replaced; empty if none was made.
	Safety string
	// DBTouched is true once the script began replacing the database.
	DBTouched bool
	ExitCode  int
	// Summary is the plain-language outcome.
	Summary string
	// Tail is the end of the script's output, kept for the failure page.
	Tail string
	// Left is true when an admin left maintenance without a finished restore.
	Left bool
}

Snapshot is a copy of the state, safe to hand to templates and JSON.

type State

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

State is the shared maintenance state. The zero value is not usable; use NewState.

func NewState

func NewState(markerPath string) *State

NewState builds a State and reads the marker. A marker left by a process that died mid-restore (state=running) comes back as interrupted; one left by a failed restore (state=failed) comes back as failed. Either way the gate starts closed. An unreadable or malformed marker is treated as closed too: the only way a marker exists is that a restore began, so serving traffic against an unknown database is the worse mistake.

func (*State) Active

func (s *State) Active() bool

Active reports whether the gate is closed.

func (*State) CheckLeaveToken

func (s *State) CheckLeaveToken(got string) bool

CheckLeaveToken compares in constant time.

func (*State) Enter

func (s *State) Enter(ownerHash, actor, manifest string) error

Enter closes the gate for a restore started by actor from the browser session whose hash is ownerHash. It writes the marker first and refuses to close the gate if that fails, because a restore that cannot be recorded could not be told apart from a normal boot after a crash.

func (*State) Fail

func (s *State) Fail(summary, tail string, exitCode int)

Fail ends the restore with an error and keeps the gate closed. The marker is rewritten as failed, so a restart stays closed too.

func (*State) Gate

func (s *State) Gate() echo.MiddlewareFunc

Gate is the global middleware. While the state is open it only counts requests in flight. While closed it refuses everything except:

  • health probes, which answer "maintenance" themselves;
  • static files, which touch no data and let the pages below look right;
  • the owning admin's session on the paths its OwnerHandler claims.

A nil State is a gate that is always open, so an App built without one (in tests) still serves.

func (*State) IsOwner

func (s *State) IsOwner(r *http.Request) bool

IsOwner reports whether the request carries the session that started the restore. The bypass is keyed to the hash of that session's token, never to a cookie value an outsider could pick, and only while the gate is closed.

func (*State) Leave

func (s *State) Leave() error

Leave opens the gate after a failed or interrupted restore, on the admin's say-so. It refuses while the restore is still running.

func (*State) LeaveToken

func (s *State) LeaveToken() string

LeaveToken is the one-time value the admin's page must send back to leave maintenance. It is shown only to the owning admin session.

func (*State) MarkerPath

func (s *State) MarkerPath() string

MarkerPath returns where the marker file lives.

func (*State) NoteDBTouched

func (s *State) NoteDBTouched()

NoteDBTouched records that the database is being replaced; from here a stop leaves it in an unknown state.

func (*State) NoteSafety

func (s *State) NoteSafety(name string)

NoteSafety records the safety copy's manifest name and persists it, so a restart mid-restore can still point at it.

func (*State) RunToken

func (s *State) RunToken() string

RunToken is the secret handed to the restore script so it can prove, with the marker, that Chronicle started it. Empty when not in maintenance.

func (*State) SetOwnerHandler

func (s *State) SetOwnerHandler(h OwnerHandler)

SetOwnerHandler installs the handler for the owning admin's session.

func (*State) SetStep

func (s *State) SetStep(step string)

SetStep updates the plain-language progress line.

func (*State) Snapshot

func (s *State) Snapshot() Snapshot

Snapshot returns a copy of the current state.

func (*State) Succeed

func (s *State) Succeed(summary string)

Succeed ends the restore, removes the marker and opens the gate.

func (*State) Track

func (s *State) Track() func()

Track marks a request as in flight; call the returned func when it ends.

func (*State) WaitIdle

func (s *State) WaitIdle(max time.Duration) bool

WaitIdle blocks until no tracked request is running or max elapses, and reports whether it went idle. The restore script starts only after this, so a slow request does not write into the database as it is being dropped.

Jump to

Keyboard shortcuts

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