Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plugins/team-setup/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,11 @@ function ServerUpdateBanner() {
<div className="flex min-w-0 items-center gap-2">
<Icon name={restarting ? "LoaderCircle" : "CircleArrowUp"} className={`size-4 shrink-0 ${restarting ? "animate-spin" : "text-primary"}`} />
{restarting ? (
<span>Restarting your server. This page reloads when it is back, in about a minute.</span>
<span>Restarting your server. This page reloads in a moment and comes back when it is up.</span>
) : (
<span>
A new build of your bb server is ready. Restarting takes about a minute and interrupts any running turn; if you never
do, it restarts tonight.
A new build of your bb server is ready. Restarting takes under a minute and interrupts any running turn; if you
never do, it restarts tonight.
</span>
)}
</div>
Expand Down
42 changes: 37 additions & 5 deletions plugins/team-setup/server-update.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { createUpdateStore, POLL_MS, RESTART_POLL_MS, UPDATE_PATH, type UpdateStoreDeps } from "./server-update.js";
import { createUpdateStore, POLL_MS, RESTART_POLL_MS, RESTART_TIMEOUT_MS, UPDATE_PATH, type UpdateStoreDeps } from "./server-update.js";

// A router and a clock the test drives. Timers fire only when `advance` is
// called, so each step is explicit.
Expand Down Expand Up @@ -98,14 +98,13 @@ describe("update store", () => {
expect(store.getState()).toEqual({ status: "pending" });
});

it("applies, follows the restart down and up, then reloads", async () => {
it("applies, then reloads as soon as the server has gone down", async () => {
const h = harness([
{ status: 200, body: { pending: true, ready: true } },
{ status: 200, body: { applied: true } },
{ status: 200, body: { pending: false, ready: true } }, // old pod still up
{ status: 503 }, // router briefly away
{ status: 200, body: { pending: false, ready: false } },
{ status: 200, body: { pending: false, ready: true } },
]);
const store = createUpdateStore(h.deps);
store.subscribe(() => {});
Expand All @@ -115,10 +114,43 @@ describe("update store", () => {
await h.flush();
expect(store.getState()).toEqual({ status: "restarting" });
expect(h.calls[1]).toEqual({ method: "POST" });
for (let i = 0; i < 4; i++) await h.advance(RESTART_POLL_MS);
for (let i = 0; i < 2; i++) await h.advance(RESTART_POLL_MS);
expect(h.reloaded()).toBe(0);
await h.advance(RESTART_POLL_MS);
await applied;
expect(h.reloaded()).toBe(1);
expect(h.calls).toHaveLength(5);
});

it("reloads anyway when the server never reports going down", async () => {
const answers: Array<{ status: number; body?: unknown }> = [
{ status: 200, body: { pending: true, ready: true } },
{ status: 200, body: { applied: true } },
];
for (let i = 0; i < 100; i++) answers.push({ status: 200, body: { pending: false, ready: true } });
const h = harness(answers);
const store = createUpdateStore(h.deps);
store.subscribe(() => {});
await h.flush();
const applied = store.apply();
await h.flush();
for (let i = 0; i * RESTART_POLL_MS < RESTART_TIMEOUT_MS; i++) await h.advance(RESTART_POLL_MS);
await applied;
expect(h.reloaded()).toBe(1);
expect(h.calls).toHaveLength(6);
});

it("drops the banner without reloading when there was nothing to apply", async () => {
const h = harness([
{ status: 200, body: { pending: true, ready: true } },
{ status: 200, body: { applied: false } },
]);
const store = createUpdateStore(h.deps);
store.subscribe(() => {});
await h.flush();
await store.apply();
expect(store.getState()).toEqual({ status: "current" });
expect(h.reloaded()).toBe(0);
expect(h.timers).toHaveLength(1); // only the regular poll
});

it("reports a refused apply instead of waiting forever", async () => {
Expand Down
41 changes: 27 additions & 14 deletions plugins/team-setup/server-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

export const UPDATE_PATH = "/_bb-gate/update";
export const POLL_MS = 60_000;
export const RESTART_POLL_MS = 3_000;
// A restart that has not come back by then is not one the banner can
// explain; reload and let the gate's starting page take over.
export const RESTART_TIMEOUT_MS = 4 * 60_000;
export const RESTART_POLL_MS = 500;
// A restart the Deployment has not reported within this long is not one the
// banner can explain; reload anyway and let the gate sort it out.
export const RESTART_TIMEOUT_MS = 15_000;

export type UpdateState =
| { status: "unknown" }
// Not behind bb-gate, or the router is older than this plugin.
| { status: "unavailable" }
| { status: "current" }
| { status: "pending" }
// The developer pressed the button; the server is going down and coming
// back, and the page reloads when it has.
// The developer pressed the button; the page reloads as soon as the
// server has gone down, onto the gate's starting page.
| { status: "restarting" }
| { status: "error"; message: string };

Expand All @@ -48,7 +48,7 @@ export interface UpdateStore {
subscribe(listener: () => void): () => void;
/** Ask the router now, outside the poll. */
refresh(): Promise<void>;
/** POST the update and follow the restart until the server is back. */
/** POST the update and reload once the server has gone down. */
apply(): Promise<void>;
}

Expand Down Expand Up @@ -106,19 +106,24 @@ export function createUpdateStore(deps: UpdateStoreDeps): UpdateStore {
schedule();
};

// Follow the restart: the Deployment reports not-ready once the old pod
// is gone, then ready when the new one answers. Reload on ready after
// not-ready, or when the clock runs out.
// Follow the restart only as far as its first step: the Deployment
// reports not-ready as soon as the old pod is gone, and from then on the
// router answers a page load with its starting page, which reloads itself
// until the new pod is up. Handing over to that page right away is the
// point: a bb whose server has just left is a dead UI, every request and
// socket failing, and there is nothing it could show that is better than
// the gate's own page.
//
// Waiting for not-ready, rather than reloading at once, keeps the reload
// from landing on the old pod in its last moments and loading the full
// app only to lose it again.
const follow = async () => {
const started = deps.now();
let wentDown = false;
while (deps.now() - started < RESTART_TIMEOUT_MS) {
await new Promise<void>((resolve) => deps.setTimeout(resolve, RESTART_POLL_MS));
try {
const s = await get();
if (s === "unavailable") break;
if (!s.ready) wentDown = true;
else if (wentDown) break;
if (s === "unavailable" || !s.ready) break;
} catch {
// The router itself is briefly away, or the gateway is; keep waiting.
}
Expand All @@ -132,6 +137,14 @@ export function createUpdateStore(deps: UpdateStoreDeps): UpdateStore {
try {
const r = await deps.fetch(UPDATE_PATH, { method: "POST", headers: { Accept: "application/json" } });
if (!r.ok) throw new Error(`bb-gate answered ${r.status}`);
const { applied } = (await r.json().catch(() => ({}))) as { applied?: unknown };
if (applied === false) {
// Nothing to apply after all: the server was already current, or
// the quiet window got there first. Nothing restarts, so do not
// wait for it to.
set({ status: "current" });
return;
}
} catch (e) {
set({ status: "error", message: e instanceof Error ? e.message : String(e) });
return;
Expand Down
Loading