diff --git a/docs/tools.md b/docs/tools.md index 69dab5cc..07ac2076 100644 --- a/docs/tools.md +++ b/docs/tools.md @@ -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. diff --git a/src/core/cache.ts b/src/core/cache.ts index dd8b57ed..8ab6fc43 100644 --- a/src/core/cache.ts +++ b/src/core/cache.ts @@ -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 = (() => Promise) & { clear(): void }; @@ -117,18 +120,34 @@ type Cached = (() => Promise) & { 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(ttlMs: number, load: () => Promise): Cached { +export function cached(ttlMs: number, load: () => Promise, keep?: (value: T) => boolean): Cached { let entry: { at: number; value: Promise } | undefined; const read = (): Promise => { 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; diff --git a/src/services/sabnzbd.ts b/src/services/sabnzbd.ts index 38ec3853..e77098aa 100644 --- a/src/services/sabnzbd.ts +++ b/src/services/sabnzbd.ts @@ -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 @@ -243,6 +246,12 @@ export class SabnzbdAdapter readonly #waitSwitch = cached(SHORT_READ_TTL_MS, () => this.#read('/api?mode=get_config§ion=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('/api?mode=history&limit=10&output=json'), + inPostProcessing + ); constructor(config: ConfigByService['sabnzbd'], fetchImpl: typeof fetch = fetch) { this.instance = config.name; @@ -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 { try { const on = (await this.#waitSwitch()).config?.misc?.pause_on_post_processing; if (on !== 1 && on !== true) return false; - const history = await this.#read('/api?mode=history&limit=10&output=json'); - return (history.history?.slots ?? []).some(s => s.loaded === true); + return inPostProcessing(await this.#waitHistory()); } catch { return false; } diff --git a/src/tools/retryDownload.ts b/src/tools/retryDownload.ts index 113327ff..64875384 100644 --- a/src/tools/retryDownload.ts +++ b/src/tools/retryDownload.ts @@ -154,7 +154,7 @@ const HOLDS_READ: Readonly> = { queue: ['queue_paused'], postProcessing: ['post_processing_wait'], @@ -268,7 +268,6 @@ 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}.`); @@ -276,7 +275,8 @@ export function registerRetryDownload(server: McpServer, context: WriteContext, 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 diff --git a/test/cache.test.ts b/test/cache.test.ts index 17b2ec6a..2fb49bf9 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -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>().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>() + .mockImplementationOnce(() => new Promise(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); + }); }); diff --git a/test/pauseDownloads.test.ts b/test/pauseDownloads.test.ts index 431bc62f..16093e28 100644 --- a/test/pauseDownloads.test.ts +++ b/test/pauseDownloads.test.ts @@ -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[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[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[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([]);