mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
Restoring a large account was one request: 94 s for 1.25 million readings.
A reverse proxy that cuts a request at 60 s (nginx's default) showed the
operator an error for a restore that went on to finish on the server.
The restore request now checks the typed confirmation, that the backup
exists and that its stored copy opens, admits one job for the account and
answers 202 with its id. The `backup-restore` queue runs the restore and
records its phase, counts and outcome on a `backup_restore_jobs` row
(migration 0347), which `GET /api/admin/backups/restores` and
`GET /api/admin/backups/restores/{id}` read back.
What a restore checks and writes is unchanged. The body moved from the
route into `restoreBackup` (src/lib/export/restore-backup.ts): every
refusal still happens before the first delete and the replacement is still
one transaction, so a failure part-way leaves the account as it was. While
it runs the account keeps showing its current data, and other writes are
neither refused nor held, as before.
- One restore per account: a partial unique index admits one queued or
running job, and a second request gets 409 `backup.restore.active` naming
the running job. A per-account advisory lock inside the transaction holds
even if a worker thought gone is still writing.
- Time budget: the job's expiry is two hours and a file whose transaction
limit would run past three quarters of it is refused before anything is
deleted (`jobDeadline`, beside `jobBudget`).
- Resuming: the job writes a heartbeat every two seconds. A boot sweep
queues a job whose worker stopped again from the start, at most twice,
then fails it as interrupted. The admission check releases an abandoned
job so it never holds the account's slot.
- The staged file is the stored `DataBackup` row, already encrypted at
rest. It stays whatever the outcome, so a retry needs no new upload. The
job records a digest of the copy and refuses one that was replaced while
it waited.
- Failures carry a stable code and a sentence that says what to do.
- The console follows the job: phase and counts while it runs, the outcome
after, and after a reload it finds the job again from the server. A poll
that fails on the network is never shown as a failed restore. A web
process separate from the worker evicts the account's caches when it
first reports the restore finished. The success line goes through the
outcome module, so an empty file is not drawn as a tick.
The model is registered in the backup plan (not carried) and the wipe plan
(wiped with the account); the queue in the maintenance registrar, the
subsystem surface and the job-fact allowlist; the new code in the error
catalogue; the routes in the OpenAPI contract. The tests that pin restore
semantics now drive the real route and the real job through one helper, and
`backup-restore-job.test.ts` pins the asynchronous contract itself.
102 lines
3.5 KiB
TypeScript
102 lines
3.5 KiB
TypeScript
/**
|
|
* A refused backup has to tell the operator what to do next.
|
|
*
|
|
* The server refuses a file whose manifest claims a section the file does not
|
|
* carry, before it deletes anything. That refusal reaches a person through the
|
|
* `code` on the restore job's failure, and a bare "restore failed" toast
|
|
* would waste it: the operator on the other end is frequently holding the only
|
|
* copy of an account, and the two facts they can act on — which section is
|
|
* missing, and that nothing was changed — have to stay on screen.
|
|
*
|
|
* Pinned here:
|
|
* 1. the missing sections are named, not counted,
|
|
* 2. the notice says nothing was changed and what to do instead,
|
|
* 3. no sections means no notice — a panel that appears after every restore
|
|
* trains the operator to ignore it,
|
|
* 4. the copy resolves in more than one locale,
|
|
* 5. `missingSectionsOf` keys on the failure code and not on the message text,
|
|
* so rewording the server's prose cannot silently remove the panel.
|
|
*
|
|
* Mutation check: render a count instead of the list and case 1 goes red; drop
|
|
* the remedy line and case 2 goes red; return the notice unconditionally and
|
|
* case 3 goes red; match on the message string in `missingSectionsOf` and case
|
|
* 5 goes red.
|
|
*/
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
|
|
import { I18nProvider } from "@/lib/i18n/context";
|
|
import {
|
|
RestoreRefusalNotice,
|
|
missingSectionsOf,
|
|
} from "@/components/admin/backups-section";
|
|
import type { MissingBackupSection } from "@/lib/export/restore-skips";
|
|
|
|
vi.mock("next/navigation", () => ({
|
|
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
|
|
usePathname: () => "/admin/backups",
|
|
}));
|
|
|
|
function render(
|
|
sections: MissingBackupSection[],
|
|
locale: "en" | "de" = "en",
|
|
): string {
|
|
return renderToStaticMarkup(
|
|
<I18nProvider initialLocale={locale}>
|
|
<RestoreRefusalNotice sections={sections} onDismiss={() => {}} />
|
|
</I18nProvider>,
|
|
);
|
|
}
|
|
|
|
describe("RestoreRefusalNotice", () => {
|
|
it("names every missing section", () => {
|
|
const html = render(["documents", "consent"]);
|
|
|
|
expect(html).toContain('data-slot="restore-refusal-notice"');
|
|
expect(html).toContain("Documents");
|
|
expect(html).toContain("Consent receipts");
|
|
});
|
|
|
|
it("says nothing was changed, and what to do instead", () => {
|
|
const html = render(["documents"]);
|
|
|
|
expect(html).toContain("Nothing was changed");
|
|
expect(html).toContain("disaster-recovery snapshot");
|
|
});
|
|
|
|
it("renders nothing when no section was missing", () => {
|
|
expect(render([])).toBe("");
|
|
});
|
|
|
|
it("speaks the operator's language", () => {
|
|
const german = render(["documents", "mentalHealth"], "de");
|
|
|
|
expect(german).toContain("Dokumente");
|
|
expect(german).toContain("Psychische Fragebögen");
|
|
expect(german).toContain("Es wurde nichts geändert");
|
|
});
|
|
});
|
|
|
|
describe("missingSectionsOf", () => {
|
|
it("reads the sections off the failed job", () => {
|
|
expect(
|
|
missingSectionsOf({
|
|
code: "backup.section.missing",
|
|
sections: ["documents"],
|
|
}),
|
|
).toEqual(["documents"]);
|
|
});
|
|
|
|
it("ignores a failure whose message merely looks like one", () => {
|
|
// The message is prose the server may reword. Only the code counts.
|
|
expect(
|
|
missingSectionsOf({ code: "something_else", sections: ["documents"] }),
|
|
).toEqual([]);
|
|
expect(missingSectionsOf(null)).toEqual([]);
|
|
});
|
|
|
|
it("survives a refusal with no section list", () => {
|
|
expect(missingSectionsOf({ code: "backup.section.missing" })).toEqual([]);
|
|
});
|
|
});
|