mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
fix(docsync): read the retry limit back out, debounce the webhook, and cover the UI
The attempt counter and the backoff were written on every failure and never consulted when planning the next run: rows were found in a live database at 46 attempts against a limit of 6, so a document a provider refuses — one it reads as corrupt, a type it will not take, a full quota — was re-uploaded on every cron tick forever. planReconcile now skips a row that is shelved or still inside its window, and a manual run clears the counters, which is what "waits for a person" has to mean. The webhook had the same shape of bug: WEBHOOK_NUDGE_DEBOUNCE_SECONDS existed, with a comment explaining it, and nothing read it. Providers fire once per document, so twenty files dropped into a watched folder were twenty runs — the in-flight guard answered `busy` to nineteen of them, throwing away nineteen announcements, and the one run that did start had begun before most of the changes landed. A burst now folds into one trailing run, and the binding is re-read when the timer fires so a sync switched off in between does not get a last run out of a stale row. Four defects the new suites turned up, all fixed: the sync button lost its accessible name below sm, where its label is hidden; the stored address was never shown in the connect dialog although the form kept submitting it; the allow-self-signed switch only read the connection at mount, so a stored "on" showed as off and the next save wrote that back; and a refused folder create escaped as an unhandled rejection with nothing said to anybody. Also here: the phone shell gets the same feature (one sheet, four views, the same hook as the desktop), the marks come from the product's own icon files rather than redrawn approximations, and the 22 non-English locales carry the new keys. ADMIN-SVC-069 asserted that `documents` has no MCP surface. It does now, so the test takes its example from MCP_GATED_ADDON_IDS instead of naming one.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Controller, HttpCode, Param, Post, Req } from '@nestjs/common';
|
||||
import { Controller, HttpCode, OnModuleDestroy, Param, Post, Req } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import crypto from 'crypto';
|
||||
import { Public } from '../auth/public.decorator';
|
||||
import { WEBHOOK_NUDGE_DEBOUNCE_SECONDS } from './doc-sync.constants';
|
||||
import { DocSyncConfigService } from './doc-sync-config.service';
|
||||
import { DocSyncService } from './doc-sync.service';
|
||||
|
||||
@@ -22,12 +23,29 @@ import { DocSyncService } from './doc-sync.service';
|
||||
* supports it, a shared secret is checked as well.
|
||||
*/
|
||||
@Controller('api/docsync/webhook')
|
||||
export class DocSyncWebhookController {
|
||||
export class DocSyncWebhookController implements OnModuleDestroy {
|
||||
/**
|
||||
* One pending run per binding, so a burst folds into a single pass.
|
||||
*
|
||||
* Providers fire per document: dropping twenty files into a watched Nextcloud
|
||||
* folder is twenty calls within a second or two. Each one used to start its
|
||||
* own run, which the service's in-flight guard then answered with `busy` —
|
||||
* so nineteen changes were announced and thrown away, and the one run that
|
||||
* did start had begun before most of them landed. Collecting them for a beat
|
||||
* and then running once is both less work and more correct.
|
||||
*/
|
||||
private readonly pending = new Map<number, ReturnType<typeof setTimeout>>();
|
||||
|
||||
constructor(
|
||||
private readonly config: DocSyncConfigService,
|
||||
private readonly sync: DocSyncService,
|
||||
) {}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
for (const timer of this.pending.values()) clearTimeout(timer);
|
||||
this.pending.clear();
|
||||
}
|
||||
|
||||
@Post(':token')
|
||||
@Public('A provider cannot hold a TREK session; the per-link token in the URL is the authentication, and the call can only ever trigger a sync run.')
|
||||
@HttpCode(200)
|
||||
@@ -42,11 +60,34 @@ export class DocSyncWebhookController {
|
||||
|
||||
// Fire and forget. Paperless allows five seconds before it counts the call
|
||||
// as failed and retries, and a sync run takes longer than that whenever
|
||||
// there is anything to do.
|
||||
void this.sync.syncLink(link);
|
||||
// there is anything to do — so the answer goes out now and the run happens
|
||||
// after the debounce window, by which time the rest of the burst has
|
||||
// arrived and been folded into this same timer.
|
||||
this.schedule(link.id, () => this.config.getLink(link.id));
|
||||
return { received: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Start one run per binding per window, trailing rather than leading.
|
||||
*
|
||||
* Trailing on purpose: the first call of a burst is the least informed one,
|
||||
* because the provider is usually still writing the rest. The link is looked
|
||||
* up again when the timer fires, so a binding switched off or deleted in the
|
||||
* meantime does not get one last run out of a stale row.
|
||||
*/
|
||||
private schedule(linkId: number, reload: () => ReturnType<DocSyncConfigService['getLink']>): void {
|
||||
if (this.pending.has(linkId)) return;
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(linkId);
|
||||
const fresh = reload();
|
||||
if (!fresh || fresh.sync_enabled !== 1) return;
|
||||
void this.sync.syncLink(fresh);
|
||||
}, WEBHOOK_NUDGE_DEBOUNCE_SECONDS * 1000);
|
||||
// A pending nudge must not hold the process open at shutdown.
|
||||
if (typeof timer.unref === 'function') timer.unref();
|
||||
this.pending.set(linkId, timer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept either a plain shared-secret header (Paperless workflows, Nextcloud
|
||||
* `authMethod: header`) or Papra's standard-webhooks HMAC. Compared in
|
||||
|
||||
Reference in New Issue
Block a user