// This file holds the one way adoption restarts an app: the canonical // host alone, through the narrowed restart door, after a dry run shows // that the door resolves exactly that host. package adopt import ( "context" "errors" "fmt" "net/http" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) // RestartAndWait's polling. Variables only so tests can shorten them. var ( pollEvery = 5 * time.Second pollLimit = 5 * time.Minute ) // RestartCanonical restarts app's canonical host, and nothing else. Every // restart adoption makes, a rollback's included, comes through here; it // is the only caller of carlosop.RestartHost. // // id must be app's canonical record: its app, and the canonical domain // claimed for its record host. The restart door is first asked for a // dry run, which must resolve exactly [id.RecordHost]; anything else is // "restart-scope-", n the number of routes it resolved, and nothing is // restarted. A route the dry run reports as hibernating is accepted: it // will not cycle now, but the next request wakes it on the new config, // and the probe that follows makes that request. // // id may be a journalled baseline, so after the dry run, the last read // before the restart, the claim is resolved again (claimHolds). A claim // that no longer binds the canonical domain to id's record restarts // nothing, with or without apply. // // A console without the narrowed door (carlosop.ErrNoScopedRestart), a // 503 or any other refusal is returned as it is, and nothing falls back // to the app-wide door. Without apply, only the dry run is made. func RestartCanonical(ctx context.Context, c *carlosop.Client, app provision.App, id Identity, apply bool) error { host := CanonicalHost(app) switch { case c == nil: return errors.New("adopt: no console client") case id.App != app.Slug: return errors.New("adopt: the identity is another app's") case id.RecordHost == "" || id.Domain != host || id.Route != id.RecordHost: return errors.New("adopt: the identity is not the canonical host's") } routes, err := c.RestartHost(ctx, app.Slug, id.RecordHost, true) if err != nil { return err } if err := exactlyHost(routes, id.RecordHost); err != nil { return err } if err := claimHolds(ctx, c, app, id); err != nil { return err } if !apply { return nil } routes, err = c.RestartHost(ctx, app.Slug, id.RecordHost, false) if err != nil { return err } // The request is made by now; a wider answer is still reported, so // the run stops rather than carrying on as if one host cycled. return exactlyHost(routes, id.RecordHost) } // claimHolds is nil when app's canonical domain still resolves to id's // record through id's route. Otherwise it is restart-binding-, the // code canonicalRecord refused with, or restart-binding-changed for a // claim that now resolves elsewhere; a read that fails is its own error. // Whether the claim is live is not asked: a rollback has to reach the // same record while its domain is unwell. func claimHolds(ctx context.Context, c *carlosop.Client, app provision.App, id Identity) error { ds, err := c.Domains(ctx, app.Slug) if err != nil { return err } all, err := c.Instances(ctx, app.Slug) if err != nil { return err } d, _, code, err := canonicalRecord(CanonicalHost(app), ds, all) switch { case err != nil: return err case code != "": return errors.New("adopt: restart-binding-" + code) case d.Route != id.Route: return errors.New("adopt: restart-binding-changed") } return nil } // exactlyHost is nil when routes is the one route host. func exactlyHost(routes []carlosop.RestartRoute, host string) error { if len(routes) != 1 || routes[0].Host != host { return fmt.Errorf("restart-scope-%d", len(routes)) } return nil } // RestartAndWait restarts app's canonical host through RestartCanonical, // then probes its public origin every 5 s, for at most 5 min, until // until accepts what the probe saw, and returns that observation. A probe // that fails (the app is between processes) is polled through. Running // out of time is "restart-timeout", with the last reason. // // Without apply, it makes RestartCanonical's dry run and one probe, and // returns what that probe saw: nothing restarted, so nothing is waited // for and until is not asked. func RestartAndWait(ctx context.Context, c *carlosop.Client, h *http.Client, app provision.App, id Identity, until func(Delivery) error, apply bool) (Delivery, error) { if until == nil { return Delivery{}, errors.New("adopt: nothing to wait for") } if err := RestartCanonical(ctx, c, app, id, apply); err != nil { return Delivery{}, err } origin := provision.PublicOrigin(provision.AdoptionLabel, app.Slug) if !apply { return Probe(ctx, h, origin) } // Every wait and every probe runs under the limit's own context, so a // stalled answer is cut off at the limit and nothing that finishes // after it counts. Its expiry is told apart from the caller's. poll, cancel := context.WithTimeout(ctx, pollLimit) defer cancel() var last Delivery reason := errors.New("adopt: the app was not probed") stopped := func() (Delivery, error) { if err := ctx.Err(); err != nil { return last, fmt.Errorf("adopt: stopped waiting for the restart: %w", err) } return last, fmt.Errorf("adopt: restart-timeout: %w", reason) } for { // The restart is asynchronous, so the first probe waits too. t := time.NewTimer(pollEvery) select { case <-poll.Done(): t.Stop() return stopped() case <-t.C: } // A probe that returns at all finished inside the limit: each of // its requests ran under poll. One cut off by the limit is not a // reason of its own, so the last real one is reported. d, err := Probe(poll, h, origin) if err != nil { if poll.Err() != nil { return stopped() } reason = err continue } last = d reason = until(d) // Accepted only if the limit still holds once until has answered: // a success reached after the deadline is a timeout. if poll.Err() != nil { if reason == nil { reason = errors.New("adopt: the app came back only after the limit") } return stopped() } if reason == nil { return d, nil } } }