Skip to content
Open
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
80 changes: 80 additions & 0 deletions apps/web/src/api/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions apps/web/src/pages/BackgroundWork.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api, result } from "../api/client";
import type { components } from "../api/schema";
import { Loading, Notice } from "../components";

type Pace = components["schemas"]["BackgroundWork"]["pace"];

const paces: { id: Pace; title: string; description: string }[] = [
{
id: "normal",
title: "Normal",
description: "Every background job runs on its usual schedule.",
},
{
id: "relaxed",
title: "Relaxed",
description:
"Background jobs run 5 times less often. Downloads, imports and list checks are noticed a little later.",
},
{
id: "light",
title: "Light",
description:
"Background jobs run 20 times less often. Good for a small server that also does other work.",
},
{
id: "minimal",
title: "Minimal",
description:
"Background jobs run 50 times less often. Finished downloads can take up to an hour to import.",
},
];

export default function BackgroundWork() {
const cache = useQueryClient();
const setting = useQuery({
queryKey: ["background-work"],
queryFn: async () => result(await api.GET("/api/background-work")),
});
const [draft, setDraft] = useState<Pace | null>(null);
const pace = draft ?? setting.data?.pace ?? "normal";
const save = useMutation({
mutationFn: async (submitted: Pace) =>
result(
await api.PUT("/api/background-work", { body: { pace: submitted } }),
),
onSuccess: (saved, submitted) => {
setDraft((current) => (current === submitted ? null : current));
cache.setQueryData(["background-work"], saved);
},
});
return (
<section className="panel editor" aria-labelledby="background-work-title">
<h2 id="background-work-title">Background work</h2>
<p className="muted">
Dewarr checks downloads, imports, lists and connections on a schedule.
On a small or shared server, a slower pace saves CPU and database work.
Searching and requesting books are not affected.
</p>
<p className="access-hint">
New in this version. Normal keeps the schedule Dewarr has always used.
</p>
{setting.isPending ? (
<Loading />
) : setting.data ? (
<form
className="editor"
onSubmit={(event) => {
event.preventDefault();
save.mutate(pace);
}}
>
<fieldset className="access-choice-list">
<legend>How often background jobs run</legend>
{paces.map((option) => (
<label key={option.id} className="check-label">
<input
type="radio"
name="background-pace"
value={option.id}
checked={pace === option.id}
onChange={() => setDraft(option.id)}
/>
<span>
<strong>{option.title}</strong>
<span className="access-hint"> {option.description}</span>
</span>
</label>
))}
</fieldset>
<button className="primary" disabled={save.isPending}>
{save.isPending ? "Saving…" : "Save background work"}
</button>
{save.isSuccess && (
<p role="status">
Background work saved. It applies within a minute.
</p>
)}
</form>
) : null}
<Notice error={setting.error || save.error} />
</section>
);
}
10 changes: 10 additions & 0 deletions apps/web/src/pages/SettingsSections.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BackgroundWork from "./BackgroundWork";
import { lazy } from "react";
import SettingsGroup from "../components/SettingsGroup";

Expand Down Expand Up @@ -102,6 +103,15 @@ export function settingsSections(role: string, permissions: string[] = []) {
title: "Notifications",
content: <Notifications admin={admin} />,
},
...(admin
? [
{
id: "background-work",
title: "Background work",
content: <BackgroundWork />,
},
]
: []),
{ id: "logs", title: "Logs", content: <Logs admin={admin} /> },
];
}
47 changes: 47 additions & 0 deletions apps/web/tests/ui/background-work.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test } from "../fixtures";

test("admins choose how often background jobs run", async ({ page }) => {
const saved: unknown[] = [];
let pace = "normal";
await page.route("**/api/**", async (route) => {
const request = route.request();
const path = new URL(request.url()).pathname;
let data: unknown = { items: [], total: 0, offset: 0, limit: 40 };
if (path === "/api/auth/me")
data = {
user: {
id: "admin",
role: "admin",
username: "admin",
display_name: "Admin",
},
csrf_token: "test",
};
else if (path === "/api/setup/onboarding") data = { status: "completed" };
else if (path === "/api/library/libraries") data = [];
else if (path === "/api/background-work") {
if (request.method() === "PUT") {
saved.push(request.postDataJSON());
pace = (request.postDataJSON() as { pace: string }).pace;
}
data = { pace };
}
return route.fulfill({ json: data });
});
await page.goto("/settings#background-work");
const section = page.getByRole("region", { name: "Background work" });
await expect(section.getByText("New in this version.")).toBeVisible();
await expect(section.getByRole("radio", { name: /^Normal/ })).toBeChecked();
await section.getByRole("radio", { name: /^Light/ }).check();
await section.getByRole("button", { name: "Save background work" }).click();
await expect(section.getByRole("status")).toHaveText(
"Background work saved. It applies within a minute.",
);
expect(saved).toEqual([{ pace: "light" }]);
await page.reload();
await expect(
page
.getByRole("region", { name: "Background work" })
.getByRole("radio", { name: /^Light/ }),
).toBeChecked();
});
77 changes: 77 additions & 0 deletions docs/openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading