Resume paused scrapes from their cursor

This commit is contained in:
Fisto
2026-09-17 00:14:57 +02:00
parent 089bca0d4f
commit 0664b54241
10 changed files with 110 additions and 0 deletions
+16
View File
@@ -182,6 +182,22 @@ export class ScrapeEngine {
this.stopRequested.add(id);
}
/** Continue a paused scrape from its stored cursor instead of walking the top again. */
async resume(scrapeId: string, backend: ScrapeBackend): Promise<ScrapeMeta | null> {
const meta = this.active.get(scrapeId) ?? readScrapeMeta(join(this.options.rootDir, scrapeId));
if (!meta) return null;
if (meta.state === "running" || meta.state === "queued") return { ...meta };
this.stopRequested.delete(scrapeId);
meta.state = "queued";
this.active.set(scrapeId, meta);
writeScrapeMeta(join(this.options.rootDir, scrapeId), meta);
this.queue.push({ meta, dir: join(this.options.rootDir, scrapeId), backend });
void this.processQueue();
return { ...meta };
}
/** Cache previews for rows a finished or paused scrape never reached. Returns the count. */
async backfillPreviews(scrapeId: string): Promise<number> {
const meta = this.active.get(scrapeId) ?? readScrapeMeta(join(this.options.rootDir, scrapeId));
+27
View File
@@ -877,6 +877,33 @@ void (async () => {
assert.equal(existsSync(join(dir, `${poster?.hash}.webp`)), true);
});
await test("engine resumes a paused scrape from its cursor", async () => {
const first = new FakeBackend([
{ messages: [message("500"), message("499")] },
{ messages: [message("498")] }
]);
const engine = engineFor(root, first);
const started = await engine.start({ channelId: "140", guildId: null, mode: "links", backend: first });
engine.stop(started.id);
await engine.processQueue();
let meta = engine.get(started.id);
assert.equal(meta?.state, "paused");
assert.equal(meta?.cursor, "499");
assert.equal(meta?.messages, 2);
const second = new FakeBackend([{ messages: [message("498")] }]);
await engine.resume(started.id, second);
await engine.processQueue();
meta = engine.get(started.id);
assert.equal(meta?.state, "done");
assert.equal(meta?.messages, 3);
assert.equal(meta?.cursor, "498");
assert.equal(second.calls[0].before, "499");
});
await test("backfillPreviews fills rows left by a preview-less walk", async () => {
const backend = new FakeBackend([
{