From 6776d9106fc76cd888504a656aceee9f5070b303 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 11 Apr 2026 12:33:30 +0500 Subject: [PATCH] fix: catalogue job always shows 0 counters after cancel/finish Two bugs fixed in runScrapeTask / runCatalogueTask: 1. FinishScrapeTask was called with the task's own context, which is already cancelled when the task is stopped. The PATCH to PocketBase failed silently, leaving all counters at their initial zero values. Fix: use a fresh context.WithTimeout(Background, 15s) for the write. 2. BooksFound was double-counted: RunBook already sets BooksFound=1 on success, but the accumulation loop added an extra +1 unconditionally, reporting 2 books per successful scrape. Fix: result.BooksFound += bookResult.BooksFound (drop the + 1). --- backend/internal/runner/runner.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/internal/runner/runner.go b/backend/internal/runner/runner.go index 66a4edf..56b2179 100644 --- a/backend/internal/runner/runner.go +++ b/backend/internal/runner/runner.go @@ -505,7 +505,11 @@ func (r *Runner) runScrapeTask(ctx context.Context, task domain.ScrapeTask) { log.Warn("runner: unknown task kind") } - if err := r.deps.Consumer.FinishScrapeTask(ctx, task.ID, result); err != nil { + // Use a fresh context for the final write so a cancelled task context doesn't + // prevent the result counters from being persisted to PocketBase. + finishCtx, finishCancel := context.WithTimeout(context.Background(), 15*time.Second) + defer finishCancel() + if err := r.deps.Consumer.FinishScrapeTask(finishCtx, task.ID, result); err != nil { log.Error("runner: FinishScrapeTask failed", "err", err) } @@ -551,7 +555,7 @@ func (r *Runner) runCatalogueTask(ctx context.Context, task domain.ScrapeTask, o TargetURL: entry.URL, } bookResult := o.RunBook(ctx, bookTask) - result.BooksFound += bookResult.BooksFound + 1 + result.BooksFound += bookResult.BooksFound result.ChaptersScraped += bookResult.ChaptersScraped result.ChaptersSkipped += bookResult.ChaptersSkipped result.Errors += bookResult.Errors