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
- Variables
- func MarkerExists(path string) bool
- func SessionCookieNames() []string
- func SessionHash(r *http.Request) string
- type OwnerHandler
- type Phase
- type Snapshot
- type State
- func (s *State) Active() bool
- func (s *State) CheckLeaveToken(got string) bool
- func (s *State) Enter(ownerHash, actor, manifest string) error
- func (s *State) Fail(summary, tail string, exitCode int)
- func (s *State) Gate() echo.MiddlewareFunc
- func (s *State) IsOwner(r *http.Request) bool
- func (s *State) Leave() error
- func (s *State) LeaveToken() string
- func (s *State) MarkerPath() string
- func (s *State) NoteDBTouched()
- func (s *State) NoteSafety(name string)
- func (s *State) RunToken() string
- func (s *State) SetOwnerHandler(h OwnerHandler)
- func (s *State) SetStep(step string)
- func (s *State) Snapshot() Snapshot
- func (s *State) Succeed(summary string)
- func (s *State) Track() func()
- func (s *State) WaitIdle(max time.Duration) bool
Constants ¶
const MarkerName = ".chronicle-maintenance"
MarkerName is the marker file's basename inside the backup directory.
const RetryAfterSeconds = "60"
RetryAfterSeconds is what a refused client is told to wait.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) CheckLeaveToken ¶
CheckLeaveToken compares in constant time.
func (*State) Enter ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
NoteSafety records the safety copy's manifest name and persists it, so a restart mid-restore can still point at it.
func (*State) RunToken ¶
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.