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
21 changes: 12 additions & 9 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -2717,15 +2717,18 @@ and a download SABnzbd's post-processor is working on (`loaded` in the history)
is the wait, which holds a Force job too. They are read too for a Force job
beside a `queued` row, and for the whole queue, which a `queued` row shows
paused by hand, since the wait can hold it as well. The switch takes one read,
shared for ten seconds, and the history one more when it is on. A pause is sent,
and keeps the queue paused once the wait ends, unless a `queued` row shows it
paused by hand already; a resume is sent too, since a manual pause can lie
behind the wait, and its preview says that downloading starts once the wait
ends. A pause by hand with no `queued` row that leaves the rows looking like
the wait, or that comes while the switch is on and a download is in
post-processing, reads the same way, since neither tells the two apart: a second pause is then offered, and
`diagnose_media` still says SABnzbd resumes by itself. An `id` the client does
not have is refused as not found.
shared for ten seconds, and the history one more when it is on. A history read
that shows a download in post-processing is shared the same way, so for up to
ten seconds after the wait ends the queue can still read as held by it; one that
shows none is not kept, since post-processing can start at any moment. A pause
is sent, and keeps the queue paused once the wait ends, unless a `queued` row
shows it paused by hand already; a resume is sent too, since a manual pause can
lie behind the wait, and its preview says that downloading starts once the wait
ends. A pause by hand with no `queued` row that leaves the rows looking like the
wait, or that comes while the switch is on and a download is in post-processing,
reads the same way, since neither tells the two apart: a second pause is then
offered, and `diagnose_media` still says SABnzbd resumes by itself. An `id` the
client does not have is refused as not found.

**Unverified live** (qBittorrent): pause, resume and the speed limit.

Expand Down
33 changes: 26 additions & 7 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export class TtlCache {
}
}

/** How long a read shared inside one get_stack_health pass stays fresh. */
/**
* How long a short shared read stays fresh: inside one get_stack_health
* pass, and SABnzbd's switch and history reads for its post-processing wait.
*/
export const SHORT_READ_TTL_MS = 10_000;

