Public: anyone can read this; the crew can change it.

A database the app cannot open or upgrade no longer kills the server: it still listens, and /healthz answers 503 naming the step that failed #225

Closed opened this
Closed without merging

mason/deal-clock:mason/222-a-database-the-app-cannot-open-or-upgrad → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +96 −5
src/db.ts +26 −3
@@ -580,12 +580,35 @@ export function openDb(dir = dataDir): Database {
580580 db.exec("PRAGMA journal_mode = WAL");
581581 db.exec("PRAGMA foreign_keys = ON");
582582 db.exec("PRAGMA busy_timeout = 5000");
583 migrate(db);
584 seed(db);
585 refresh(db);
583 try {
584 step("migration", () => migrate(db));
585 step("seed", () => seed(db));
586 step("sample refresh", () => refresh(db));
587 } catch (error) {
588 db.close();
589 throw error;
590 }
586591 return db;
587592 }
588593
594 /** A boot step that failed, by name, with the database's own message. */
595 export class OpenError extends Error {
596 constructor(
597 readonly step: string,
598 readonly detail: string,
599 ) {
600 super(`${step}: ${detail}`);
601 }
602 }
603
604 function step(name: string, run: () => void): void {
605 try {
606 run();
607 } catch (error) {
608 throw new OpenError(name, error instanceof Error ? error.message : String(error));
609 }
610 }
611
589612 /** Apply the migrations past the recorded version, all or none. Returns the version. */
590613 export function migrate(db: Database): number {
591614 db.exec(
src/health.test.ts +32
@@ -93,3 +93,35 @@ test("a boot on a database missing a column the first paint reads logs the check
9393 test("a healthy boot writes nothing to stderr", () => {
9494 expect(bootLog()).toEqual([]);
9595 });
96
97 test("a database whose migration throws still boots: /healthz 503 names the step, / 503 with the headers, file untouched", async () => {
98 const dir = mkdtempSync(join(tmpdir(), "app-unopenable-"));
99 const raw = new Database(join(dir, "app.db"));
100 raw.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))");
101 raw.exec("INSERT INTO schema_version (version) VALUES (1)");
102 raw.close();
103 const log = spyOn(console, "error").mockImplementation(() => {});
104 const server = serve({ port: 0, dataDir: dir, identityKey: "k".repeat(32) });
105 try {
106 const base = `http://127.0.0.1:${server.port}`;
107 const health = await fetch(`${base}/healthz`);
108 expect(health.status).toBe(503);
109 expect(health.headers.get("content-type")).toContain("text/plain");
110 expect(health.headers.get("x-content-type-options")).toBe("nosniff");
111 const body = await health.text();
112 expect(body).toContain("migration");
113 expect(body).not.toContain("at ");
114 const home = await fetch(`${base}/`);
115 expect(home.status).toBe(503);
116 expect(home.headers.get("content-security-policy")).toBeTruthy();
117 expect(home.headers.get("x-content-type-options")).toBe("nosniff");
118 expect(home.headers.get("referrer-policy")).toBeTruthy();
119 expect(log.mock.calls.some((c) => String(c[0]).includes('step "migration"'))).toBe(true);
120 const after = new Database(join(dir, "app.db"), { readonly: true });
121 expect(after.query("SELECT count(*) AS n FROM schema_version").get()).toEqual({ n: 1 });
122 after.close();
123 } finally {
124 server.stop(true);
125 log.mockRestore();
126 }
127 });
src/server.ts +38 −2
@@ -7,7 +7,7 @@
77 * serves what the forge said; a test calls `serve` with its own.
88 */
99
10 import { openDb } from "./db.ts";
10 import { OpenError, openDb } from "./db.ts";
1111 import { dataDir, identityKey, title, where } from "./env.ts";
1212 import { reportHealthAtBoot } from "./health.ts";
1313 import { html, sourceBytes, text } from "./http.ts";
@@ -24,7 +24,12 @@ export interface ServeOptions {
2424 }
2525
2626 export function serve(options: ServeOptions) {
27 const db = openDb(options.dataDir);
27 let db;
28 try {
29 db = openDb(options.dataDir);
30 } catch (error) {
31 return unopened(options, error);
32 }
2833 reportHealthAtBoot(db, title);
2934 // The terminal writes from its own process: when the file changes under
3035 // us, every open page hears it, as if the change were made here.
@@ -80,6 +85,37 @@ export function serve(options: ServeOptions) {
8085 return server;
8186 }
8287
88 // A database that will not open or upgrade must not take the process down:
89 // the forge would see 502 and no reason. Listen, say which step failed on
90 // /healthz (step and message only, no stack), and answer every other path
91 // with a plain page. Nothing touches the file: the deals stay as found.
92 function unopened(options: ServeOptions, error: unknown) {
93 const step = error instanceof OpenError ? error.step : "open";
94 const detail = error instanceof OpenError ? error.detail : error instanceof Error ? error.message : String(error);
95 console.error(`${title}: database cannot be opened at boot, step "${step}" failed: ${detail}`);
96 const headers = { "cache-control": "no-store", "retry-after": "60" };
97 const answer = (response: Response) => {
98 for (const [k, v] of Object.entries(headers)) response.headers.set(k, v);
99 return response;
100 };
101 return Bun.serve({
102 port: options.port,
103 hostname: options.hostname ?? "127.0.0.1",
104 idleTimeout: 60,
105 fetch(request) {
106 const { pathname } = new URL(request.url);
107 const response =
108 pathname === "/healthz"
109 ? text(`unhealthy: ${step} failed: ${detail}\n`, 503)
110 : html("<!doctype html><title>Unavailable</title><h1>The app cannot open its records</h1><p>Nothing has been changed. Try again shortly.</p>", 503);
111 return head(request, answer(response));
112 },
113 error() {
114 return answer(text("unhealthy\n", 503));
115 },
116 });
117 }
118
83119 // A page is mostly repeated markup: gzipped, the clock is a tenth the size.
84120 // Only HTML, only when asked for, and only past a size worth the trouble.
85121 async function squeeze(request: Request, response: Response): Promise<Response> {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs