Keep retrying lookups until the sources answer
A network error, timeout, 5xx or 429 no longer ends a lookup or sends a book to Needs attention. Lookups back off exponentially with jitter (2 s doubling to 5 min, honouring Retry-After) for as long as the book is pending, keep each definitive answer so a retry only asks for what is missing, and limit requests to two at a time per host. Once a call number is in hand, a step that still fails after six tries is left for the backfills. Books an earlier version failed for a temporary reason are queued again at startup. The cards show which source is not answering and when the next try is, title search waits for Open Library with a Cancel button, and Look up again sits next to Fix. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
ee10e00ccd
commit
e65b10c0fb
24 files changed
+700
-104
No files matched your search
@@ -2,6 +2,7 @@ import { displayAuthors } from '../../shared/names.ts';
|
||||
import type { Book } from '../../shared/types.ts';
|
||||
import { rowState } from '../bookstate.ts';
|
||||
import { CallNo } from './CallNo.tsx';
|
||||
import { LookUpAgain } from './LookUpAgain.tsx';
|
||||
import { StateMark, StateText } from './StateMark.tsx';
|
||||
|
||||
function Main({ book, showYear }: { book: Book; showYear: boolean }) {
|
||||
@@ -24,7 +25,7 @@ export function QueueRow({ book, onFix, onRemove }: { book: Book; onFix: () => v
|
||||
<CallNo value={kind === 'looking' ? null : book.callNumber} approximate={book.confidence === 'approximate'} source={book.source} />
|
||||
<Main book={book} showYear={false} />
|
||||
<span class="row-actions">
|
||||
{kind === 'attention' ? <button type="button" class="btn" onClick={onFix}>Fix</button> : null}
|
||||
{kind === 'attention' ? <><LookUpAgain book={book} /><button type="button" class="btn" onClick={onFix}>Fix</button></> : null}
|
||||
<button type="button" class="btn-icon" onClick={onRemove} aria-label={`Remove "${book.title}"`} title="Remove">×</button>
|
||||
</span>
|
||||
</li>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { useEffect, useState } from 'preact/hooks';
|
||||
import { useEffect, useRef, useState } from 'preact/hooks';
|
||||
import type { Book, SearchResult } from '../../shared/types.ts';
|
||||
import { displayAuthors } from '../../shared/names.ts';
|
||||
import { api } from '../api.ts';
|
||||
import { Cancelled, searchPatiently } from '../patient.ts';
|
||||
import type { BookPatch } from '../api.ts';
|
||||
import { getBook, setLastScan, upsertBook, dispatch, useBooks } from '../live.ts';
|
||||
import { showToast } from '../toast-store.ts';
|
||||
@@ -204,21 +205,30 @@ function FixSearch({ initial, onPick }: { initial: string; onPick: (r: SearchRes
|
||||
const [active, setActive] = useState(-1);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [message, setMessage] = useState('');
|
||||
const [waiting, setWaiting] = useState(0); // the try the search is on once Open Library has not answered (2, 3, ...)
|
||||
const stop = useRef<AbortController | null>(null);
|
||||
const seq = useRef(0); // only the newest search may write to the form
|
||||
|
||||
async function search() {
|
||||
const { title, author } = splitTitleAuthor(text);
|
||||
if (!title) return;
|
||||
setBusy(true); setMessage(''); setResults(null); setActive(-1);
|
||||
const mine = ++seq.current;
|
||||
stop.current?.abort();
|
||||
const ctl = stop.current = new AbortController();
|
||||
setBusy(true); setMessage(''); setResults(null); setActive(-1); setWaiting(0);
|
||||
try {
|
||||
const found = await api.search(title, author);
|
||||
const found = await searchPatiently(title, author, ctl.signal, n => { if (mine === seq.current) setWaiting(n); });
|
||||
if (mine !== seq.current) return;
|
||||
setResults(found);
|
||||
if (found.length === 0) setMessage('No matches. Edit the details by hand below.');
|
||||
} catch (e) {
|
||||
if (e instanceof Cancelled || mine !== seq.current) return;
|
||||
setMessage(`The search failed: ${(e as Error).message}`);
|
||||
}
|
||||
setBusy(false);
|
||||
setBusy(false); setWaiting(0);
|
||||
}
|
||||
useEffect(() => { if (initial) void search(); }, []);
|
||||
const cancel = () => { seq.current++; stop.current?.abort(); setBusy(false); setWaiting(0); };
|
||||
useEffect(() => { if (initial) void search(); return () => { seq.current++; stop.current?.abort(); }; }, []);
|
||||
|
||||
const choose = (r: SearchResult) => { onPick(r); setResults(null); setMessage('Details copied below. Check them, then save.'); };
|
||||
const open = !!results && results.length > 0;
|
||||
@@ -244,7 +254,10 @@ function FixSearch({ initial, onPick }: { initial: string; onPick: (r: SearchRes
|
||||
<button type="button" class="btn" disabled={busy || !text.trim()} onClick={() => void search()}>Search</button>
|
||||
</span>
|
||||
</label>
|
||||
{busy ? <p class="help"><Spinner /> Searching…</p> : null}
|
||||
{busy ? (
|
||||
<p class="help"><Spinner /> {waiting > 1 ? `Waiting for Open Library… (try ${waiting})` : 'Searching…'}
|
||||
{waiting > 1 ? <> <button type="button" class="btn" onClick={cancel}>Cancel</button></> : null}</p>
|
||||
) : null}
|
||||
{message ? <p class="help" role="status">{message}</p> : null}
|
||||
{open ? <ResultList id="fix-results" label="Search results" results={results!} active={active} onPick={choose} onHover={setActive} /> : null}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useState } from 'preact/hooks';
|
||||
import type { Book } from '../../shared/types.ts';
|
||||
import { api } from '../api.ts';
|
||||
import { upsertBook } from '../live.ts';
|
||||
import { showError } from '../toast-store.ts';
|
||||
|
||||
/** Asks the catalogues again for a book that needs attention: they add records over time. Not for hand-entered numbers. */
|
||||
export function LookUpAgain({ book }: { book: Book }) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
if (book.manualOverride) return null;
|
||||
async function again() {
|
||||
setBusy(true);
|
||||
try { upsertBook(await api.retryBook(book.id)); }
|
||||
catch (e) { showError(`Could not look up "${book.title}" again: ${(e as Error).message}`); }
|
||||
setBusy(false);
|
||||
}
|
||||
return <button type="button" class="btn" disabled={busy} onClick={() => void again()}>Look up again</button>;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Book } from '../../shared/types.ts';
|
||||
import { elapsedText, rowState } from '../bookstate.ts';
|
||||
import { lookupStartedAt, useNow } from '../live.ts';
|
||||
import type { Book, LookupRetry } from '../../shared/types.ts';
|
||||
import { elapsedText, retryText, rowState } from '../bookstate.ts';
|
||||
import { lookupStartedAt, retryReceivedAt, useNow } from '../live.ts';
|
||||
|
||||
export const LABEL = { looking: 'Looking up…', ready: 'Ready', attention: 'Needs attention' } as const;
|
||||
|
||||
@@ -20,12 +20,12 @@ export function StateMark({ book }: { book: Book }) {
|
||||
return <span class="mark mark-ready" role="img" aria-label={LABEL.ready} title={LABEL.ready}><CheckIcon /></span>;
|
||||
}
|
||||
|
||||
function LookingText({ book, note }: { book: Book; note: string | null }) {
|
||||
function LookingText({ book, retry }: { book: Book; retry: LookupRetry | null }) {
|
||||
const now = useNow(true);
|
||||
return (
|
||||
<span class="state-text state-looking">
|
||||
{LABEL.looking} <span class="elapsed">{elapsedText(now - lookupStartedAt(book))}</span>
|
||||
{note ? <span class="note"> · {note}</span> : null}
|
||||
{retry ? <span class="note"> · {retryText(retry, retryReceivedAt(book), now)}</span> : null}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ function LookingText({ book, note }: { book: Book; note: string | null }) {
|
||||
/** The worded state under a title: "Looking up… 12 s", or "Needs attention · reason". Nothing when ready. */
|
||||
export function StateText({ book }: { book: Book }) {
|
||||
const s = rowState(book);
|
||||
if (s.kind === 'looking') return <LookingText book={book} note={s.note} />;
|
||||
if (s.kind === 'looking') return <LookingText book={book} retry={s.retry} />;
|
||||
if (s.kind === 'attention') return <span class="state-text state-attention">{LABEL.attention} · {s.reason}</span>;
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user