type Cached<T> = (() => Promise<T>) & { clear(): void };
Expand All @@ -117,18 +120,34 @@ type Cached<T> = (() => Promise<T>) & { clear(): void };
* One in-flight or recent read, shared by every caller until `ttlMs` passes.
* The ttl counts from the start of a load, so a load slower than `ttlMs`
* is already stale when it resolves and shares nothing.
* A failed read is dropped at once, so the next caller retries. `clear()`
* forgets the current read, for a caller that must see fresh data.
* A failed read is dropped at once, so the next caller retries, and so is
* one that `keep`, when given, refuses or throws on: the callers sharing it
* still get its value, and a throw from `keep` is not passed on. Either is
* dropped only if no newer read has taken its place, for the reason
* `TtlCache.get` gives for `ttlFor`. `clear()` forgets the current read, for
* a caller that must see fresh data.
*/
export function cached<T>(ttlMs: number, load: () => Promise<T>): Cached<T> {
export function cached<T>(ttlMs: number, load: () => Promise<T>, keep?: (value: T) => boolean): Cached<T> {
let entry: { at: number; value: Promise<T> } | undefined;
const read = (): Promise<T> => {
if (entry === undefined || Date.now() - entry.at > ttlMs) {
const value = load();
const current = { at: Date.now(), value };
value.catch(() => {
if (entry === current) entry = undefined;
});
value.then(
resolved => {
if (keep === undefined || entry !== current) return;
let kept: boolean;
try {
kept = keep(resolved);
} catch {
kept = false;
}
if (!kept) entry = undefined;
},
() => {
if (entry === current) entry = undefined;
}
);
entry = current;
}
return entry.value;
Expand Down
25 changes: 19 additions & 6 deletions src/services/sabnzbd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ const sabWaiting = (queue: RawQueue['queue']): boolean => {
const waitUnknown = (queue: RawQueue['queue'], slots: readonly RawSlot[]): boolean =>
queue?.paused === true && !sabWaiting(queue) && (!anyQueued(queue) || slots.some(s => s.priority === 'Force'));

/** Whether the history shows SABnzbd's post-processor working on a download (`loaded`), as `#waitOn` reads it. */
const inPostProcessing = (history: RawHistory): boolean => (history.history?.slots ?? []).some(s => s.loaded === true);

/**
* What holds each slot while the queue's `paused` flag is set (`sabWaiting`).
* `waitOn` is the wait as `#waitOn` read it where the slots cannot tell: then
Expand Down Expand Up @@ -243,6 +246,12 @@ export class SabnzbdAdapter
readonly #waitSwitch = cached(SHORT_READ_TTL_MS, () =>
this.#read<RawWaitSwitch>('/api?mode=get_config&section=misc&keyword=pause_on_post_processing&output=json')
);
/** The history's first ten rows, where `#waitOn` looks for the download in post-processing; kept only when they show one. */
readonly #waitHistory = cached(
SHORT_READ_TTL_MS,
() => this.#read<RawHistory>('/api?mode=history&limit=10&output=json'),
inPostProcessing
);

constructor(config: ConfigByService['sabnzbd'], fetchImpl: typeof fetch = fetch) {
this.instance = config.name;
Expand Down Expand Up @@ -576,17 +585,21 @@ export class SabnzbdAdapter
* marks `loaded`. A job only queued for post-processing, as one is while
* post-processing is paused, does not. Only that one setting is asked
* for: `get_config` with no keyword answers every setting, the API and NZB
* keys among them. It changes rarely, so it is shared for a few seconds,
* as a stack health pass shares its reads. The history lists the
* downloads in post-processing first. A read that fails answers no, which
* leaves the slots' own reading.
* keys among them. It changes rarely, so it is shared for ten seconds, as
* a stack health pass shares its reads. The history lists the downloads in
* post-processing first. A read that shows one is shared the same way, so
* for up to ten seconds after the wait ends the queue can still read as
* held by it. A read that shows none is not kept: post-processing can
* start at any moment, and a stale no would have pause_downloads answer
* "already paused" for a queue the wait has just started holding, and send
* nothing. A failed read answers no, which leaves the slots' own reading,
* and the next call reads again.
*/
async #waitOn(): Promise<boolean> {
try {
const on = (await this.#waitSwitch()).config?.misc?.pause_on_post_processing;
if (on !== 1 && on !== true) return false;
const history = await this.#read<RawHistory>('/api?mode=history&limit=10&output=json');
return (history.history?.slots ?? []).some(s => s.loaded === true);
return inPostProcessing(await this.#waitHistory());
} catch {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/tools/retryDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const HOLDS_READ: Readonly<Record<'downloading' | 'processing', ReadonlySet<Queu
processing: new Set(['queue', 'postPaused', 'queueAndPostPaused'])
};

/** The result fields that say what holds a job, in `HOLD_FLAGS` order, which `applied` reads back. */
/** The result fields that say what holds a job, which `applied` reads back. */
const HOLD_FIELDS: Readonly<Record<QueueHold, readonly HoldFlag[]>> = {
queue: ['queue_paused'],
postProcessing: ['post_processing_wait'],
Expand Down Expand Up @@ -268,15 +268,15 @@ export function registerRetryDownload(server: McpServer, context: WriteContext,
download_id: string;
paused_itself?: true;
};
const flags = HOLD_FLAGS.filter(flag => r[flag] === true).join();
const view = views.get(plan);
const lines = [`Applied. ${plan.summary}`];
if (r.previous_id !== undefined) lines.push(`Its id is now ${r.id}.`);
lines.push(
describeOutcome(
{
...r,
...withHold((Object.keys(HOLD_FIELDS) as QueueHold[]).find(hold => HOLD_FIELDS[hold].join() === flags)),
// The hold whose fields are exactly the ones set, whatever their order.
...withHold((Object.keys(HOLD_FIELDS) as QueueHold[]).find(hold => HOLD_FLAGS.every(flag => (r[flag] === true) === HOLD_FIELDS[hold].includes(flag)))),
...(r.paused_itself === true ? { pausedItself: true } : {})
},
r.service
Expand Down
36 changes: 36 additions & 0 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,40 @@ describe('cached', () => {
await expect(read()).resolves.toBe('second');
expect(load).toHaveBeenCalledTimes(2);
});

it('drops a read that keep refuses, so the next caller reads again', async () => {
const load = vi.fn<() => Promise<string>>().mockResolvedValueOnce('no').mockResolvedValue('yes');
const read = cached(1000, load, value => value === 'yes');
// Read again straight from the first caller's continuation: the drop has already happened.
expect(await read()).toBe('no');
expect(await read()).toBe('yes');
expect(await read()).toBe('yes');
expect(load).toHaveBeenCalledTimes(2);
});

it('keeps a newer read when an older one keep refuses resolves late', async () => {
let resolveFirst: (value: string) => void = () => {};
const load = vi
.fn<() => Promise<string>>()
.mockImplementationOnce(() => new Promise<string>(resolve => (resolveFirst = resolve)))
.mockResolvedValue('yes');
const read = cached(1000, load, value => value === 'yes');
const first = read();
read.clear();
await expect(read()).resolves.toBe('yes');
resolveFirst('no');
await expect(first).resolves.toBe('no');
await expect(read()).resolves.toBe('yes');
expect(load).toHaveBeenCalledTimes(2);
});

it('drops a read that keep throws on', async () => {
const load = vi.fn(async () => 'v');
const read = cached(1000, load, () => {
throw new Error('cannot judge');
});
await expect(read()).resolves.toBe('v');
await expect(read()).resolves.toBe('v');
expect(load).toHaveBeenCalledTimes(2);
});
});
36 changes: 35 additions & 1 deletion test/pauseDownloads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,48 @@ describe('sabnzbd pause', () => {
expect((await adapter.getQueue()).map(row => row.heldBy)).toEqual(['queue', 'postProcessing']);
});

it('shares the switch for a few seconds, and reads the history each time', async () => {
it('shares the switch, and a history that shows the wait, for a few seconds', async () => {
const { adapter, seen } = sab([['Downloading', 'Force']]);
await adapter.getQueue();
await adapter.getQueue();
await adapter.readPauseState();
expect(reads(seen, 'get_config')).toBe(1);
expect(reads(seen, 'history')).toBe(1);
});

it('reads the history again after a failed read, which answers no', async () => {
const opts: NonNullable<Parameters<typeof sab>[1]> = { refuse: 'history' };
const { adapter, seen } = sab([['Downloading', 'Force']], opts);
expect((await adapter.getQueue())[0]?.heldBy).toBeUndefined();
delete opts.refuse;
expect((await adapter.getQueue())[0]?.heldBy).toBe('postProcessing');
expect(reads(seen, 'history')).toBe(2);
});

// Post-processing can start at any moment, so a history showing none is read again.
it('reads the history again while it shows nothing in post-processing', async () => {
const opts: NonNullable<Parameters<typeof sab>[1]> = { history: [['Completed', false]] };
const { adapter, seen } = sab([['Downloading', 'Force']], opts);
expect((await adapter.getQueue())[0]?.heldBy).toBeUndefined();
await expect(adapter.readPauseState()).resolves.toEqual({ scope: 'the SABnzbd queue', paused: true });
opts.history = [['Extracting', true], ['Completed', false]];
expect((await adapter.getQueue())[0]?.heldBy).toBe('postProcessing');
await expect(adapter.readPauseState()).resolves.toEqual({ scope: 'the SABnzbd queue', paused: false, heldBy: 'postProcessing' });
expect(reads(seen, 'history')).toBe(3);
});

it('previews a pause once the wait starts, though the queue read as paused by hand a moment before', async () => {
const opts: NonNullable<Parameters<typeof sab>[1]> = { history: [['Completed', false]] };
const { adapter } = sab([['Downloading', 'Force']], opts);
const pause = () => harness({ adapters: [adapter] }).call({ service: 'sabnzbd', action: 'pause' });
expect((await pause()).structuredContent.noop).toBe(true);
opts.history = [['Extracting', true], ['Completed', false]];
const { structuredContent } = await pause();
expect(structuredContent.noop).toBe(false);
expect(structuredContent.confirm_token).toBeDefined();
expect(structuredContent.effects[0]).toContain('reads as waiting on post-processing');
});

it('reads neither for an empty queue, which has no row to hold', async () => {
const { adapter, seen } = sab([]);
await expect(adapter.getQueue()).resolves.toEqual([]);
Expand Down
Loading